can git be run with less file permissions than the files it tracks? - linux

here is the situation: i want to use git to track a directory of preference files that are owned by root.
it would be nice if git commands that don't modify the files (like git status and git diff) can be run as the user (without sudo).
through the following steps, i've created a repo that seems to function in a reasonable manner:
sudo mkdir .git
sudo chmod <user>:<group> .git
git init
git add .
git commit -m "initial commit"
i can run git status and git diff as the user. git checkout ... and git reset ... fail as the user, but seem to run fine when prefixed with sudo.
this seems perfect: no sudo needed to get into the repo and take a look at what's gone on, but to change the tracked files requires an extra level of effort, as it should for system files.
the question is: will this cause me any problems with how git is reading and writing files inside the .git directory? will any commands i need sudo to run end up creating things inside .git with root permission that will then cause problems trying to run commands that don't modify the tracked files as the user?
thanks and advance for any answers.

Related

Git tracking to wrong local directory

I created a new directory 438-healthme and cloned a repo into it.
When I index into that directory on the master branch and run a git status it lists all of my computer's files as untracked (see screenshot).
It seems like I set up git wrong a few years ago--is there a way to fix this?
You must have run git init in a folder. You need to find it and run rm -r .git.
Try again, removing that folder, and recreating it with:
git clone https://url/remote/repo 438-healthme
Then, in 438-healthme, there should be a .git subfolder, which means a git status (in the newly created 438-healthme folder) will show you files only from 438-healthme.

git pull doesn't apply deletions in kubuntu /var/www/html folder

It fails with warning: unable to unlink ... Permission denied
Order of operations:
Deleted some files, added the deleted files to staged files.
# On 1st local remote repository:*
git commit -m "deletion of folder with files"
# pushed to remote repository
git push origin branchName
# On local repository
git pull
gives the error: warning: unable to unlink ... Permission denied
I see that the files were not deleted.
Maybe it's related to the fact that repository is in /var/wwww/html ?
Usually i make changes with sudo there.
It seems also that change was applied in index after running:
git log -1
How to correctly apply this deletion by git pull ?
Running git pull now returns
Already up-to-date.
Your issue here is operating system permissions. It has nothing to do with git. If you look at your git status for that repo, you will likely see the files in /var/www/html as new untracked files as git thinks you've already deleted them. I would recommend finding a user that has the appropriate permissions and deleting them manually, or running something like the following:
sudo git reset --hard && git clean -fdx .
WARNING: This command will delete any non-tracked files and reset any tracked changes.

How can I verify git clone is working correctly?

I'm following the documentation provided here by git to setup a bare git repository in a folder called root.
I started in the root directory where I ran
git init
git -A *
git commit -m "test"
I then ran git status and all appears good.
Next I ran the line from the documentation at a directory one level above the repo I created above.
git clone --bare root root.git
This created root.git but I cannot see any evidence that anything was cloned I just see a set of files and directories when I cd root.git.
I don't know how to verify it was actually cloned, and if it was why can't I see the original files?
When you do --bare --- you are telling git to clone just the git portion -
This is the option you use when you want to have a remote repository that does not include a workspace.
If you want to verify that it actually cloned your changes, you'll want to clone it again in a different directory - without the --bare flag
I would recommend using the full path to do this:
cd /path/to/some/workspace
git clone /path/to/your/root.git successful-git-clone #that last bit is optional
This will put the workspace contents of root.git into a folder named successful-git-clone/ - without that last bit, it will default to root/ -
Even if you are in a bare repository, some git commands works and you could do a git branch to see if you have all your branches or git log to look at your commits...

Git ignore and changing the history (on Windows)

I've already read several posts about this here (like Git ignore & changing the past, How to remove files that are listed in the .gitignore but still on the repository?, and Applying .gitignore to committed files), but they have several problems:
Commands that only work on Linux.
Incomplete commands (like the first post I've linked to).
Only for one file.
I have pretty much no experience with Git so I was hoping for some help here.
What I'm basically trying to do is rescue one of my projects history. It's currently Hg and I converted it to Git with Hg-Git (all very easy) and it includes the history (great!). However, I also added a .gitignore file and added several new files & folders that I want completely gone from the history (like the bin and obj folders, but also files from ReSharper). So I'm looking for a way to apply the .gitignore file to all of my history. The commands should work on Windows as I have no intention of installing Linux for this.
No need to add the .gitignore in the history (there is no added value to do it), just add it for your future commits.
For the remove of files and directories in your history, use bfg-repo-cleaner which is fast, easy and works very well on Windows (done in scala).
It will do the job for you!
This is working for me:
Install hg-git.
cd HgFolder
hg bookmark -r default master
mkdir ../GitFolder
cd ../GitFolder
git init --bare
cd ../HgFolder
hg push ../GitFolder
Move all files from GitFolder to a '.git' folder (in this GitFolder) and set this folder to hidden (not the subfolders and files).
cd ../GitFolder
git init
git remote add origin https://url.git
Copy all current content (including .gitignore) to GitFolder.
git add .
git commit -m "Added existing content and .gitignore".
git filter-branch --index-filter "git rm --cache d -r --ignore-unmatch 'LINES' 'FROM' 'GITIGNORE'" --prune-empty --tag-name-filter cat -- --all
git rm -r --cached .
git add .
git gc --prune=now --aggressive
git push origin master --force
There is probably an easier way to do this and it might not be perfect but this had the result I wanted.

Git pull to www folder after git push

I would like to pull code to virtual host directory from GIT after a push. The problem is that commit hook runs as git user but the folder of virtual directory on apache is under www-data:www-data.
Is there any possibility to run script as www-data (shell script including git pull and some rm stuff for temp) instead of git user?
Thanks Martin.
While it is possible using sudo and defining specific commands, I'd recommend against it: This setup sounds easy to misconfigure or break.
I'd recommend that you have a cron job run a git pull every few minutes instead.

Resources