git 如何从远程分支中挑选?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13788945/
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
How to cherry-pick from a remote branch?
提问by user291701
I'm having trouble performing a cherry-pick. On my local machine, I'm currently on my "master" branch. I want to cherry-pick in a commit from another branch, named "zebra". The "zebra" branch is a remote branch.
我在挑选樱桃时遇到了麻烦。在我的本地机器上,我目前在我的“主”分支上。我想从另一个名为“斑马”的分支中挑选一个提交。“斑马”分支是一个远程分支。
So git status:
所以git状态:
# On branch master
nothing to commit (working directory clean)
Ok, now I try to cherry-pick the commit I want:
好的,现在我尝试挑选我想要的提交:
git cherry-pick xyz
fatal: bad object xyz
where "xyz" is the signature of the commit I'm interested in, that happened on branch "zebra".
其中“xyz”是我感兴趣的提交的签名,它发生在分支“zebra”上。
So the first obvious question is, why can't git find the commit I'm referencing? I don't really understand how this is working in the first place to be honest. Does git store something like a database of commits locally in my working directory, for all other branches? When executing the cherry-pick command, does it go and search that local database to find the commit I'm talking about?
所以第一个明显的问题是,为什么 git 找不到我引用的提交?老实说,我一开始真的不明白这是如何工作的。对于所有其他分支,git 是否在我的工作目录中本地存储了诸如提交数据库之类的东西?执行cherry-pick 命令时,它是否会搜索本地数据库以找到我正在谈论的提交?
Since "zebra" is a remote branch, I was thinking I don't have its data locally. So I switched branches:
由于“斑马”是一个远程分支,我想我在本地没有它的数据。所以我切换了分支:
git checkout zebra
Switched to branch 'zebra'
So now here on my local machine, I can see that the files in the directory reflect zebra's state correctly. I switch back to master, try to cherry-pick again (hoping the commit data is available now), but I get the same problem.
所以现在在我的本地机器上,我可以看到目录中的文件正确反映了 zebra 的状态。我切换回 master,再次尝试挑选(希望提交数据现在可用),但我遇到了同样的问题。
I've got a fundamental misunderstanding of what's going on here, any help would be great.
我对这里发生的事情有一个根本的误解,任何帮助都会很棒。
回答by Peter Lundgren
Since "zebra" is a remote branch, I was thinking I don't have its data locally.
由于“斑马”是一个远程分支,我想我在本地没有它的数据。
You are correct that you don't have the right data, but tried to resolve it in the wrong way. To collect data locally from a remote source, you need to use git fetch
. When you did git checkout zebra
you switched to whatever the state of that branch was the last time you fetched. So fetch from the remote first:
您没有正确的数据是正确的,但试图以错误的方式解决它。要从远程来源本地收集数据,您需要使用git fetch
. 当您这样做时,git checkout zebra
您切换到该分支的任何状态是您上次获取时的状态。所以首先从远程获取:
# fetch just the one remote
git fetch <remote>
# or fetch from all remotes
git fetch --all
# make sure you're back on the branch you want to cherry-pick to
git cherry-pick xyz
回答by Cezar Augusto
Just as an addendum to OP accepted answer:
就像 OP 接受的答案的附录一样:
If you having issues with
如果你有问题
fatal: bad object xxxxx
that's because you don't have access to that commit. Which means you don't have that repo stored locally. Then:
那是因为您无权访问该提交。这意味着您没有将该存储库存储在本地。然后:
git remote add LABEL_FOR_THE_REPO REPO_YOU_WANT_THE_COMMIT_FROM
git fetch LABEL_FOR_THE_REPO
git cherry-pick xxxxxxx
Where xxxxxxx is the commit hash you want.
其中 xxxxxxx 是您想要的提交哈希。
回答by Muhammad Soliman
Adding remote repo (as "foo") from which we want to cherry-pick
添加我们想要挑选的远程仓库(作为“foo”)
$ git remote add foo git://github.com/foo/bar.git
Fetch their branches
取他们的树枝
$ git fetch foo
List their commits (this should list all commits in the fetched foo
)
列出他们的提交(这应该列出 fetched 中的所有提交foo
)
$ git log foo/master
Cherry-pick the commit you need
樱桃挑选你需要的提交
$ git cherry-pick 97fedac
回答by Juuso Ohtonen
After merging a development branch to master, I usually delete the development branch. However, if I want to cherry pick the commits in the development branch, I have to use the merge commithash to avoid "bad object" error.
将开发分支合并到master后,我通常会删除开发分支。但是,如果我想在开发分支中挑选提交,我必须使用合并提交哈希来避免“坏对象”错误。
回答by temyong
Need to pull both branch data on your local drive first.
需要先在本地驱动器上提取两个分支数据。
What is happening is your trying to cherry-pick from branch-a to branch-b, where in you are currently on branch-b, but the local copy of branch-a is not updated yet (you need to perform a git pull on both branches first).
发生的事情是你试图从分支 a 到分支 b 中挑选,你目前在分支 b 上,但分支 a 的本地副本尚未更新(你需要执行 git pull on首先是两个分支)。
steps:
- git checkout branch-a
- git pull origin branch-a
- git checkout branch-b
- git pull origin branch-b
- git cherry-pick <hash>
步骤:
- git checkout branch-a
- git pull origin branch-a
- git checkout branch-b
- git pull origin branch-b
- git cherry-pick <hash>
output:
[branch-b <hash>] log data
Author: Author <Author
1 file changed, 1 insertion(+), 3 deletions(-)
输出:
[branch-b <hash>] 日志数据
作者:作者 <作者
1 个文件已更改,1 次插入(+),3 次删除(-)
回答by Mary
I had this error returned after using the commit id from a pull request commit id tab. That commit was subsequently squashed and merged. In the github pull request, look for this text: "merged commit xxxxxxx into..." instead of attempting to use the commit ids from the commits tab.
在使用拉取请求提交 ID 选项卡中的提交 ID 后,我返回了此错误。该提交随后被压缩并合并。在 github 拉取请求中,查找此文本:“merged commit xxxxxxx into...”,而不是尝试使用提交选项卡中的提交 ID。
回答by Kuldeep Saxena
The commit should be present in your local, check by using git log
.
提交应该存在于您的本地,请使用git log
.
If the commit is not present then try git fetch
to update the local with the latest remote.
如果提交不存在,则尝试git fetch
使用最新的远程更新本地。
回答by lulalala
If you have fetched, yet this still happens, the following might be a reason.
如果您已提取,但仍然发生这种情况,则可能是以下原因。
It can happen that the commit you are trying to pick, is no longer belonging to any branch. This may happen when you rebase.
您尝试选择的提交可能不再属于任何分支。变基时可能会发生这种情况。
In such case, at the remote repo:
在这种情况下,在远程仓库中:
git checkout xxxxx
git checkout -b temp-branch
git checkout xxxxx
git checkout -b temp-branch
Then in your repo, fetch again. The new branch will be fetched, including that commit.
然后在你的回购中,再次获取。将获取新分支,包括该提交。
回答by RVscript
I solved this issue by going on the branch with the commit I want to cherry pick.
我通过使用我想要挑选的提交进入分支解决了这个问题。
git checkout <branch With Commit To Cherry-Pick>
use log to find commit hash
使用日志查找提交哈希
git log
when you've found your hash cut and paste on note pad. if using command just scroll up to get the hash then checkout the branch you want to place the commit in.
当你在记事本上找到你的哈希剪切和粘贴时。如果使用命令只需向上滚动以获取哈希值,然后签出要放置提交的分支。
git checkout < branch I Want To Place My Cherry-Picked-Hash In>
finally call cherry-pick from git (note) -x is to append your cherry-pick message to the original. "When recording the commit, append a line that says "(cherry picked from commit …?)" to the original commit message in order to indicate which commit this change was cherry-picked from. "
最后从 git (note) -x 调用cherry-pick 是将您的cherry-pick 消息附加到原始消息中。“在记录提交时,在原始提交消息中附加一行写着“(从提交中挑选的樱桃......?)”,以表明此更改是从哪个提交中挑选出来的。“
git cherry-pick -x <your hash commit to add to the current branch>
回答by Giona
This can also be easily achieved with SourceTree:
这也可以通过 SourceTree 轻松实现:
- checkout your master branch
- open the "Log / History" tab
- locate the xyz commit and right click on it
- click on "Merge..."
- 结帐你的主分支
- 打开“日志/历史”选项卡
- 找到 xyz 提交并右键单击它
- 点击“合并...”
done :)
完毕 :)