DEV Community

foadlind
foadlind

Posted on • Originally published at practicalgit.com

Make Git ignore changes to a tracked file

Sometimes you want to change a tracked file so that you can run your project locally on your machine. This is typical of configuration files and templates. But you don't want to commit these files.

One way is to discard these changes every time you are about to commit and redo them every time you pull from master. Another way is to stash them before you commit and reapply them after you pull.

But there is a better way! You can tell Git to ignore changes to this file:

$ git update-index --assume-unchanged <file-to-ignore>
Enter fullscreen mode Exit fullscreen mode

Now you can go ahead and do whatever you want in that file and it will not show up as a changed file in Git.

This will work unless that file is changed on the remote branch. In that case if you do a pull, you will get an error.

When that happens you need to tell Git to start caring about the file again, stash it, pull, apply your stashed changes, and tell Git to start ignoring the file again:

# tell Git to stop ignoring this file
$ git update-index --no-assume-unchanged <file-to-ignore>

# stash your local changes to the file
$ git stash <file-to-ignore>

# Pull from remote
$ git pull

# Apply your stashed changes and resolve any possible conflicts
$ git stash apply

# Now tell Git to ignore this file again
$ git update-index --assume-unchanged <file-to-ignore>
Enter fullscreen mode Exit fullscreen mode

You can automate this by either creating a bash script or a Git alias that does all these steps at once.

signup

Top comments (0)