Git reset - Git How to Undo commit and revert to last branch

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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:

    • commit is completely reversed (as if
    it has never been done, perhaps removing it from history as well)
    • Restore current working directory

    (where .git is) to a certain

    branch (last one will do for now).

http://stackoverflow.com/questions/2923055/how-to-move-a-branch-backwards-in-git/2923142#2923142 http://stackoverflow.com/questions/2923055/how-to-move-a-branch-backwards-in-git/2923142#2923142

http://git-scm.com/docs/git-reset http://git-scm.com/docs/git-reset

    • What is HEAD?
    • What is the difference between
    HEAD and * master?
    • In my situation (see above) do I

    need to use --soft, --hard or

    other (3 more options)?
    • Do I need to run another command

    (after doing git reset) to

    "finalize" the reversal?
  UPDATE:    After reading the answer below:
    • Do I understand correctly that all I
    need to do in my situation is issue a single command git reset --hard HEAD^?
    • How do I verify that reversal was
    performed correctly?

Answers

    • HEAD is the latest commit of the checked-out branch.
    • 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.
    • 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.
    • 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^

http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/6624738/git-how-to-undo-commit-and-revert-to-last-branch

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils