How to temporary ignore files in git

#dev, #git

tl;dr

Ignore:

$ git update-index --assume-unchanged file

Come back to track:

$ git update-index --no-assume-unchanged file

Once I was starting to work in a team, unfortunately, for me, they kept track of config/database.yml using git. I was the only one that needed to change it because I was using docker and docker-compose. I had just met that guys, I did not know how to say “you are doing it wrong.” So… instead of saving the world 🤔 I just edited it and kept it ignored (nothing was sent to the repository).

Hands on!

The way to temporarily ignore a file in git is easy:

$ git update-index --assume-unchanged file

In my case:

$ git update-index --assume-unchanged config/database.hml

Turn it back to git

To revert your ignore 🤔, i. e., to come backtracking the file is:

$ git update-index --no-assume-unchanged file

Once more, in my case it was:

$ git update-index --no-assume-unchanged config/database.hml

Notes

  • If you checkout the file it changes back your changes. However, it does not revert the --assume-unchanged
  • If you checkout branches the file is still going to be out of git
  • git stash save or git stash pop does not affect those file

Example

Follow this example:

$ mkdir /tmp/untouch
$ cd /tmp/untouch
$ pwd > README.md
$ git init
Initialized empty Git repository in /tmp/untouch/.git/
$ git add README.md
$ git commit -m "Add README.md"
[master (root-commit) 0168380] Add README.md
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ git status
On branch master
nothing to commit, working tree clean
$ pwd >> README.md
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
$ git update-index --assume-unchanged README.md
$ git status
On branch master
nothing to commit, working tree clean
$ cat README.md
/tmp/untouch
/tmp/untouch
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
$ git status
On branch new-branch
nothing to commit, working tree clean
$ cat README.md
/tmp/untouch
/tmp/untouch
$ git update-index --no-assume-unchanged README.md
$ git status
On branch new-branch
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
$ cat README.md
/tmp/untouch
/tmp/untouch

References