简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM