3

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?

3 Answers 3

5

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".

Sign up to request clarification or add additional context in comments.

1 Comment

That's right. The correct way to make any branch point to another commit is through reset. Here's a more in depth guide about how reset works: git-scm.com/blog/2011/07/11/reset.html
0

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.