views:

1805

answers:

5

While coding i add print stataments in to files to keep track of what is 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 prints in File A but i modified File B. B is what i want to commit and A i want to go back to its old state ?

+1  A: 

man git-checkout: git checkout A

NicDumZ
A: 

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 :-)

Jay
+13  A: 

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
1800 INFORMATION
Note: the revert does revert the all commit, so that could mean a lot of "reset B", isn't it ? See last comments of http://gitready.com/intermediate/2009/03/16/rolling-back-changes-with-revert.html . (as mentioned in http://stackoverflow.com/questions/642264/undo-change-in-git-not-rewriting-history/642809#642809 , a "negative merge" could be more accurate).
VonC
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
1800 INFORMATION
"git reset A" is equivalent to "git checkout HEAD A".
Jakub Narębski
+1  A: 
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
Stefan Mai
A: 

Why can't you simply mark what changes you want to have in a commit using "git add <file>" (or even "git add --interactive", or "git gui" which has option for interactive comitting), and then use "git commit" instead of "git commit -a"?

In your situation (for your example) it would be:

prompt> git add B
prompt> git commit

Only changes to file B would be comitted, and file A would be left "dirty", i.e. with those print statements in the working area version. When you want to remove those print statements, it would be enought to use

prompt> git reset A

or

prompt> git checkout HEAD -- A

to revert to comitted version (version from HEAD, i.e. "git show HEAD:A" version).

Jakub Narębski