Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: reset --hard now I'm stuck in the past

Tags:

git

I was messing about with reset after reading stuff in the Pro Git book.

I basically ended up doing a reset --hard to a revision 12 commits previous.

I can't seem to get back to the present, or the latest commit. I've tried reset using ORIG_HEAD and even feeding it in the sha1 of the revision to go forward to.

Running git status I get: Your branch is behind by 12 commits and can be fast-forwarded.

How do I move HEAD back to the latest commit?

like image 438
Gabe Avatar asked Nov 21 '11 14:11

Gabe


People also ask

How do I get my changes back after resetting git hard?

Hard reset explained. In this case, you can restore the file using either git checkout or git reflog . You can find the hash-ID of the previous commit from the command: git log . In case you don't have the hash ID, you can use the command git reflog .

Can I undo a git reset hard?

To undo Git reset with the –hard flag, open the Git terminal and move to the Git directory. Then, list the content of the current repository. Open and update the required file. After that, commit changes, check log history, and run the “$ git reset –hard <commit-ref>” to undo the Git reset.

What after git reset hard?

Using the git reset --hard option resets the current branch tip, and also deletes any changes in the working directory and staging area. Therefore, it resets index entries to the state of the specified commit.

Does git reset hard affect remote?

A hard reset can be done if you're the only one using the local and remote repository, or if the reset removes commits that have not yet been pushed to a shared remote. In this case, you're the only one affected by the hard reset, so it's relatively safe to do.


3 Answers

Use the reflog to find out where you want to go. You can get it using git reflog and then just reset to the appropriate commit. Assuming you haven't done anything since you did the reset,

git reset --hard 'HEAD@{1}'

should do it.

like image 61
Noufal Ibrahim Avatar answered Oct 31 '22 21:10

Noufal Ibrahim


It seems as though you've already pushed the 12 commits you reset. If that's the case, then

git merge --ff-only REMOTE/BRANCH_NAME

should work where REMOTE is the name of the remote (commonly origin) and BRANCH_NAME is the name of your current branch.

like image 23
Michael Mior Avatar answered Oct 31 '22 22:10

Michael Mior


Another way (beside reflog) would be to use the fact that your branch seems to be referenced on the remotes namespace side, as a remote branch, which is why you see:

Your branch is behind by 12 commits and can be fast-forwarded.

A simple

git merge origin/yourBranch

should be enough to fast-forward the HEAD of your local branch back to where your remote branch was.

like image 3
VonC Avatar answered Oct 31 '22 22:10

VonC