wolfv
wolfv

Reputation: 993

restore to a previous git commit and reattach the head to it

Is this how to restore to a previous commit and reattach the head to it?

git log
git checkout 4bce33d    #restore to a previous commit
git branch -f master    #create new branch at head and force branch name to master
git checkout master     #attach head to master branch

This is only on local repository, there is no remote.
Is there a better way?

Upvotes: 3

Views: 3688

Answers (3)

wolfv
wolfv

Reputation: 993

To move head-branch to previous commit:

$ git status                    #make sure current directory is committed, or lose it
$ git log --oneline --decorate  #make sure there is a ref besides HEAD branch, or lose it
$ git reset --hard 9e5e64a      #move Head-branch to specified commit

Gaston pointed out a good reference. The latest version is on http://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified#The-Role-of-Reset

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27210

If you are ahead than your required commit then you can use this method hit

git log

See how much commit you want to go back

then hit

git reset --hard HEAD~N

Will move you on N back commit. N = 1,2,3 etc... It will move you N commit back.

Upvotes: 0

Useless
Useless

Reputation: 67733

If you just want to point master at a specific commit,

$ git checkout master # only if you're not already on this branch
$ git reset --hard 4bce33d

will work. Note that this resets both the branch pointer and your index and working tree. If you want to leave your working tree alone,

$ git reset --soft 4bce33d

won't change your files. Any differences between the new HEAD and your local directories will show up as "Changes to be committed".

Upvotes: 5

Related Questions