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.
This question is related to
git
file
version-control
undo
revert
man git-checkout: git checkout 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 :-)
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
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
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).
Source: Stackoverflow.com