Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to Undo commit *and* revert to last branch

Uh oh... I mistakenly committed a pretty complex change (including subdirectory and files renames) without really knowing what I am doing (or what Git would be doing).

I now want to undo everything such that:

  1. commit is completely reversed (as if it has never been done, perhaps removing it from history as well)
  2. Restore current working directory (where .git is) to a certain branch (last one will do for now).

I found references to git reset --soft and git reset --hard but I have already proven to myself that I can do real damage by prematurely using a command without fully understanding it. :)

I found the git reset man page but I am still confused as to:

  1. What is HEAD?
  2. What is the difference between HEAD and * master?
  3. In my situation (see above) do I need to use --soft, --hard or other (3 more options)?
  4. Do I need to run another command (after doing git reset) to "finalize" the reversal?

UPDATE: After reading the answer below:

  1. Do I understand correctly that all I need to do in my situation is issue a single command git reset --hard HEAD^?
  2. How do I verify that reversal was performed correctly?
like image 230
WinWin Avatar asked Jul 08 '11 12:07

WinWin


People also ask

How do I revert a last commit in a branch?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I revert back to the last pull in git?

You can use the git reset command to undo a git pull operation. The git reset command resets your repository to a particular point in its history. If you made changes to files before running git pull that you did not commit, those changes will be gone.

Can we undo last commit?

Undoing the Last CommitReset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone. Note the --soft flag: this makes sure that the changes in undone revisions are preserved.


2 Answers

  1. HEAD is the latest commit of the checked-out branch.
  2. master is a branch (the main branch, by convention) whereas HEAD is a location in history for the checked-out branch. HEAD is relative to the branch you are on.
  3. git reset --soft will leave your changes in the working tree, uncommitted for you to do whatever you like with. git reset --hard will restore the working tree to the state it was in at the commit you reset to.
  4. No other command is needed.

First, to keep the commit in case you want to inspect it later, make a branch:

git checkout -b my_bad_commit

(or alternatively do git branch my_bad_commit as mentioned in larsman's comment.)

Then return to master or whatever branch you were on and reset:

git checkout branch_with_bad_commit
git reset --hard HEAD^

HEAD^ translates to "the parent of HEAD," which you can even stack for HEAD^^ = 2 commits back. For more on this topic, check the git community book chapter on undo in git

like image 122
shelhamer Avatar answered Nov 01 '22 18:11

shelhamer


  1. HEAD is the tip of the current branch.
  2. The difference between HEAD and master is that HEAD changes when you checkout a branch (or commit).
  3. --soft will leave the changes around, so you can re-add/commit them or undo them by doing git checkout on the changed files. --hard will reset the working area to the state of the commit you are resetting to.
  4. Not if you reset --hard. You might have to git push --force to remote repos (although, if the changes you made are already on a remote, rewriting history is strongly discouraged).
like image 21
Fred Foo Avatar answered Nov 01 '22 18:11

Fred Foo