git*_*182 676 git cherry-pick
我正在使用一个git存储库,需要从另一个不知道第一个存储库的git存储库提交.
通常我会HEAD@{x}
在reflog中使用它来挑选,但因为.git
它对这个reflog条目(不同的物理目录)一无所知,我怎么能挑选这个,或者我可以吗?
我正在使用git-svn
.我的第一个分支使用git-svn
的trunk
一个颠覆回购,而下一个分支是在git-svn
上一个Subversion分支.
Rob*_*ler 830
给出的答案是使用格式补丁,但由于问题是如何从另一个文件夹中挑选,所以这里有一段代码:
$ git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k
Run Code Online (Sandbox Code Playgroud)
(来自@cong ma的解释)
该
git format-patch
命令some_other_repo
从其SHA指定的提交创建一个补丁(仅-1
用于一次提交).此修补程序通过管道传输git am
,在本地应用修补程序(-3
意味着如果修补程序无法干净地应用,则尝试三向合并).希望解释一下.
Cha*_*esB 509
您需要将其他存储库添加为远程存储库,然后获取其更改.从那里你看到提交,你可以樱桃挑选它.
像那样:
git remote add other https://example.link/repository.git
git fetch other
Run Code Online (Sandbox Code Playgroud)
现在您可以完成所有信息git cherry-pick
.
有关使用遥控器的更多信息,请访问:https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
Bri*_*ian 142
这是一个remote-fetch-merge的例子.
cd /home/you/projectA
git remote add projectB /home/you/projectB
git fetch projectB
Run Code Online (Sandbox Code Playgroud)
然后你可以:
git cherry-pick <first_commit>..<last_commit>
Run Code Online (Sandbox Code Playgroud)
或者你甚至可以合并整个分支
git merge projectB/master
Run Code Online (Sandbox Code Playgroud)
doc*_*hat 118
你可以做到,但它需要两个步骤.这是如何做:
git fetch <remote-git-url> <branch> && git cherry-pick FETCH_HEAD
Run Code Online (Sandbox Code Playgroud)
替换<remote-git-url>
为您想要挑选的存储库的URL或路径.
替换<branch>
为您要从远程存储库中挑选的分支或标记名称.
您可以FETCH_HEAD
使用分支中的git SHA 替换它.
更新:根据@ pkalinow的反馈修改.
jar*_*lli 59
以下是添加远程,获取分支和樱桃选择提交的步骤
# Cloning our fork
$ git clone git@github.com: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
Run Code Online (Sandbox Code Playgroud)
资料来源:https://coderwall.com/p/sgpksw
Aas*_*set 17
请参阅如何使用Git创建和应用修补程序.(根据你的问题的措辞,我假设这个其他存储库用于完全不同的代码库.如果它是相同代码库的存储库,你应该将它添加为@CharlesB建议的远程存储库.即使它是另一个代码库,我想您仍然可以将其添加为远程,但您可能不希望将整个分支添加到您的存储库...)
Don*_*Don 10
您可以在一行中执行以下操作.希望你在git资源库中,需要樱桃挑选的更改,你已经检查出正确的分支.
git fetch ssh://git@stash.mycompany.com:7999/repo_to_get_it_from.git branchToPickFrom && git cherry-pick 02a197e9533
#
Run Code Online (Sandbox Code Playgroud)
git fetch [branch URL] [分支到樱桃挑选] && git cherry-pick [提交ID]
这是一个很容易凭记忆打出的内容,灵感来自@radicand 的评论。它依赖于 forge 的能力,但 Github、Gitlab 和 Gitea 绝对支持它。
您附加.patch
到提交 URL 并通过以下方式应用它git am
:
curl --location URL.patch | git am
Run Code Online (Sandbox Code Playgroud)
--location
使其遵循重定向,例如从拉取请求复制补丁时可能会发生这种情况
如果同一台机器上存在其他存储库,您可以通过应用补丁然后提交原始消息来实现与cherry-pick类似的效果。这是一个例子:
$ git apply <(git -C "$PATH_TO_OTHER_REPO" show "$COMMIT_HASH")
$ MSG="$(git -C "$PATH_TO_OTHER_REPO" log -n 1 --pretty=format:'%s' "$COMMIT_HASH")"
$ git commit -m "$MSG"
Run Code Online (Sandbox Code Playgroud)
我不需要经常这样做,所以我对这个工作流程很满意。然而,为此编写自定义的 Git 命令应该相当容易,并且让它更好、更自动化。
请注意,里面的命令<(...)
可以是为 Git 生成有效补丁的任何命令:git show
、git diff
、 usingwget
或curl
从远程(例如 Github)获取原始 diff 内容(这样您就可以跳过克隆)、cat
从文件...该行本身,非常有用。