0

What happens if I execute the following command when I have a local commit (i.e. a commit that I haven't pushed to the remote repository yet) that I want to revert back ?

git revert HEAD

I researched a lot and found out that -

  • "git revert $id" is used to revert back changes made in a particular commit commit ID - $id, and
  • the HEAD itself is nothing but a reference to a commit ID (of the latest commit).

But I'm unable to understand that in this scenario (when I have a local commit), the HEAD would point at my latest local commit, or the latest commit on the remote repository ?

Why I ask this?

  • I tried "git pull" from my branch, but it wouldn't happen as I had some changes in my local.
  • So, I executed "git commit", and then tried the "git pull".
  • Now, "git pull" gave me errors (probably merge-conflicts : I happened to missed out on the details of the "errors"), so I decided to revert my commit, for which I executed "git revert HEAD".
  • Now I'm confused that did this command (git revert HEAD) only revert my changes in the (local) commit I mentioned previously? or the last commit on the remote repository?
  • `HEAD` means the currently checked out commit (so it's definitely a local commit, and it _might_ be a remote commit too). `git revert ` will create a "mirror commit" with every added and removed line switched. Since you tried to pull and got merge conflicts, you are probably amidst a merge and need to resolve/abort it first. – knittl Jul 29 '15 at 15:18

1 Answers1

0

You can use git reset command.
Note that you will lose every changes (if any) in your current workspace.

$ git reset --hard HEAD~1

Also, this post is useful to understand the differences between revert and reset.

Community
  • 1
  • 1
Richard Dally
  • 1,432
  • 2
  • 21
  • 38
  • Yes, I realized that's the best way I should have done it. But now, that I have already run "git revert HEAD", I'm in a mess. Is there any way to revert this command? And just for knowledge sake, which commit do you think would have been reverted in the scenario I explained above? – Akshit Singla Jul 29 '15 at 15:44
  • @AkshitSingla, git reset --hard HEAD~1 will delete 1 commit. git revert made 1 commit. So you want to trash two commits, right ? Look at git log to be sure then trash the correct number of commits. – Richard Dally Jul 29 '15 at 16:19
  • Sounds good! Just to confirm - hope this'll work even if I've pushed the changes, right ? – Akshit Singla Jul 29 '15 at 16:52