551

I have setup a local git on my machine. When I initialized git, I added pre-compiled libs and binaries. However, now during my development I don't want to check in those files intermittently. I dont want to remove these files from repo. Is there any way to not keep a track of these files till I complete my development. (I think I can not use .gitignore as it works only for those files which are not in git. I want to temporarily disable tracking of files.)

1615903
  • 32,635
  • 12
  • 70
  • 99
agent.smith
  • 9,134
  • 9
  • 37
  • 49
  • related http://stackoverflow.com/q/936249/11343 – CharlesB Mar 10 '12 at 13:23
  • Possible duplicate of [git: can i commit a file and ignore the content changes?](https://stackoverflow.com/questions/3319479/git-can-i-commit-a-file-and-ignore-the-content-changes) – 1615903 Jun 30 '17 at 05:46

10 Answers10

862

git update-index should do what you want

This will tell git you want to start ignoring the changes to the file
git update-index --assume-unchanged path/to/file

When you want to start keeping track again
git update-index --no-assume-unchanged path/to/file

Github Documentation: update-index

Kristian
  • 21,204
  • 19
  • 101
  • 176
Andy
  • 44,610
  • 13
  • 70
  • 69
  • 4
    Finally a command that actually works for my accidentally-committed-dev-file, thanks :) – Richard de Wit Oct 14 '14 at 13:05
  • 17
    Why not git rm -r --cached ? – Dr. Ehsan Ali Aug 24 '15 at 05:49
  • 28
    @Ehsan it seems `git rm --cached` will lead to a deletion of the local copies on other machines, where the same branch is checked out, on their next pull. See http://www.arlocarreon.com/blog/git/untrack-files-in-git-repos-without-deleting-them/ and especially the discussion below it. – mit Aug 30 '15 at 11:12
  • That wouldn't be an issue if the file was a generated file produced by a build though, would it? I have a situation where a `.chw` file (some sort of index for a windows help CHM file) was being tracked, I could presumably `git rm -r --cached Help\foo.chw`, right? Although that might nuke the file on another build machine, it would be regenerated on the next build. – rossmcm Sep 09 '15 at 04:18
  • 3
    Not working for me, when I make changes to the file it's still added to included changes. I could exclude, but then at some point it'll probably be added in accidentally. – MrFox Dec 08 '15 at 16:23
  • 12
    assume-unchanged wasn't meant for this purpose. Might want to read this [thread](http://stackoverflow.com/questions/23097368/git-ignore-vs-exclude-vs-assume-unchanged) – Appy Jun 06 '16 at 21:44
  • 1
    What is the outcome if another dev modifies + commits their version of the file, pushes it to remote repository and then the dev with "--assume-unchanged" flag applied on the file pulls those changes? Does the file get updated with latest changes from remote, remains as modified or creates a conflict since they now differ? – dchayka Jul 12 '17 at 18:35
  • @Appy so should .git/info/exclude be used instead of using this as an "ignore mechanism"? – cjsimon Mar 27 '18 at 23:45
  • In case you want to untrack a directory : https://stackoverflow.com/questions/12288212/git-update-index-assume-unchanged-on-directory – Kira Sep 24 '18 at 14:29
  • `assume-unchanged` makes Guthub Desktop (git UI app) unstable (latest version as of now). – Md. A. Apu Mar 30 '21 at 10:37
348

you could keep your files untracked after

git rm -r --cached <file>

add your files with

git add -u

them push or do whatever you want.

