是否可以从另一个 git 存储库中挑选提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5120038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:12:26  来源:igfitidea点击:

Is it possible to cherry-pick a commit from another git repository?

gitcherry-pick

提问by gitcoder182

I'm working with a git repository that needs a commit from another git repository that knows nothing of the first.

我正在使用一个 git 存储库,它需要从另一个对第一个一无所知的 git 存储库提交。

Typically I would cherry-pick using the HEAD@{x}in the reflog, but because this .gitknows nothing of this reflog entry (different physical directory), how can I cherry-pick this, or can I?

通常我会HEAD@{x}在 reflog 中选择使用,但是因为这.git对这个 reflog 条目(不同的物理目录)一无所知,我该如何选择它,或者我可以吗?

I'm using git-svn. My first branch is using git-svnof the trunkof a Subversion repo, and the next branch is using git-svnon a Subversion branch.

我正在使用git-svn. 我的第一个分支使用git-svntrunk是 Subversion 存储库,下一个分支使用git-svn的是 Subversion 分支。

采纳答案by CharlesB

You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

您需要将另一个存储库添加为远程存储库,然后获取其更改。从那里你可以看到提交,你可以挑选它。

Like that:

像那样:

git remote add other https://example.link/repository.git
git fetch other

Now you have all the information to simply do git cherry-pick.

现在您拥有了所有可以轻松完成的信息git cherry-pick

More info about working with remotes here: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

有关在此处使用遥控器的更多信息:https: //git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

回答by Robert Wahler

The answer, as given, is to use format-patch but since the question was how to cherry-pick from another folder, here is a piece of code to do just that:

给出的答案是使用格式补丁,但由于问题是如何从另一个文件夹中挑选,这里有一段代码可以做到这一点:

$ git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k

(explanation from @cong ma)

The git format-patchcommand creates a patch from some_other_repo's commit specified by its SHA (-1for one single commit alone). This patch is piped to git am, which applies the patch locally (-3means trying the three-way merge if the patch fails to apply cleanly). Hope that explains.

(来自@cong ma 的解释)

git format-patch命令some_other_repo从其 SHA 指定的提交创建补丁(仅-1针对一次提交)。此补丁通过管道传输到git am,它在本地应用补丁(-3意味着如果补丁无法完全应用,则尝试三向合并)。希望能解释一下。

回答by Brian

Here's an example of the remote-fetch-merge.

这是远程获取合并的示例。

cd /home/you/projectA
git remote add projectB /home/you/projectB
git fetch projectB

Then you can:

然后你可以:

git cherry-pick <first_commit>..<last_commit>

or you could even merge the whole branch

或者你甚至可以合并整个分支

git merge projectB/master

回答by docwhat

You can do it, but it requires two steps. Here's how:

你可以做到,但它需要两个步骤。就是这样:

git fetch <remote-git-url> <branch> && git cherry-pick FETCH_HEAD

Replace <remote-git-url>with the url or path to the repository you want cherry-pick from.

替换<remote-git-url>为您要从中挑选的存储库的 url 或路径。

Replace <branch>with the branch or tag name you want to cherry-pick from the remote repository.

替换<branch>为您要从远程存储库中挑选出来的分支或标签名称。

You can replace FETCH_HEADwith a git SHA from the branch.

您可以FETCH_HEAD使用分支中的 git SHA替换。

Updated: modified based on @pkalinow's feedback.

更新:根据@pkalinow 的反馈修改。

回答by jaredwilli

Here are the steps to add a remote, fetch branches, and cherry-pick a commit

以下是添加远程、获取分支和挑选提交的步骤

# Cloning our fork
$ git clone [email protected]:ifad/rest-client.git

# Adding (as "endel") the repo from we want to cherry-pick
$ git remote add endel git://github.com/endel/rest-client.git

# Fetch their branches
$ git fetch endel

# List their commits
$ git log endel/master

# Cherry-pick the commit we need
$ git cherry-pick 97fedac

Source: https://coderwall.com/p/sgpksw

来源:https: //coderwall.com/p/sgpksw

回答by Aasmund Eldhuset

See How to create and apply a patch with Git. (From the wording of your question, I assumed that this other repository is for an entirely different codebase. If it's a repository for the same code base, you should add it as a remote as suggested by @CharlesB. Even if it is for another code base, I guess you could still add it as a remote, but you might not want to get the entire branch into your repository...)

请参阅如何使用 Git 创建和应用补丁。(根据您的问题的措辞,我假设这个其他存储库用于完全不同的代码库。如果它是相同代码库的存储库,您应该按照@CharlesB 的建议将其添加为远程。即使是另一个代码库,我想您仍然可以将其添加为远程,但您可能不想将整个分支添加到您的存储库中...)

回答by Don

You can do it in one line as following. Hope you are in git repository which need the cherry-picked change and you have checked out to correct branch.

您可以在一行中完成,如下所示。希望您在 git 存储库中需要精心挑选的更改,并且您已签出正确的分支。

git fetch ssh://[email protected]:7999/repo_to_get_it_from.git branchToPickFrom && git cherry-pick 02a197e9533
# 

git fetch [branch URL][Branch to cherry-pick from]&& git cherry-pick [commit ID]

git fetch [branch URL] [branch to cherry-pick from]&& git cherry-pick [commit ID]

回答by wilhelmtell

Yes. Fetch the repository and then cherry-pick from the remote branch.

是的。获取存储库,然后从远程分支中挑选。

回答by pranavk

Assuming Ais the repo you want to cherry-pick from, and Bis the one you want to cherry-pick to, you can do this by adding </path/to/repo/A/>/.git/objectsto </path/to/repo/B>/.git/objects/info/alternates. Create this alternatesfiles if it does not exist.

假设A是您想要挑选的回购,并且B是您想要挑选的回购,您可以通过添加</path/to/repo/A/>/.git/objects</path/to/repo/B>/.git/objects/info/alternates. alternates如果该文件不存在,则创建该文件。

This will make repo B access all git objects from repo A, and will make cherry-pick work for you.

这将使 repo B 访问 repo A 中的所有 git 对象,并使cherry-pick 为您工作。

回答by Diomidis Spinellis

If you want to cherry-pick multiple commits for a given file until you reach a given commit, then use the following.

如果您想为给定文件挑选多个提交,直到达到给定提交,请使用以下命令。

# Directory from which to cherry-pick
GIT_DIR=...
# Pick changes only for this file
FILE_PATH=...
# Apply changes from this commit
FIST_COMMIT=master
# Apply changes until you reach this commit
LAST_COMMIT=...

for sha in $(git --git-dir=$GIT_DIR log --reverse --topo-order --format=%H $LAST_COMMIT_SHA..master -- $FILE_PATH ) ; do 
  git --git-dir=$GIT_DIR  format-patch -k -1 --stdout $sha -- $FILE_PATH | 
    git am -3 -k
done