Unstage changes in Git to a specific file
I've made some changes to a specific file in a repository, and committed开发者_运维知识库 those changes. All under one commit.
In that one commit, I also made changes to several other files. How do I unstage the changes I made to that one specific file and not all of the files that were changed in the commit?
Unstage isn't exactly the right word here - that refers to removing changes from the index (the staging area), with the implication that they haven't been committed.
Since your changes have been committed, what you're asking how to do is check out the version of the file from the previous commit:
git checkout HEAD^ path/to/file
HEAD
is the current commit, and HEAD^
is the first parent of HEAD
.
If you have not yet pushed the commit anywhere:
$ git checkout HEAD^ path/to/revert
This will restore the path you wanted to revert to the state it was in as of the prior commit. (It will revert any changes made in the last commit you made.)
The index will also be updated, so now you just have to amend your commit:
$ git commit --amend
Kind of stating the obvious, but it might help someone someday if they need to unstage a file and not revert a commit. From my git prompt it shows:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
精彩评论