cajuuh
  • 3,627
  • 1
  • 11
  • 13
  • 25
    That is actually the right answer according to: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository – Dr. Ehsan Ali Aug 24 '15 at 05:49
  • 1
    `git rm --cached .DS_Store` then it prints `rm '../.DS_Store'` And the local file is deleted??! (`git version 2.6.4 (Apple Git-63)`) – Weishi Z May 26 '16 at 00:54
  • Got the answer here http://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git – Weishi Z May 26 '16 at 03:11
  • What does the `-r` option do? Does the whole command unstages the change and remove the file also from the repo or the folder structure ? Is it a recursive option ? – giannis christofakis Feb 15 '18 at 15:30
  • the -r flag recursivelly unstages and remove from the index; The work tree is kept unchanged. – cajuuh Feb 19 '18 at 22:38
  • 10
    This isn't the correct answer because this will remove the file in source. The OP wants to ignore local changes of that file, not remove tracking for it altogether – cjsimon Mar 27 '18 at 23:35
  • i can't remove the files from source please show me how. – cajuuh Mar 29 '18 at 19:47
  • 6
    WARNING: it will remove the files you want untrack as well. Solution is to copy all the files somewhere, and copy them back after the commit. – psad May 10 '20 at 22:08
117
git rm --cached

However, you shouldn't be committing compiled binaries and external dependancies in the first place. Use a tool like Bundler to pull those in instead.

Tekkub
  • 30,739
  • 2
  • 30
  • 20
63

Use following command to untrack files

git rm --cached <file path>
Dinesh Vaitage
  • 2,983
  • 19
  • 16
12

Not an answer to the original question. But this might help someone.

To see the changes you have done (know which files are marked as --assume-unchanged)

git ls-files -v

The resulted list of files will have a prefix with one character (ex : H or h) If it is a lowercase (i.e. h) then the file was marked --assume-unchanged

prime
  • 14,464
  • 14
  • 99
  • 131
  • 3
    So use `git ls-files -v | grep "^h "` to see just the `--assume-unchanged` ones. – ZX9 Oct 28 '20 at 15:42
  • 1
    If you need something more Windows-friendly: `powershell "git ls-files -v |? {$_ -cmatch '^h '}"` ought to do the trick. – ZX9 Oct 28 '20 at 15:50
8

The git-book mentions this in section 2.4: "Undoing Things". Basically, what you have to do is reset the state of the index for some files back to the HEAD state, that is to the state of the latest checkout (or commit). This undoes staging the file to the current index. The command for this task is git reset.[1]

So, the command you have to execute is:

git reset HEAD /path/to/file

The newer versions of git (I believe since 1.6 or so) gives this command (and many more) as a tip when executing git status. These versions are very user-friendly. A personal tip: if you are only staging a few files, use git add -i. This will launch the interactive staging tool, which makes thing particularly easy. Also, I highly recommend reading the book, as it is very explicit on the use of git in practical situations.

[1] http://www.git-scm.com/book

Eric Spreen
  • 341
  • 3
  • 7
5

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

You can use this command:

git rm -r -f /build\*

Make sure that you are in the parent directory of the build 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

Vraj Pandya
  • 591
  • 1
  • 9
  • 13
4

An alternative to assume-unchanged is skip-worktree. The latter has a different meaning, something like "Git should not track this file. Developers can, and are encouraged, to make local changes."

In your situation where you do not wish to track changes to (typically large) build files, assume-unchanged is a good choice.

In the situation where the file should have default contents and the developer is free to modify the file locally, but should not check their local changes back to the remote repo, skip-worktree is a better choice.

Another elegant option is to have a default file in the repo. Say the filename is BuildConfig.Default.cfg. The developer is expected to rename this locally to BuildConfig.cfg and they can make whatever local changes they need. Now add BuildConfig.cfg to .gitignore so the file is untracked.

See this question which has some nice background information in the accepted answer.

AlainD
  • 5,413
  • 6
  • 45
  • 99
1

To remove all Untrack files. Try this terminal command

 git clean -fdx
cokeman19
  • 2,405
  • 1
  • 25
  • 40
divya d
  • 169
  • 7
0

Run

git rm -r --cached file_to_git_ignore

Then go to your gitignore file and add path_to_file_to_git_ignore

Good luck

NMukama
  • 1,114
  • 9
  • 10