2013-03-26 94 views
9

好吧,我做了一些愚蠢的事情。我需要刪除一個提交到一個分支

  • 我分叉回購我應該做出貢獻。
  • 因此,我直接創建了一個名爲「blafile」的文件來檢查我可以提交(顯然,我 不明白分支是什麼),並提交了一條消息「check I can commit」。
  • 我推到我的github分叉回購和忘了它。
  • 我在第二天開始修復一個錯誤。
  • 我犯了我的修補程序,並推送到我的分叉回購消息「固定bug xyz」。

現在我想發出拉請求,突然間我看到我的「檢查我可以提交」提交。我寧願不喜歡那樣出現在拉請求上。 :)

我可以完全刪除該提交嗎?我可以在單次提交時發出拉取請求嗎?還是會拉取所有提交?

我知道我可以在本地git reset --hard HEAD~1(這是一個小的修復,我可以很快重做),但只修復我的本地回購,而不是我的github(分叉)回購。

回答

9

很多選擇。

最好的選擇可能是使一個新的分支,櫻桃挑選你修復成分支:

git checkout -b my-fix-branch origin/master 
git cherry-pick master 
git push -u origin my-fix-branch 

然後從my-fix-branch做pull請求GitHub上。 (假定您的工作分支名爲master,基於遠程master;根據需要更改分支名稱)。


IF沒有人拉或克隆你的叉子,可以有力地改寫歷史。做git rebase -i HEAD~2並刪除違規提交,然後刪除git push --force。這將打破任何其他回購基於您的分叉,所以做而不是如果您懷疑任何人正在使用您的回購,這樣做。

+0

正如我確信沒有人分叉我的回購(通過看叉形圖),我重寫了歷史:)謝謝! – faboolous 2013-03-26 22:41:30

3

我相信下面的命令序列應該可以工作,假設您的無效提交只是當前HEAD之前的一個提交,並且分支名稱爲master

注意:以下命令將重寫歷史記錄,如果任何人已經克隆了您的repo,則不建議這樣做。如果不是這樣的話push -f應該不是什麼大問題。

# Switch the current working tree to master branch 
git checkout master 
# Soft reset to get all the changes since HEAD~2 into the index/staging area 
git reset --soft HEAD~2 
# Remove the blafile from the staging area 
git rm --cached blafile 
# Commit the changes you actually intended to make 
git commit 
# Update the refs on the remote forcefully to push the newer commit. 
# Note that if any one else has pulled master already with your blafile 
# commit, they would be really pissed off with you now. 
git push -f origin master 
+0

'push -f'是危險的,如果有人拉你的回購。 – nneonneo 2013-03-26 22:23:32

+0

@nneonneo - 是的,已添加註釋表明。但從它的外觀來看,OP要重寫歷史,沒有創建新分支就沒有'blafile'。 – Tuxdude 2013-03-26 22:25:30

相關問題