'git revert' Is Not Equivalent To 'svn revert'
I just learned that if you have some changes in your working tree that you want to get rid of, you don't type 'git revert' like you might guess. No, that's what cvs, subversion, mercurial, and bazaar (to name a few) use revert to mean, but not git. With git, revert is used to undo actual commits. Thankfully, you can undo your revert with another 'git revert', I just learned. So let me repeat to be clear, if you have changes to your working files that you want to abandon, DO NOT do this:
git revert HEAD
That will undo your last commit. Do this instead:
git reset --hard HEAD
I'm glad I have that straightened out now. I'm wondering if /etc was really a good place for me to start out playing with git.
UPDATE: Nearly two years later and I'm still getting comments on this. I'm glad I've been able to help people out this way. The discussion in the comments is good, and one thing I'd like to point out is that I now always use and recommend:
git checkout filename
(as recommended by Anonymous and others below) instead of git reset
. I think the git stash
trick from Nicolas is pretty cool too.
Comments
I also note that CVS doesn't have a revert command. Getting rid of some changes was accomplished by removing the file ("rm FILE") and then updating it ("cvs up FILE").
When I do git checkout -f file I get this message I can't understand:
git checkout: updating paths is incompatible with switching branches/forcing
Did you intend to checkout 'file' which can not be resolved as commit?
What does that mean? I've resigned to doing rm file; git checkout file in the mean time, but this seems like a kludge since the git-checkout manpage clearly states the the -f option "is used to throw away local changes."
Thanks.
(Also, you do not need to remove a file first before using checkout)
When [paths] are given, this command does not switch branches. It updates the named paths in the working tree from the index file (i.e. it runs git-checkout-index -f -u), or from a named commit. In this case, the -f and -b options are meaningless and giving either of them results in an error.
The -R creates a reverse patch, if you want to see what would be undone or keep track of what you undid (without the "| patch").
git stash drop
git checkout 4524b -- sample.txt
*The part of 4524b, is the version of the file which the content you want
It's better.
git revert, git reset reset --hard with or without its head on all _do not_ do this.
So what command do I need for this?
Antoine
Sounds like a partial misunderstanding of git. The concept of a 'central server' is very very vague in git. You can use it that way, but every 'working copy' is a full repository, and could be used as a new source.
If you really want to blow away everything you've done locally, and just get what the 'server' has, delete your directory, and run another 'git clone' of the server. Otherwise, a 'git reset --hard' takes you back to the last commit on your local setup. Or, your can pick any commit in your history, and create a new branch from there, using 'git checkout -b '
http://stackoverflow.com/questions/1146973/how-to-revert-all-local-changes-in-a-git-managed-project-to-previous-state