3

我想重做以前提交的更改。

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

好的,所以他们在野外的某个地方,但我如何让他们回来?现在说再见还为时过早。

帮助?

4

4 回答 4

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.

祝你好运。

于 2014-12-28T08:13:32.917 回答
1

要将在分离的 HEAD 上所做的更改推送到 origin/master(或不同的分支),请尝试:

git push origin HEAD:master

如果您收到警告,请确保您了解 origin 的最新信息(您可以先尝试获取它)。尽管如果您正在修改提交,或者您确定您的更改是最新的,您可以使用强制 ( -f/ --force) 推送它。不建议这样做,除非您知道自己在做什么(例如变基)。

于 2015-09-17T20:42:56.460 回答
0

由于您已经推送了提交7f9dd753d39fd65b4272af713ef9c07a9f84f016,我建议您执行git revert.

首先检查你的主人并做

git revert 7f9dd753d39fd65b4272af713ef9c07a9f84f016

这会在此基础上创建一个新的提交,master以恢复7f9dd753.

现在检查您作为新分支所做的修复(您可以在 reflog 中看到提交 ID e9e202c HEAD@{1}: commit: no border

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
于 2014-12-28T12:15:37.137 回答
0

您可以将上游master分支与-s ours策略合并(或-s theirs当您在 master 上时)。因此,您不会破坏历史记录(likegit reset --hard或 rebase 操作),而是保留工作树。

这种方法在单个git revert不够用时特别有用(例如,当好分支和上游分支之间存在合并时)。

于 2015-09-17T20:52:08.463 回答