I am new in Git
and my experience is from Clearcase
.
In Clearcase
when I wanted to modify a file, I checked-out
the file to make it writable, modified it and if I was not happy with the changes I simply did an undo checkout
going back to the original version of the file.
With git
it is like black box.
I can start writing to my file (it is not read only, right?) and then I am supposed to do add
followed by commit
.
But what if I am not happy with my changes and decided not to even add
?
How do I go back to the original file removing my modifications essentially?

- 204,365
- 48
- 270
- 300

- 52,998
- 69
- 209
- 339
-
`git checkout HEAD -- path/to/file` – jthill May 12 '13 at 21:39
2 Answers
The git status
output tells you how to do so:
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
Use git checkout -- <file>
to discard changes even before add
.
If you have already add
the file and then you find it's not satisfactory,
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
Use git reset HEAD <file>
to unstage.

- 7,712
- 9
- 48
- 65
-
`git status` reports on the whole code base or the current directory? Confused on this. I tried to run it in a large code base and seemed to take forever and I killed it – Cratylus May 12 '13 at 19:21
-
@Cratylus You may add files/folders (e.g. data files, binary files, etc) to a `.gitignore` file to stop tracking them. Typically a code base should not contain too many changed files. – Yang May 12 '13 at 19:30
-
-
Yang:I stopped `git status` after waiting for ~20secs. I assumed that it scanned all the repository to see the status. May be I am thinking this in the `Clearcase` model I have in my mind and I shouldn't – Cratylus May 12 '13 at 19:42
-
@Cratylus Can you use `git status | more` to see page by page what files it's reporting? – Yang May 12 '13 at 19:43
-
-
if `git status` takes >20 secs, `git status|more` will not be any better. you seem to have quite an unoptimized repo there. – eis May 12 '13 at 20:22
May be I am thinking this in the Clearcase model I have in my mind and I shouldn't
Yes: see the main differences between ClearCase and Git here.
Check "Git Status Takes a Long Time to Complete" to see why git status takes a long time: it shouldn't exceed a few seconds at most (unless there is a pager issue).
Remember your git repo is much smaller than a ClearCase Vob. I have Vobs which are huge (several Terabytes). A git repo shouldn't exceed a few Megabytes, in order to be cloned around efficiently.
Make sure your current repo doesn't represent what was a huge ClearCase Vob, with potentially multiple projects in it.
Regarding your initial question, I like the unadd/unstage
alias defined in "Undo 'git add
' before commit".
git config --global alias.unadd 'reset HEAD --'
git config --global alias.unstage 'reset HEAD --'