简体   繁体   English

从远程git repo获取特定提交

[英]Fetch specific commit from remote git repo

So, what I'm basically trying to do is to pull down a given commit (identified by its SHA) from one remote repo, and then push it to another remote repo as a new branch (whose name I autogenerate). 因此,我基本上要做的是从一个远程仓库下拉一个给定的提交(由其SHA标识),然后将其作为新分支(其名称我自动生成)推送到另一个远程仓库。 The source repo will vary and these are all one-shot operations, so I don't want to git remote add these remotes and I don't want to create refs/branches for the commits. 源代码将有所不同,这些都是一次性操作,所以我不想git remote add这些遥控器,我不想为提交创建引用/分支。 And I don't want to change the HEADs of any of my local branches. 而且我不想改变任何本地分支机构的HEAD。

So, how do I just grab the given remote commit (and any of its parent commits that are new to me) and add it/them to my local git database? 那么, 我如何才能获取给定的远程提交(以及任何对我来说不熟悉的父提交)并将它/它们添加到我的本地git数据库中?

git pull implicitly involves a merge or rebase, so that's out of the question. git pull隐式涉及合并或变基,所以这是不可能的。

I tried git fetch https://github.com/foo/bar.git 7d0969daba1fdf9d5bb9bc9a2847fd1bc1c7e426 我试过git fetch https://github.com/foo/bar.git 7d0969daba1fdf9d5bb9bc9a2847fd1bc1c7e426
but that just leads to 但这只是导致
error: no such remote ref 12819ad8e10e5906df5352d7d8fec1fceb1f3afc
(and yes, I verified that that commit SHA exists on that remote; looks like git doesn't accept a SHA here anyway). (是的,我验证了那个提交SHA存在于该遥控器上;看起来git在这里不接受SHA)。

I mean, I guess I could come up with a single arbitrary local branch name to always pull the commits down into, and then delete that branch after every push, but that's seems inelegant... 我的意思是,我想我可以想出一个任意的本地分支名称,以便始终将提交下拉,然后在每次推送后删除该分支,但这似乎是不优雅的......

Git does not allow you to fetch by SHA by design. Git不允许您按设计获取SHA。 It doesn't seem like there are any plans to enable that either based on previous conversations on the developers mailing list 根据开发人员邮件列表上的先前对话,似乎没有计划启用它

You will have to have a reference on the remote in order to be able to fetch. 您必须在遥控器上有一个参考才能获取。 You should be able to auto-generate this part I would imagine, since you have to have a mechanism to pass the remote and sha back and forth already. 您应该能够自动生成我想象的这个部分,因为您必须有一个机制来传递远程和Sha来回。

Use git fetch to get all commits from your origin repository. 使用git fetchorigin存储库获取所有提交。 Then, create a branch on the desired commit 然后,在所需的提交上创建一个分支

git branch theBranch 7d0969dab

Now, you can push this branch to the second repo 现在,您可以将此分支推送到第二个仓库

git push https://github.com/secondRepo/bar.git theBranch:theRemoteBranchNameYouWant

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM