Git: how to undo * and * revert to last branch

Uh ... I mistakenly made a rather complicated change (including a subdirectory and renaming files), not knowing what I'm doing (or what Git will do).

Now I want to undo everything that:

  • commit is completely canceled (as if it had never been done, possibly removing it from history)
  • Restore the current working directory (where .git ) to a specific branch (the latter will be done now).

I found links to git reset --soft and git reset --hard , but I have already proved to myself that I can do real damage prematurely using the command without fully understanding it. :)

I found git reset man page , but I'm still confused:

  • What is HEAD ?
  • What is the difference between HEAD and * master ?
  • In my situation (see above), do I need to use --soft , --hard or others (3 more options)?
  • Do I need to run another command (after executing git reset ) before โ€œcompleteโ€ cancellation?

UPDATE:. Having read the answer below:

  • Do I understand correctly that all I need to do in my situation is the problem is the only git reset --hard HEAD^ command?
  • How to check what is running correctly?
+7
source share
2 answers
  • HEAD is the last commit of the selected branch.
  • master is a branch (main branch, conditionally), while HEAD is a place in history for the selected branch. HEAD refers to the branch you are in.
  • git reset --soft will leave your changes in the working tree, preventing you from doing whatever you like. git reset --hard restore the working tree to the state it was in, on the commit that you reset.
  • No other command is required.

Firstly, to save the commit in case you want to check it later, create a branch:

 git checkout -b my_bad_commit 

(or alternatively git branch my_bad_commit , as pointed out by larsman.)

Then go back to master or to any branch you were in and reset:

 git checkout branch_with_bad_commit git reset --hard HEAD^ 

HEAD ^ translates into the "parent HEAD element", which you can even drain for HEAD ^^ = 2. For more on this topic, see the git community book chapter on cancel in git

+7
source
  • HEAD is the end of the current branch .
  • The difference between HEAD and master is that HEAD changes when checking a branch (or commit).
  • --soft will leave the changes around, so you can re-add / copy them or discard them by doing a git checkout on the changed files. --hard will reset the workspace to the commit state you are resetting to.
  • Not if you reset --hard . You may need git push --force for remote repositories (although if the changes you made are already on the remote side, rewriting history is strongly discouraged).
+1
source

All Articles