untrack files in git

Today I learned how to untrack files that have already been added to a git repository. The following command will stop tracking but keep the file there intact. This is good for directories that have auto generated files every so often so everytime you do a git commit, you don’t have to commit a new copy of these files.

git rm --cached filename

The rm made me nervous because I didn’t want to accidentally remove my file ( made a backup before I did it).  Good thing it all worked out and now git doesn’t have to do any unnecessary tracking.

== update ==

Just likes James mentions in the comments, you can use .gitignore as your next step.

== update 7/7/2011 ==

As Colin and Jonathan mention in the comments,

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

seems like a much better solution than rm –cached in a collaborative environment. My use cases have always been working on my own stuff  by myself so I can’t argue against that point. This was a new command to me so I messed around with it for a little bit with a test repository and noticed some differences:

  • I can still checkout the file from the repository since it’s only ignored.
  • git update-index --no-assume-unchanged filename will bring it back into the mix to be tracked.
  • I couldn’t find a way to see what files were “assumed-unchanged” through git or in the .git folder.

Your mileage may vary, but I think I will start using the git update-index –assume-unchanged in the future. Thanks for all the comments and help.

This entry was posted in software and tagged , , . Bookmark the permalink.

46 Responses to untrack files in git

  1. Colin says:

    Just what I’ve been lookin for. Thanks

  2. Leandro says:

    Thank you!
    You saved me minutes!

  3. Stefan says:

    Thanks for the google friendly title 🙂

  4. James says:

    Very helpful, I’ve been wondering how to do that for a while! thank you

  5. Lin says:

    Thanks a lot 🙂

  6. Yogsototh says:

    Thanks, simple, useful and easier googling than to search the documentation.

  7. Lifesaver, thanks a lot.

  8. Nice! But also if you don’t want to commit autmatically generated directories or files, you could just put them in the .gitignore file. That way you don’t always have to remove them. Unless the naming pattern for your autmatic directories are unpredictable.

  9. Ryan says:

    Funny you mention it James, I’ve had a post about that in my drafts folder for awhile now.

  10. git update-index –assume-unchanged pathtoyourfile

    This command may look less as a workaround.

  11. bauer says:

    Thank You very much for this

  12. mr.wayn says:

    If you do that and then push to a bare and then pull it from bare to another repository. git delete files from that another repository!

  13. Pranil says:

    Hey I’ve got the same problem as mr.wayn, deletes the files from the other repository. Is there a way around this?

  14. Patrick says:

    Take it one step further…place a ‘.gitignore’ file within a directory (example: images/useruploads) and ignore the directory EXCEPT for the .gitignore placed within that directory. This way, when someone pulls your repo, it will create the directory for them but not include all of the files along side.

    The idea is, there must be at least one file within the directory in order for GIT to create the directory.

  15. Michal says:

    Just wrote into URL “git untrack files” thats cool request – response practice 😀

  16. RyanJM says:

    Thank you! Exactly what I needed. I was late in adding my .gitignore file so there were files there I didn’t want. This has been corrected.

  17. Michal says:

    Iam using this again and again, always missing some .gitignore rules 🙂

  18. Hi, I had to untrack not only one single file but a whole directory. That worked for me:
    git rm -r --cached dir_to_be_untracked

  19. Thanks for this! I stopped git rm-ing files because I didn’t know how to untrack them but save the file. Now I live worry-free. My psychiatrist says I might pull through.

  20. Magnus says:

    Or simply edit the .gitignore file. Adding the files or directories you don’t want to track.

  21. Tagnu says:

    Thank you. Even though –cached option is discussed in the man page for git rm, it was confusing for me as beginner. Your post makes it clear.

    Thank you again.

  22. I think more emphasis should be placed on using:

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

    per Jonathan Métillon’s comment. It’s MUCH nicer when you’re working collaboratively. +1!

    git rm --cached is “correct” when you want to be the only one who still has the file. Other people have pointed this out, I’m just pointing it out again.

  23. AndrewJohnson says:

    When I do this for a file locally and then push it to a shed repository and someone else pulls it down is the assume-unchanged entry there for them as well?

  24. Michael Härtl says:

    I tried the git update-index. It does nothing for me. When i have local changes in the file, git status doesn’t show them. But when i try to switch branches, it will not let me due to local changes in that file, that might be overwritten.

    Can it be a problem that i added this file to .gitignore?

  25. Antônio Carlos says:

    Thanks!!! =D

  26. grena says:

    Thanks 🙂

  27. Alex Mercer says:

    The way to see “assumed-unchanged” files is:
    git ls-files -v | grep "^[[:lower:]]"

  28. Mike says:

    Just want to reiterate, for those that missed it, including a file in .gitignore that is already in the index does *not* cause it to be ignored without extra steps.

  29. Rinku says:

    Thanks this was very helpful.

  30. Code says:

    Excellent. Thank you!

  31. kuyseng says:

    It’s helpful. Thank 😉
    put these .gitconfig to save it more seconds!

    [alias]
    track = update-index --no-assume-unchanged
    untrack = update-index --assume-unchanged
    ls-untrack = ls-files -v | grep "^[[:lower:]]"

  32. Leszek Wronski says:

    Thanks a lot!

  33. Rolfen says:

    Thanks!
    I like this much better than the “magic” answers that I found on stackoverflow, which require to execute a sequence of commands, without explaining what they do. I hate this aspect of open source communities… you ask a question and people would answer “open the terminal and type this and this and this and this” and consider it a complete answer.
    Here I know what I’m doing. Thanks again.

  34. Mustafa says:

    Thanks so much

  35. You can list files that are assumed unchanged with a verbose ls-files git ls-files -v will begin with a lowercase h if they are assumed. I alias assumed to git ls-files -v | grep ^h | cut -c 3- and then git assumed will give me a list of every file I assume is unchanged.

  36. amartinez says:

    genial! la mejor solución de la red 😉

  37. Luca says:

    good post! Thank you

  38. Timothy Bunch says:

    This was exactly what I needed. Over 5 years old and still relevant! Thanks.

  39. Michiel Bakker says:

    Thanks!

  40. Zionmaster says:

    Correction for .gitconfig to list files ‘assume-unchanged’

    [alias]
    ...
    ls-untrack = !sh -c 'git ls-files -v | grep "^[[:lower:]]"'

  41. How do you untrack a folder and all subfolders and files. Thanks!

  42. Rolfen says:

    I prefer rm cached, then keeping things in the index and assuming they don’t change, effectively silently keeping an outdated copy.

  43. Jack says:

    Great! I have was not getting the .gitignore file to ignore a config file with passwords. This worked perfect.

  44. semja says:

    You’re the man. Was accidentally deleting files…luckily I tested on ones I didn’t care about.

  45. khunzohn says:

    thanks so much. I’ve been searching for this solution for weeks.

  46. nat says:

    Regarding this use of `update-index –assume-unchanged` it’s worth noting that git makes no explicit promise not to check that the specified files are unchanged; it seems this command was a workaround to expedite git use in slow OS environments.

    See http://stackoverflow.com/questions/23097368/git-ignore-vs-exclude-vs-assume-unchanged

Leave a Reply

Your email address will not be published. Required fields are marked *