git cherry-pick說:“…38c74d是合併,但未提供-m選項”

本文翻譯自:git cherry-pick says “…38c74d is a merge but no -m option was given”

I made some changes in my master branch and want to bring those upstream. 我在master分支中進行了一些更改,並希望將其引入上游。 when I cherry-pick the following commits however I get stuck on fd9f578 where git says: 當我選擇以下提交時,但是我陷入了git說的fd9f578上:

$ git cherry-pick fd9f578
fatal: Commit fd9f57850f6b94b7906e5bbe51a0d75bf638c74d is a merge but no -m option was given.

What is git trying to tell me and is cherry-pick the right thing to be using here? git試圖告訴我什麼,在這裏選擇正確的選擇是正確的選擇嗎? The master branch does include changes to files which have been modified in the upstream branch, so I'm sure there will be some merge conflicts but those aren't too bad to straighten out. master分支確實包含對文件的更改,這些更改已在上游分支中進行了修改,因此我敢肯定會有一些合併衝突,但是合併衝突還算可以解決。 I know which changes are needed where. 我知道哪些地方需要更改。

These are the commits I want to bring upstream. 這些是我想帶入上游的提交。

e7d4cff added some comments...
23e6d2a moved static strings...
44cc65a incorporated test ...
40b83d5 whoops delete whitspace...
24f8a50 implemented global.c...
43651c3 cleaned up ...
068b2fe cleaned up version.c ...
fd9f578 Merge branch 'master' of ssh://extgit/git/sessions_common
4172caa cleaned up comments in sessions.c ...

#1樓

參考:https://stackoom.com/question/cixh/git-cherry-pick說-c-d是合併-但未提供-m選項


#2樓

@Borealid's answer is correct, but suppose that you don't care about preserving the exact merging history of a branch and just want to cherry-pick a linearized version of it. @Borealid的答案是正確的,但假設您不關心保留分支的確切合並歷史,只想選擇線性化的分支即可。 Here's an easy and safe way to do that: 這是一種簡單安全的方法:

Starting state: you are on branch X , and you want to cherry-pick the commits Y..Z . 起始狀態:您在分支X ,並且想要選擇提交Y..Z

  1. git checkout -b tempZ Z
  2. git rebase Y
  3. git checkout -b newX X
  4. git cherry-pick Y..tempZ
  5. (optional) git branch -D tempZ (可選) git branch -D tempZ

What this does is to create a branch tempZ based on Z , but with the history from Y onward linearized, and then cherry-pick that onto a copy of X called newX . 這是基於Z創建分支tempZ ,但是將Y以後的歷史線性化,然後將其櫻桃拾取到名爲newXX副本上。 (It's safer to do this on a new branch rather than to mutate X .) Of course there might be conflicts in step 4, which you'll have to resolve in the usual way ( cherry-pick works very much like rebase in that respect). (在新分支上執行此操作比對X進行突變更安全。)當然,在第4步中可能會發生衝突,您必須以通常的方式解決(在這方面, cherry-pick工作原理與rebase非常相似) )。 Finally it deletes the temporary tempZ branch. 最後,它刪除臨時的tempZ分支。

If step 2 gives the message "Current branch tempZ is up to date", then Y..Z was already linear, so just ignore that message and proceed with steps 3 onward. 如果步驟2給出消息“當前分支tempZ是最新的”,則Y..Z已經是線性的,因此只需忽略該消息,然後繼續執行步驟3。

Then review newX and see whether that did what you wanted. 然後查看newX ,看看是否滿足您的要求。

(Note: this is not the same as a simple git rebase X when on branch Z , because it doesn't depend in any way on the relationship between X and Y ; there may be commits between the common ancestor and Y that you didn't want.) (注意:這與在分支Z上的簡單git rebase X ,因爲它不以任何方式取決於XY之間的關係;在共同祖先和Y之間可能存在您未提交的提交”不想。)


#3樓

Simplify. 簡化。 Cherry-pick the commits. 櫻桃挑選提交。 Don't cherry-pick the merge. 不要挑剔合併。

Here's a rewrite of the accepted answer that ideally clarifies the advantages/risks of possible approaches: 這是對已接受答案的重寫,可以理想地闡明可能方法的優點/風險:

You're trying to cherry pick fd9f578, which was a merge with two parents. 您正在嘗試挑選fd9f578,它與兩個父母合併。

Instead of cherry-picking a merge, the simplest thing is to cherry pick the commit(s) you actually want from each branch in the merge. 除了簡單地選擇合併,最簡單的方法是從合併中的每個分支中挑選您實際想要的提交。

