How to make git forget a tracked file that is in gitignore
Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.
Imagine a situation in which a particular file (or folder) doesn’t want to be tracked in Git.
You can put this file in the gitignore file. But still, it will be tracked because Git started to track this file before you put this in the gitignore file.
So how to make Git forget about a tracked file?
Put that file in the gitignore file
Then,
git rm --cached <file_name>
This command will remove the file from the index. The index is something Git uses to track file changes.
There will be some untracked changes which point that you have removed a file from the index. Commit to that change and push it.
Note that this change will affect other collaborators as well when they take a pull
Another point to note is that the file won’t be deleted from your local file system.
As the file is already in the gitignore file, Git will comfortably ignore all the changes from this file forever.
Using update-index
The first method is straightforward.
There is one more way we can ask Git to not track file changes without putting that file in the gitignore file
When to use this method?
Assume that you have a config file in your repo. You have to make some changes in that file every time if you want to run the app locally.
So whenever you make a change, Git will track those changes and you don’t want to push those changes accidentally to the remote server.
In this situation, you can ask Git not to track any changes on this file forever using the below command
git update-index --skip-worktree <file_name>
The above command comes with some catches as well. It may not be very straightforward.
Some of the key points to note before using this
If the file is not often changed in the remote you can use this command. And remember this update in the index will affect only your machine, not your collaborators.
If the file is changed in the remote server and locally, then if you take a pull Git won’t allow you and will abort with an error
In this case, the skip_context.txt file is the one I have updated the index
In the above image, you can see there are no changes to commit and when I’m trying to take a pull Git is throwing an error saying a particular file is changed.
In this case, you have to update the index again usinggit update-index --no-skip-worktree <file_name>
And take a pull.
This is not very straightforward and has some additional work to do.If you have made some change in the file (which the index is updated) or in some other files in the repo and trying to checkout to another branch, again Git won’t allow you and abort it with a change
In the above image, you can see I’m trying to checkout to another branch after making some changes in the file (which has the index updated) but Git is throwing an error
In this case, as well, you have to update the index again and move to a different branchIf you have made some changes in the file (which the index is updated) and there are no changes in this file remotely, then you are good to go.
The only problem is when you have made some changes and the remote is also having some changes, then you have to do some additional work
To conclude,
Update the index of the file when you feel
The file won’t get updated very frequently
You have to make some changes in the file very frequently in your local
You don’t want to accidentally commit the changes of the file to the remote
You can also list the files or folders you have updated the index using
git ls-files -v . | grep ^S
Put this command in the alias
Still, when you are checking out to another branch, you have to do additional work in updating the index again and checking out.
I have made a bash script here that will update the index, stash them all, check out to the branch specified, and then unstash all the files and update the index again.
Put this bash script in the alias and you can use it with much ease.
Hope you have learned something from this.
That’s all for today.
See you next week :)