如何将远程git分支干净地获取/复制到本地存储库 [英] How to cleanly get/copy a remote git branch to local repository
问题描述
我想要复制"到特定本地分支的远程分支的精确副本".
I want an exact "copy" of a remote branch "copied" to a specific local branch.
例如,一个团队成员创建了一个实验性功能,他将该功能检入了远程存储库中名为 experiment
的分支中.我希望能够在本地存储库中签出一个新分支,然后将 experiment
分支按原样复制"到我新签出的分支中.
Say, for example, a team member has created an experimental feature, which he has checked into a branch called experiment
on the remote repository. I want to be able to checkout a new branch on my local repository, and "copy" the experiment
branch, as-is, to my newly checked out branch.
我不想将其与我的代码合并-我想完全覆盖我的代码,这样我就可以清楚地了解他在实验"分支上所做的工作.
I don't want to merge it with my code - I want to completely overwrite my code so that I can have a clean look at what he's done on the "experiment" branch.
一个人如何获取"(获取/拉取/以任何方式...)别人已经提交给远程存储库的远程分支到您自己的本地存储库,而这种方式不会尝试对您的本地存储库执行合并自己的本地代码?
How would one "get" (fetch/pull/whatever...) a remote branch that someone else has committed to the remote repository to your own local repository, in a way that does not try and perform a merge on your own local code?
推荐答案
如果您不关心合并:
git reset --hard <remote>/<branch_name>
这将完全满足您的要求:无需合并,无需重新设置基准,只需将本地分支置于与远程节点完全相同的状态即可.
This will exactly do what you want: no merging, no rebasing, simply put the local branch to exactly the same state as the remote is.
但是,对于您而言,则不需要.您要创建一个与远程状态相同的 new 本地分支,因此在使用
In your case however, you don't need that. You want to create a new local branch that has the same state as the remote, so specify the remote branch when creating the local one with git checkout
:
git checkout -b <my_new_branch> <remote>/<branch_name>
如果要在本地使用相同的名称,可以将其缩短为:
If you want to use the same name locally, you can shorten that to:
git checkout <branch_name>
您应使用 git reset --hard
如果您已经有一个本地分支(例如,其中有一些更改),请放弃您 所做的任何修改,而采用远程分支的确切版本.
You would use git reset --hard
if you already have a local branch (for example with some changes in it), discard any modifications that you made and instead take the exact version of the remote branch.
要确保您具有远程分支的最新状态,请使用 git fetch< remote>
,然后再签出或重置.
To be sure that you have the latest state of the remote branch, use git fetch <remote>
before either checking out or resetting.
这篇关于如何将远程git分支干净地获取/复制到本地存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!