Since you've already merged, it's likely all your desired commits are in your list. 由於您已經合併,因此可能所有需要的提交都在列表中。 Cherry-pick them directly and you don't need to mess with the merge commit. 直接選擇它們,您無需弄亂合併提交。

explanation 說明

The way a cherry-pick works is by taking the diff that a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying the changeset to your current branch. 櫻桃選擇的工作方式是通過獲取變更集表示的差異(該點上的工作樹與其父級工作樹之間的差異),然後將變更集應用於當前分支。

If a commit has two or more parents, as is the case with a merge, that commit also represents two or more diffs. 如果一個提交有兩個或多個父級(如合併情況),則該提交也代表兩個或多個差異。 The error occurs because of the uncertainty over which diff should apply. 發生錯誤是由於不確定應應用差異的原因。

alternatives 備擇方案

If you determine you need to include the merge vs cherry-picking the related commits, you have two options: 如果確定需要包括合併與挑選相關提交的比較,則有兩種選擇:

  1. (More complicated and obscure; also discards history) you can indicate which parent should apply. (更加複雜和晦澀;也將丟棄歷史記錄),您可以指明應採用哪個家長。

    • Use the -m option to do so. 使用-m選項來執行此操作。 For example, git cherry-pick -m 1 fd9f578 will use the first parent listed in the merge as the base. 例如, git cherry-pick -m 1 fd9f578將使用合併中列出的第一個父對象作爲基礎。

    • Also consider that when you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit . 還要考慮一下,當您選擇一個合併提交時,它會將您未指定對-m的父級所做的所有更改摺疊到那個提交中 You lose all their history, and glom together all their diffs. 您會失去他們的所有歷史,並將他們所有的差異混在一起。 Your call. 你的來電。

  2. (Simpler and more familiar; preserves history) you can use git merge instead of git cherry-pick . (更簡單,更熟悉;保留歷史記錄),您可以使用git merge代替git cherry-pick

    • As is usual with git merge , it will attempt to apply all commits that exist on the branch you are merging, and list them individually in your git log. git merge ,它將嘗試應用要合併的分支上存在的所有提交,並在git日誌中單獨列出它們。

#4樓

Simplification of @Daira Hopwood method good for picking one single commit. @Daira Hopwood方法的簡化適用於選擇一個提交。 Need no temporary branches. 不需要臨時分支。

In the case of the author: 對於作者而言:

  • Z is wanted commit (fd9f578) Z是通緝犯(fd9f578)
  • Y is commit before it Y在它之前提交
  • X current working branch X當前工作分支

then do: 然後做:

git checkout Z   # move HEAD to wanted commit
git reset Y      # have Z as changes in working tree
git stash        # save Z in stash
git checkout X   # return to working branch
git stash pop    # apply Z to current branch
git commit -a    # do commit

#5樓

-m means the parent number. -m表示父編號。

From the git doc: 從git doc:

Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline. 通常,您無法選擇合併,因爲您不知道合併的哪一側應被視爲主線。 This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent. 此選項指定主線的父代號(從1開始),並允許cherry-pick重播相對於指定父代的更改。

For example, if your commit tree is like below: 例如,如果您的提交樹如下所示:

- A - D - E - F -   master
   \     /
    B - C           branch one

then git cherry-pick E will produce the issue you faced. 然後git cherry-pick E將產生您遇到的問題。

git cherry-pick E -m 1 means using DE , while git cherry-pick E -m 2 means using BCE . git cherry-pick E -m 1表示使用DE ,而git cherry-pick E -m 2表示使用BCE


#6樓

The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch. 櫻桃選擇的工作方式是,獲取變更集表示的差異(該點上的工作樹與其父級工作樹之間的差異),並將其應用於當前分支。

So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied? 因此,如果一個提交有兩個或多個父項,則它也代表兩個或多個差異-應該應用哪個差異?

You're trying to cherry pick fd9f578 , which was a merge with two parents. 您正在嘗試挑選fd9f578 ,它是與兩個父母合併的。 So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m option. 因此,您需要通過使用-m選項來告訴cherry-pick命令應針對哪個差異計算差異。 For example, git cherry-pick -m 1 fd9f578 to use parent 1 as the base. 例如, git cherry-pick -m 1 fd9f578使用父級1作爲基礎。

I can't say for sure for your particular situation, but using git merge instead of git cherry-pick is generally advisable. 對於您的特定情況,我無法確定,但是通常建議使用git merge而不是git cherry-pick When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit . 當您選擇一個合併提交時,它會將您未指定對-m的父級所做的所有更改摺疊到那個提交中 You lose all their history, and glom together all their diffs. 您會失去他們的所有歷史,並將他們所有的差異混在一起。 Your call. 你的來電。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章