How to remove a file from the index in git?

How to remove a file from the index ( = staging area = cache) without removing it from the file system?

  • GIT: How to copy contents of one branch to other branch?
  • BFG: mapping from old (pre-cleaning) to new hashes
  • Jenkins Git integration - How to disable SSL certificate validation
  • Portable Git and GitHub: SSH Keys
  • git diff between cloned and original remote repository
  • Git: cannot open .pbxproj file error after renaming Xcode project and pulling changes
  • Git master branch has no upstream branch
  • How do I get git branch history to match master after a squash rebase?
  • Git- how to checkout a specific version (commit hash) in Teamcity?
  • How do you manage your forked reusable apps on django with git
  • How to cancel a pull request on github?
  • git: External Diff Tool for Interactive Mode
  • 3 Solutions collect form web for “How to remove a file from the index in git?”

    You want:

    git rm --cached [file]
    

    If you omit the --cached option, it will also delete it from the working tree. git rm is slightly safer than git reset, because you’ll be warned if the staged content doesn’t match either the tip of the branch or the file on disk. (If it doesn’t, you have to add --force.)

    This should unstage a <file> for you (without removing or otherwise modifying the file):

    git reset HEAD <file>
    

    Depending on your workflow, this may be the kind of thing that you need rarely enough that there’s little point in trying to figure out a command-line solution (unless you happen to be working without a graphical interface for some reason).

    Just use one of the GUI-based tools that support index management, for example:

    • git gui <– uses the Tk windowing framework — similar style to gitk
    • git cola <– a more modern-style GUI interface

    These let you move files in and out of the index by point-and-click. They even have support for selecting and moving portions of a file (individual changes) to and from the index.


    How about a different perspective: If you mess up while using one of the suggested, rather cryptic, commands:

    • git rm --cached [file]
    • git reset HEAD <file>

    …you stand a real chance of losing data — or at least making it hard to find. Unless you really need to do this with very high frequency, using a GUI tool is likely to be safer.


    Working without the index

    Based on the comments and votes, I’ve come to realize that a lot of people use the index all the time. I don’t. Here’s how:

    • Commit my entire working copy (the typical case): git commit -a
    • Commit just a few files: git commit (list of files)
    • Commit all but a few modified files: git commit -a then amend via git gui
    • Graphically review all changes to working copy: git difftool --dir-diff --tool=meld
    Git Baby is a git and github fan, let's start git clone.