简体   繁体   中英

Pushing new commit from detached head back to origin main on github after git reset --hard

while working on a project, I messed up and used the command git reset --hard to revert back to the latest commit(eg Commit ID: a12345) which is also the latest working version of the project. This resulted in the HEAD being detached from commit a12345.

Afterwards, I made some changes and decided to stage and commit these new changes (Commit ID: b12345). However, since my HEAD is already detached, I am unable to push these new changes in commit b12345 back to my main branch in github. I've tried using git checkout to set HEAD to be at commit b12345 but I'm still unable to push commit b12345 to github. Thus, my question is how to push my new commit from my detached head back to the main branch in github.

This image shows the workflow of my commits with "latest commit" being commit a12345 and the other commit being commit b12345:

You said:

This resulted in the HEAD being detached from commit a12345.

Doing a Git hard reset at the HEAD of your branch should not be putting you in a detached HEAD state. The only way this generally would happen is if you did a checkout of some other commit, and then started working.

Regardless of how you arrived in the detached HEAD state, you may preserve any commit you made by using:

git checkout -b branch_from_detached_head

If you want the full history of this branch, you may push it to your repository. If you don't want the full history, but perhaps say the two most recent commits, then you can cherry-pick them onto some other branch. In either case, certainly the commits you have made are not lost and can be salvaged.

First git checkout <branch> to get out of the detached head state.

Then use git reflog and git show <commit-id> to locate the orphaned commit(s) you want (if you've lost the commit IDs) and git cherry-pick <commit-id> or git cherry-pick -n <commit-id> to bring them back to life on your branch.

If you are in the situation in your screenshot, and want to move your main branch up to your current commit b12345 , there are several ways to do that.

Here is one of them:

# switch to the main branch :
git checkout main

# move forward to b12345 :
git merge --ff-only b12345

# --ff-only is optional : its advantage is to prevent accidentally creating merge
#   commits when you don't expect to ; if you see it fails, you can inspect your history
#   and choose what's the appropriate action to do (merge with a merge commit ? rebase ?
#   reset ?)
#
# in your current situation : the merge *is* a fast forward, and will work straight away

This resulted in the HEAD being detached from commit a12345.

To emphasize what @TimBiegeleisen said: git reset alone will not turn your repo in a detached head state, your repo was already in that state prior to your git reset action.

If you want to understand how you landed in your current state, you can run git reflog : it will display the list of actions you last ran on your repo, and allow you to restore a previous state if you ever need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM