[git] "git rm --cached x" vs "git reset head --? x"?

GitRef.org - Basic:

git rm will remove entries from the staging area. This is a bit different from git reset HEAD which "unstages" files. By "unstage" I mean it reverts the staging area to what was there before we started modifying things. git rm on the other hand just kicks the file off the stage entirely, so that it's not included in the next commit snapshot, thereby effectively deleting it.

By default, a git rm file will remove the file from the staging area entirely and also off your disk > (the working directory). To leave the file in the working directory, you can use git rm --cached.

But what exactly is the difference between git rm --cached asd and git reset head -- asd?

This question is related to git git-reset git-index git-rm

The answer is


git rm --cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD -- file will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting. If that change happens to be newly adding the file, then they will be equivalent.


Perhaps an example will help:

git rm --cached asd
git commit -m "the file asd is gone from the repository"

versus

git reset HEAD -- asd
git commit -m "the file asd remains in the repository"

Note that if you haven't changed anything else, the second commit won't actually do anything.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?

Examples related to git-index

Undo git update-index --assume-unchanged <file> "git rm --cached x" vs "git reset head --? x"? How to uncommit my last commit in Git What are the differences between "git commit" and "git push"?

Examples related to git-rm

Remove a folder from git tracking Staging Deleted files Why are there two ways to unstage a file in Git? How to remove multiple deleted files in Git repository "git rm --cached x" vs "git reset head --? x"? Git: list only "untracked" files (also, custom commands) Git add all files modified, deleted, and untracked? Git: How to remove file from index without deleting files from any repository How to revert a "git rm -r ."? How can I delete a file from a Git repository?