I have been caught out by what appears to be a common enough problem for git newbies.
I forgot to .gitignore a particular file, and adding it to .gitignore after having committed makes no difference.
I found this page on gitready which explains how to remove a file from the repository without removing it from the working tree (by using the command git rm --cached <file>
, which works ok, except that if I then try to merge that back into another branch, the files in the working tree get deleted.
Steps to reproduce, on an empty folder:
git init
touch want.txt
touch wantnot.txt
git add .
git commit -m "Initial"
git checkout -b BranchForIgnore
git rm --cached wantnot.txt
cat > .gitignore
wantnot.txt [Ctrl-D Ctrl-D here to exit cat]
git add .
git commit -m "Ignoring file"
Up to here, everything is fine
git checkout master
git merge BranchForIgnore
At this point, my wantnot.txt files are no longer in my master, and obviously, checking out BranchForIgnore won't help either.
What to do?
gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.
gitignore file prevents future untracked files from being added to the git index. In other words, any files that are currently tracked will still be tracked by git. In this tutorial, we'll explore different possibilities to remove tracked files from the git index after adding them to . gitignore.
Ignoring a previously committed file If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it.
I forgot to .gitignore a particular file, and adding it to
.gitignore
after having committed makes no difference.
Well, of course it doesn't. Ignoring is about untracked files (which means files not under version control).
I found a page on gitready which explains how to remove a file from the repository without removing it from the working tree (by using the command
git rm --cached <file>
, which works ok, except that if I then try to merge that back into another branch, the files in the working tree get deleted.
Git deletes file because you can recover it, as it was tracked in one branch.
The solution would be to commit "untracking" a file (removing file from version control) in all branches, using git cherry-pick
, or making git rm --cached <file> && git commit -a
on a separate commit branch, then merging this topic branch in all branches (and then recovering file from a tracked version).
Rename the file to a temporary name (not using git), commit the deletion plus the .gitignore file, then rename the file back to its original name.
mv wantnot.txt wantnot.txt.tmp
git rm wantnot.txt
echo wantnot.txt >.gitignore
git add .gitignore
git commit -m "remove mistakenly committed wantnot.txt"
mv wantnot.txt.tmp wantnot.txt
Your use of a separate branch for this may be unnecessarily confusing the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With