Git undo changes in some files

162,619

Solution 1

There are three basic ways to do this depending on what you have done with the changes to the file A. If you have not yet added the changes to the index or committed them, then you just want to use the checkout command - this will change the state of the working copy to match the repository:

git checkout A

If you added it to the index already, use reset:

git reset A

If you had committed it, then you use the revert command:

# the -n means, do not commit the revert yet
git revert -n <sha1>
# now make sure we are just going to commit the revert to A
git reset B
git commit

If on the other hand, you had committed it, but the commit involved rather a lot of files that you do not also want to revert, then the above method might involve a lot of "reset B" commands. In this case, you might like to use this method:

# revert, but do not commit yet
git revert -n <sha1>
# clean all the changes from the index
git reset
# now just add A
git add A
git commit

Another method again, requires the use of the rebase -i command. This one can be useful if you have more than one commit to edit:

# use rebase -i to cherry pick the commit you want to edit
# specify the sha1 of the commit before the one you want to edit
# you get an editor with a file and a bunch of lines starting with "pick"
# change the one(s) you want to edit to "edit" and then save the file
git rebase -i <sha1>
# now you enter a loop, for each commit you set as "edit", you get to basically redo that commit from scratch
# assume we just picked the one commit with the erroneous A commit
git reset A
git commit --amend
# go back to the start of the loop
git rebase --continue

Solution 2

Source : http://git-scm.com/book/en/Git-Basics-Undoing-Things

git checkout -- modifiedfile.java


1)$ git status

you will see the modified file

2)$git checkout -- modifiedfile.java

3)$git status

Solution 3

git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index

Solution 4

man git-checkout: git checkout A

Solution 5

Yes;

git commit FILE

will commit just FILE. Then you can use

git reset --hard

to undo local changes in other files.

There may be other ways too that I don't know about...

edit: or, as NicDumZ said, git-checkout just the files you want to undo the changes on (the best solution depends on wether there are more files to commit or more files to undo :-)

Share:
162,619

Related videos on Youtube

Hamza Yerlikaya
Author by

Hamza Yerlikaya

Updated on January 12, 2020

Comments

  • Hamza Yerlikaya
    Hamza Yerlikaya over 4 years

    While coding I added print statements into some files to keep track of what was going on.

    When I am done, is it possible to revert changes in some files, but commit the file I actually worked on?

    Say I added print in file A, but I modified file B. B is what I want to commit and A, I want to be set back to its old state.

  • 1800 INFORMATION
    1800 INFORMATION almost 15 years
    Yeah, if you had lots of files that were modified as part of the commit that you do not want to revert, then another method would be required, I'll edit it to suggest one
  • Anonigan
    Anonigan almost 15 years
    "git reset A" is equivalent to "git checkout HEAD A".
  • Someone Somewhere
    Someone Somewhere over 12 years
    is there a quick and easy click within GitGUI that can revert a file to what's in the repository? using the Git bash worked... but if I can click it that would be quicker :-)
  • Jean-Philippe Caruana
    Jean-Philippe Caruana over 10 years
    thank you, you just saved my life !
  • Ahmed
    Ahmed about 9 years
    RTFM is not a good answer. git checkout A will result in an error
  • Chris
    Chris over 7 years
    git checkout -- modifiedfile.java is a nice tip.thanks
  • Bhavuk Mathur
    Bhavuk Mathur over 7 years
    Suppose I made changes in file A (state1), and the file has been modified at Master (state2). If I do git checkout A, to which state will it point now, the previous state1, or updated state2..??
  • mehov
    mehov over 7 years
    git checkout did the job, thanks!
  • Zhenhua
    Zhenhua over 6 years
    For a special case, where you 1. are using Gerrit (or other suite) for code review 2. has already committed your change 3. has already submitted for review 4. want to undo changes only in a particular file. 5. Don't want to reset and invoke a brand new review session I found the following sequence to be most convenient: 1. Get change set for the particular file git show HEAD [targe_file_name] > path/file.patch 2. Reverse the change made to the particular file git apply -R path/file.patch 3. Commit and Review git add -u git commit --amend git review