[git] How to stop tracking and ignore changes to a file in Git?

I have cloned a project that includes some .csproj files. I don't need/like my local csproj files being tracked by Git (or being brought up when creating a patch), but clearly they are needed in the project.

I have added *.csproj to my LOCAL .gitignore, but the files are already in the repo.

When I type git status, it shows my changes to csproj which I am not interested in keeping track of or submitting for patches.

How do I remove the "tracking of" these files from my personal repo (but keep them in the source so I can use them) so that I don't see the changes when I do a status (or create a patch)?

Is there a correct/canonical way to handle this situation?

This question is related to git

The answer is


Lots of people advise you to use git update-index --assume-unchanged. Indeed, this may be a good solution, but only in the short run.

What you probably want to do is this: git update-index --skip-worktree.

(The third option, which you probably don't want is: git rm --cached. It will keep your local file, but will be marked as removed from the remote repository.)

Difference between the first two options?

  • assume-unchanged is to temporary allow you to hide modifications from a file. If you want to hide modifications done to a file, modify the file, then checkout another branch, you'll have to use no-assume-unchanged then probably stash modifications done.
  • skip-worktree will follow you whatever the branch you checkout, with your modifications!

Use case of assume-unchanged

It assumes this file should not be modified, and gives you a cleaner output when doing git status. But when checking out to another branch, you need to reset the flag and commit or stash changes before so. If you pull with this option activated, you'll need to solve conflicts and git won't auto merge. It actually only hides modifications (git status won't show you the flagged files).

I like to use it when I only want to stop tracking changes for a while + commit a bunch of files (git commit -a) related to the same modification.

Use case of skip-worktree

You have a setup class containing parameters (eg. including passwords) that your friends have to change accordingly to their setup.

  • 1: Create a first version of this class, fill in fields you can fill and leave others empty/null.
  • 2: Commit and push it to the remote server.
  • 3: git update-index --skip-worktree MySetupClass.java
  • 4: Update your configuration class with your own parameters.
  • 5: Go back to work on another functionnality.

The modifications you do will follow you whatever the branch. Warning: if your friends also want to modify this class, they have to have the same setup, otherwise their modifications would be pushed to the remote repository. When pulling, the remote version of the file should overwrite yours.

PS: do one or the other, but not both as you'll have undesirable side-effects. If you want to try another flag, you should disable the latter first.


If you do git update-index --assume-unchanged file.csproj, git won't check file.csproj for changes automatically: that will stop them coming up in git status whenever you change them. So you can mark all your .csproj files this way- although you'll have to manually mark any new ones that the upstream repo sends you. (If you have them in your .gitignore or .git/info/exclude, then ones you create will be ignored)

I'm not entirely sure what .csproj files are... if they're something along the lines of IDE configurations (similar to Eclipse's .eclipse and .classpath files) then I'd suggest they should simply never be source-controlled at all. On the other hand, if they're part of the build system (like Makefiles) then clearly they should--- and a way to pick up optional local changes (e.g. from a local.csproj a la config.mk) would be useful: divide the build up into global parts and local overrides.


I am assuming that you are asking how to remove ALL the files in a specific folder or the bin folder, Rather than selecting each files separately.

You can use this command:

git rm -r -f /<floder-name>\*

Make sure that you are in the parent directory of the of that directory.
This command will, recursively "delete" all the files which are in the bin/ or build/ folders. By the word delete I mean that git will pretend that those files are "deleted" and those files will not be tracked. The git really marks those files to be in delete mode.

Do make sure that you have your .gitignore ready for upcoming commits.
Documentation : git rm


To save some time the rules you add to your .gitignore can be used for removing multiple files/folders i.e.

git rm --cached app/**/*.xml

or

git rm --cached -r app/widgets/yourfolder/

e.t.c.


An almost git command-free approach was given in this answer:

To ignore certain files for every local repo:

  1. Create a file ~/.gitignore_global, e.g. by touch ~/.gitignore_global in your terminal.
  2. Run git config --global core.excludesfile ~/.gitignore_global for once.
  3. Write the file/dir paths you want to ignore into ~/.gitignore_global. e.g. modules/*.H, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.H.

To ignore certain files for a single local repo:

  1. Do the above third step for file .git/info/exclude within the repo, that is write the file/dir paths you want to ignore into .git/info/exclude. e.g. modules/*.C, which will be assumed to be in your working directory, i.e. $WORK_DIR/modules/*.C.

one line answer git update-index --assume-unchanged [path]

Use this whenever you have a file that is in central repo and also local repo.you need to make changes in that file but must not be staged/committed to the central repo. This file should not be added in .gitignore. because new changes in the file if introduced by system administrators, senior developers needs to be distributed among all local repos.

Best Example: config file for DB connections. In a central repo, you will have all the username,password, host,port with values of a production DB server. But in local dev, you should use only a local or any other development DB server(that your team has setup). In this case you wanna make changes in config file but should not be committed to central repo.

Best


Forgot your .gitignore?

If you have the entire project locally but forgot to add you git ignore and are now tracking some unnecessary files use this command to remove everything

git rm --cached -r .

make sure you are at the root of the project.

Then you can do the usual

Add

git add .

Commit

git commit -m 'removed all and added with git ignore'

Push

git push origin master

Conclusion

Hope this helps out people who have to make changes to their .gitignore or forgot it all together.

  • It removes the entire cache
  • Looks at your .gitignore
  • Adds the files you want to track
  • Pushes to your repo

The accepted answer still did not work for me

I used

git rm -r --cached .

git add .

git commit -m "fixing .gitignore"

Found the answer from here


As pointed out in other answers, the selected answer is wrong.

The answer to another question suggests that it may be skip-worktree that would be required.

git update-index --skip-worktree <file>

There are 3 options, you probably want #3

1. This will keep the local file for you, but will delete it for anyone else when they pull.

git rm --cached <file-name> or git rm -r --cached <folder-name>

2. This is for optimization, like a folder with a large number of files, e.g. SDKs that probably won't ever change. It tells git to stop checking that huge folder every time for changes, locally, since it won't have any. The assume-unchanged index will be reset and file(s) overwritten if there are upstream changes to the file/folder (when you pull).

git update-index --assume-unchanged <path-name>

3. This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

git update-index --skip-worktree <path-name>

It's important to know that git update-index will not propagate with git, and each user will have to run it independently.


The problem may be caused by the order of operation. If you modified the .gitignore first, then git rm --cached xxx,you may have to continue to encounter this problem.

Correct solution:

  1. git rm --cached xxx
  2. modified the .gitignore

Order invariant!

The .gitignore reload after modification!


To ignore any changes to all the files (of a certain type) in a directory, I had to combine some of these approaches, otherwise the files were created if they didn't previously exist.

In the below, "excludedir" is the name of the directory that I wish to not watch changes to.

First, remove any existing new files from your change tracking cache (without removing from your file system).

git status | grep "new file:" | cut  --complement -d " " -f1-4 | grep "^excludedir" | xargs git rm --cache

You can do the same with modified:. renamed: is a bit more complicated, as you'll have to look at the post -> bit for the new filename, and do the pre -> bit as described for deleted: below.

deleted: files prove a bit more complicated, as you can't seem to update-index for a file that doesn't exist on the local system

echo .deletedfiles >> .gitignore
git status | grep "deleted:" | cut  --complement -d " " -f1-4 | grep "^excludedir" > .deletedfiles
cat .deletedfiles | xargs -d '\n' touch
cat .deletedfiles | xargs -d '\n' git add -f
cat .deletedfiles | xargs -d '\n' git update-index --assume-unchanged
cat .deletedfiles | xargs -d '\n' rm

The last command in the list above will remove the files again from your file system, so feel free to omit that.

Then, block change tracking from that directory

git ls-files excludedir/ | xargs git update-index --skip-worktree
git update index --skip-worktree excludedir/

This is a two step process:

  1. Remove tracking of file/folder - but keep them on disk - using

    git rm --cached 
    

    Now they do not show up as "changed" but still show as

        untracked files in  git status -u  
    
  2. Add them to .gitignore


To tell Git not to track changes to your local file/folder (meaning git status won't detect changes to it), do:

git update-index --skip-worktree path/to/file

And to tell Git to track changes to your local version once again (so you can commit the changes), do:

git update-index --no-skip-worktree path/to/file

after search a long time , find a way do this . alias a git command in .gitconfig.like in android studio project,before checkout branch revert config file and then skip it ,after checkout branch use sed change config file to my local config. checkoutandmodifylocalproperties = !git update-index --no-skip-worktree local.properties && git checkout local.properties && git checkout $1 && git update-index --skip-worktree local.properties && sed -i '' 's/.*sdk.dir.*/sdk.dir=\\/Users\\/run\\/Library\\/Android\\/sdk/g' local.properties && :


To prevent monitoring a file by git

git update-index --assume-unchanged [file-path]

And to revert it back use

git update-index --no-assume-unchanged [file-path]

A repo to refer for similar use cases https://github.com/awslabs/git-secrets


git rm --fileName

git ls-files to make sure that the file is removed or untracked

git commit -m "UntrackChanges"

git push


I am assuming that you are trying to remove a single file from git tacking. for that I would recommend below command.

git update-index --assume-unchanged

Ex - git update-index --assume-unchanged .gitignore .idea/compiler.xml


Apply .gitignore to the present/future

This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored.

Can't use --exclude-from=.gitignore anymore :/ - Here's the updated method:

General advice: start with a clean repo - everything committed, nothing pending in working directory or index, and make a backup!

#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"

#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *

#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.

git commit -m "re-applied modified .gitignore"

#other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED

If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.