Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unstage changes in Git to a specific file

Tags:

git

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?

like image 742
Alex Avatar asked Nov 16 '10 04:11

Alex


2 Answers

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.

like image 83
Cascabel Avatar answered Sep 23 '22 02:09

Cascabel


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
like image 22
cdhowie Avatar answered Sep 21 '22 02:09

cdhowie