簡體   English   中英

合並提交不會出現在 git rebase --interactive 中

[英]Merge commits don't appear in git rebase --interactive

git log顯示以下內容:

commit 1abcd[...]
Author: [...]
Date: [...]

    [Useful commit]

commit 2abcd[...]
Author: [...]
Date: [...]

    Merge branch [...] of [etc. etc.]

commit 3abcd[...]
Author: [...]
Date: [...]

    [Useful commit]

那個合並提交對我來說沒用 - 它不代表分支的有意義的狀態並且是從遠程拉取生成的,所以我有遠程歷史的真實提交 - 不需要提交來標記我拉取的事實. 我想壓縮這個合並提交。 我通常做壁球的技巧是:

git rebase --interactive HEAD~2 (或者我需要走多遠)

然后我會將它壓縮到相鄰的提交中。 有時我會這樣做,例如我進行了一次提交,意識到我錯過了一個很小的重要細節(單個文件,或者沒有更改其中一個文件中的一行),然后再進行一次提交,這基本上只是一個快速的 oops。 這樣,當我將更改推回遙控器時,一切都變得美好而干凈,並講述了一個有凝聚力的敘述。

但是,在這種情況下,當我運行git rebase ...命令時,不會出現 commit 2abcd 它似乎跳過了2abcd ,而是顯示1abcd3abcd 合並提交有什么特別之處可以防止它出現在git rebase --interactive嗎? 我可以使用什么其他技術來壓縮合並提交?

每@蛋糕的要求更新

git log --graph --oneline --decorate的輸出如下所示:

* 1abcd (useful commit)
* 2abcd (merge)
|  \ <-- from remote
|   * 3abcd (useful commit)
|   |

有用嗎?

Rebase 通常不會在沒有--preserve-merges情況下保留合並提交

好的,所以我不確定如果您嘗試使用帶有--preserve-merges的交互式 rebase --preserve-merges合並提交會發生什么......但這就是我將如何刪除您的案例中的合並提交並創建您的歷史記錄線性:

  1. 在遠程分支之上的合並提交之前重新設置所有內容。

  2. 在合並提交之后,在先前重新調整的提交之上挑選或重新調整所有內容。

如果在合並提交后只有 1 次提交

所以就命令而言,這看起來像這樣:

# Reset to commit before merge commit
git reset --hard <merge>^

# Rebase onto the remote branch
git rebase <remote>/<branch>

# Cherry-pick the last commit
git cherry-pick 1abcd 

如果您在合並提交后有 1 個以上的提交

# Leave a temporary branch at your current commit
git branch temp

# Reset to commit before merge commit
git reset --hard <merge>^

# Rebase onto the remote branch
git rebase <remote>/<branch>

# Cherry-pick the last commits using a commit range.
# The start of the range is exclusive (not included)
git cherry-pick <merge>..temp

# Alternatively to the cherry-pick above, you can instead rebase everything
# from the merge commit to the tip of the temp branch onto the other
# newly rebased commits.
#
# You can also use --preserve-merges to preserve merge commits following
# the first merge commit that you want to get rid of...but if there were
# any conflicts in those merge commits, you'll need to re-resolve them again.
git rebase --preserve-merges --onto <currentBranch> <merge> temp

# These next steps are only necessary if you did the rebase above,
# instead of using the cherry-pick range.
#
# Fast-forward your previous branch and delete temp
git checkout <previousBranch>
git merge temp
git branch -d temp

文檔

Git 提供了一種使用--rebase-merges的新方法:

在 --rebase-merges 之前,我遇到了 6 個與我的特定案例的沖突。

git rebase -i --rebase-merges origin/master

你可能會看到類似的東西:

label onto

reset 4127388 # Formatting
pick 87da5b5 feat: add something
pick 8fcdff4 feat: add ..
merge -C 80784fe onto # Merge remote-tracking branch 'origin/master' into a-branch-name
pick 3d9fec7 add unit tests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM