2014-12-28 20 views
3

我想重做先前提交的更改。推送到分離頭上的主提交

Maxims-MacBook-Air:hellodebug.com maximveksler$ git log 
commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016 
Author: Maxim Veksler <[email protected]> 
Date: Sun Dec 28 09:12:17 2014 +0200 

    Imagine a Supercomputer Helping You Fix Bugs Faster 

commit 54561ed320633e72bb35a7ab668a9996e6ffca8f 
Author: Maxim Veksler <[email protected]> 
Date: Sun Dec 28 08:57:25 2014 +0200 

    highcharts tweaks 

commit d57144cbd004b3b9e893e8d50d7077634824ce9a 
Author: Genady Okrain <[email protected]> 
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 

但現在我需要推回到主人,並upsteam。這裏來的雜亂的部分...

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 [email protected]:maximveksler/hellodebug.com.git 
! [rejected]  HEAD -> master (non-fast-forward) 
error: failed to push some refs to '[email protected]: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 [email protected]{0}: checkout: moving from e9e202c4703939064e099b735ceeb6b78dc7933a to master 
e9e202c [email protected]{1}: commit: no border 
b5cb3e4 [email protected]{2}: commit: Back to not fucking it up 
54561ed [email protected]{3}: checkout: moving from master to 54561ed320633e72bb35a7ab668a9996e6ffca8f 
7f9dd75 [email protected]{4}: commit: Imagine a Supercomputer Helping You Fix Bugs Faster 
54561ed [email protected]{5}: commit: highcharts tweaks 

好的,所以他們是在野外的某個地方,但我怎麼讓他們回來?說再見還爲時過早。

幫助?

+0

[在SourceTree中投資0美元,它可以讓你看到你的本地和遠程分支達到了什麼樣的程度](http://www.sourcetreeapp.com/)。 –

+1

@ ta.speot.is將問題卸載到工具上無法替代理解實際發生的情況以及如何解決問題。 – Abizern

+0

@Abizern我建議他用SourceTree查看分支。這與'git log'的高度定製版本沒有多大區別。 –

回答

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

祝你好運。

0

由於您已經推行了承諾7f9dd753d39fd65b4272af713ef9c07a9f84f016我建議您執行git revert

一結賬你的主人,做

git revert 7f9dd753d39fd65b4272af713ef9c07a9f84f016 

這將創建對master頂一下,恢復在7f9dd753引入的變化的新的提交。

現在結帳你作爲一個新的分支所進行的修復(你可以看到在引用日誌e9e202c [email protected]{1}: commit: no border提交ID)

git checkout -b myFix e9e202c 

調整基線上mastermyFix變化。

git rebase master 

切換回master和快速轉發給重訂基期myFix分支。

git checkout master 
git merge myFix 

現在你可以刪除myFix分支和推master

git branch -D myFix 
git push origin master 
1

把你對分離的頭做出origin/master的變化(或不同的分支),嘗試:

git push origin HEAD:master 

如果你有警示,確保你瞭解最新的由來(你可以試着先拉它)。雖然如果您正在修改提交,並且您確定自己的更改是最新的,則可以強制推送(-f/--force)。不建議這樣做,除非你知道自己在做什麼(如重新綁定)。

0

您可以合併上游master分支與-s ours策略(或當您在主設備上時爲-s theirs)。所以,你不打破歷史(如git reset --hard或rebase操作),但保持工作樹。

當單個git revert不足時(例如,好的和上游分支之間有合併時),此方法特別有用。