大师,我现在位于第10个提交。但是,在这个过程中我意识到我破坏了一些测试没有检测到的东西。
最终我回到了第5个提交,然后慢慢地重新做了每个提交的开发,并不断调整以确保它不会重新引起bug。现在我基本上又回到了第10个提交,但进行了许多更改以防止出现错误。
现在,我想使用我的更改创建第11个提交。但是当我尝试推送到主分支时,我收到以下提示:
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use
git push master HEAD:<name-of-remote-branch>
这是可以预料的。但我要如何将此推送到我的远程分支呢?
我尝试了git push origin HEAD:master
,但得到了以下提示:
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/tomhammond/sample.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
当我执行 git status
命令时,我会看到 HEAD detached from 4a74ac3
的提示信息。
git reset [commit10Hash]
取消暂存的更改,然后git stash
暂存这些更改,git reset --hard origin/nameofremotebranch
将分支重置为远程仓库的状态,git stash pop
重新应用你需要的更改,接着执行git add .
和git commit -m "Your new changes on top of origin"
。最后,你就可以无冲突地执行git push origin nameofremotebranch
了。 - scrowler