96

I want to automatically exclude 2 files from committing, that are part of the git repository but have some changes, that shouldn't be part of the github repo, because they have some changes that are only meaningful to my local system and not the other developers.

I added them to .git/info/exclude in the hopes git understands that he shouldn't commit changes to them:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
a.conf
b.conf
# *~

but git status still lists them as staged for committing:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   build/conf/a.conf
#   modified:   build/conf/b.conf
#

I don't want to unstage them regularly with every commit (one time I might just forget), how can I do that?

erikbstack
  • 12,878
  • 21
  • 81
  • 115
  • 2
    Why not to `.gitignore`? – maverik Jun 04 '12 at 10:25
  • 7
    You can't ignore files that you are tracking. You probably need to find another way of managing local system specific information such has having "template" files that you generate or manually edit configuration files from. – CB Bailey Jun 04 '12 at 10:31
  • 5
    @maverik: if `exclude` doesn't work, `.gitignore` won't either. The use case for `exclude` is that this information is locally while `.gitignore` could be checked in and shared. Take `exclude` for your local stuff when you don't want to blow up the `.gitignore`. Imagine a large project where every developer enters his excludes into `.gitignore`. It would become no longer managable... – eckes Jun 04 '12 at 10:48

1 Answers1

215

What you want to is to ignore changes on tracked files. This cannot achieved (as Charles Bailey says) correctly, neither with .gitignore nor with .git/info/exclude.

You'll need to use git update-index:

git update-index --assume-unchanged build/conf/a.conf
git update-index --assume-unchanged build/conf/b.conf

will achieve what you want: the files are always assumed unchanged.

If you want to track changes in these files again, use --no-assume-unchanged.

Finally, if you want to know which files are currently in the --assume-unchanged mode, ask git for

git ls-files -v | grep -e "^[hsmrck]"
Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • is this solution bound to a branch? Let´s be file1 and file 2 marked as "assume-unchanged" in branch1. Then checkout to branch2. Are these files marked as "assume-unchanged" as well? – nespapu Feb 23 '18 at 09:03
  • 1
    @Leo Unable to make file is because the file is not tracked yet. You can simply go to .git/info/exclude and add the paths you want to exclude. – Augustus Francis Sep 26 '18 at 21:05
  • Apparently maintainers of `git` do not agree with this answer: [git-update-index --assume-unchanged was never meant to ignore changes to tracked files](https://github.com/git/git/commit/936d2c9301e41a84a374b98f92777e00d321a2ea). Also see https://stackoverflow.com/a/23305143/2705757 . – AXO Mar 26 '19 at 05:56
  • 2
    @eckes Anyway to make this work for all branches? if I use the command, make changes I can not switch branches and gives me error to commit or stash them – Saber Jul 31 '19 at 00:50