我想重做之前提交的更改。
Maxims-MacBook-Air:hellodebug.com maximveksler$ git log
commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016
Author: Maxim Veksler <maxim@vekslers.org>
Date: Sun Dec 28 09:12:17 2014 +0200
Imagine a Supercomputer Helping You Fix Bugs Faster
commit 54561ed320633e72bb35a7ab668a9996e6ffca8f
Author: Maxim Veksler <maxim@vekslers.org>
Date: Sun Dec 28 08:57:25 2014 +0200
highcharts tweaks
commit d57144cbd004b3b9e893e8d50d7077634824ce9a
Author: Genady Okrain <genady@okrain.com>
Date: Mon Dec 22 18:46:21 2014 +0200
sash api
所以我在7f9dd753d39fd65b4272af713ef9c07a9f84f016打破了它,并决定从54561ed320633e72bb35a7ab668a9996e6ffca8f
打破它Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout 54561ed320633e72bb35a7ab668a9996e6ffca8f
Note: checking out '54561ed320633e72bb35a7ab668a9996e6ffca8f'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 54561ed... highcharts tweaks
到目前为止一直很好,现在让我们来解决。
Maxims-MacBook-Air:hellodebug.com maximveksler$ git commit -a -m'Back to not fucking it up'
[detached HEAD b5cb3e4] Back to not fucking it up
但现在我需要把它推回到主人和上层。这是一个混乱的部分......
Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout HEAD
Maxims-MacBook-Air:hellodebug.com maximveksler$ git status
HEAD detached from 54561ed
nothing to commit, working directory clean
Maxims-MacBook-Air:hellodebug.com maximveksler$ git push
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use
git push origin HEAD:<name-of-remote-branch>
Maxims-MacBook-Air:hellodebug.com maximveksler$ git push origin HEAD:master
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.
To git@github.com:maximveksler/hellodebug.com.git
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:maximveksler/hellodebug.com.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.
是的......所以现在我被卡住了。让我们尝试一些更不稳定的东西
Maxims-MacBook-Air:hellodebug.com maximveksler$ git up
Fetching origin
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
You're not currently on a branch. I'm exiting in case you're in the middle of something.
git up不跟我们说话,这太酷了。反正我也不需要它。
Maxims-MacBook-Air:hellodebug.com maximveksler$ git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
e9e202c no border
b5cb3e4 Back to not fucking it up
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch new_branch_name e9e202c
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
什么?没有!回来承诺,我爱你。
Maxims-MacBook-Air:hellodebug.com maximveksler$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
忘记!永远?我希望不是..
Maxims-MacBook-Air:hellodebug.com maximveksler$ git reflog
7f9dd75 HEAD@{0}: checkout: moving from e9e202c4703939064e099b735ceeb6b78dc7933a to master
e9e202c HEAD@{1}: commit: no border
b5cb3e4 HEAD@{2}: commit: Back to not fucking it up
54561ed HEAD@{3}: checkout: moving from master to 54561ed320633e72bb35a7ab668a9996e6ffca8f
7f9dd75 HEAD@{4}: commit: Imagine a Supercomputer Helping You Fix Bugs Faster
54561ed HEAD@{5}: commit: highcharts tweaks
好的,所以他们在野外的某个地方,但是我怎么让他们回来?现在说再见还为时过早。
HELP?
答案 0 :(得分:2)
如果修复是合理的,我会再做一次,但是以不同的方式,从你的错误提交开始
git reset --hard HEAD^ # brings your entire master branch and working copy back to where it worked
git reset origin/master # brings your HEAD pointer back to origin/master but leaves your working copy with the working code
# fix code
git commit -a -m'try to not fuck it up'
git push
另一种方法是恢复错误提交,清楚地表明它是坏的,然后在那之后进行修复
git revert HEAD^
# fix code
git commit -a -m'better code this time'
git push # pushes two commits, the revert and the new code
如果你想保留已经在分离头上的代码,你应该能够cherry-pick
在上面的两个流程中,而不是修复代码,只需git cherry-pick b5cb3e4
。
答案 1 :(得分:1)
要将对分离的HEAD所做的更改推送到origin / master(或不同的分支),请尝试:
git push origin HEAD:master
如果您收到警告,请确保您与原产地保持同步(您可以尝试先将其拉出来)。虽然如果您要修改提交,或者您确定自己的更改是最新的,则可以强行推送(-f
/ --force
)。除非你知道自己在做什么(比如变基),否则不建议这样做。
答案 2 :(得分:0)
由于您已经推送了提交7f9dd753d39fd65b4272af713ef9c07a9f84f016
,我建议您执行git revert
。
首先结帐你的主人并做
git revert 7f9dd753d39fd65b4272af713ef9c07a9f84f016
这会在master
之上创建一个新的提交,以恢复7f9dd753
中引入的更改。
现在检查您作为新分支所做的修复(您可以在reflog e9e202c HEAD@{1}: commit: no border
中看到提交ID)
git checkout -b myFix e9e202c
重新调整myFix
上的master
更改。
git rebase master
切换回master
并将其快进到重新定位的myFix
分支。
git checkout master
git merge myFix
现在您可以删除myFix
分支并推送master
。
git branch -D myFix
git push origin master
答案 3 :(得分:0)
您可以将上游master
分支与-s ours
策略合并(或者当您在主人时使用-s theirs
)。因此,您不会破坏历史记录(例如git reset --hard
或rebase操作),而是保留工作树。
当单个git revert
不够时(例如,在良好和上游分支之间存在合并时),此方法特别有用。