如何干净地获取/复制远程 git 分支到本地存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11356460/
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 cleanly get/copy a remote git branch to local repository
提问by Stanley
I want an exact "copy" of a remote branch "copied" to a specific local branch.
我想要一个“复制”到特定本地分支的远程分支的精确“副本”。
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.
举例来说,一个团队成员创建了一个实验性功能,他已将其签入experiment
远程存储库上调用的分支。我希望能够在我的本地存储库上签出一个新分支,并将该experiment
分支按原样“复制”到我新签出的分支。
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?
如何“获取”(获取/拉取/无论如何...)其他人已将远程存储库提交到您自己的本地存储库的远程分支,以不尝试对您自己的本地代码执行合并的方式?
回答by eckes
If you don't care about merging:
如果您不关心合并:
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.
这将完全满足您的要求:不合并,不重新定位,只需将本地分支置于与远程分支完全相同的状态。
In your case however, you don't need that. You want to create a newlocal branch that has the same state as the remote, so specify the remote branch when creating the local one with git checkout
:
但是,在您的情况下,您不需要那个。您想创建一个与远程具有相同状态的新本地分支,因此在创建本地分支时指定远程分支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>
You would use git reset --hard
if you already have a local branch (for example with some changes in it), discard any modifications that youmade and instead take the exact version of the remote branch.
git reset --hard
如果您已经有一个本地分支(例如其中有一些更改),您将使用,放弃您所做的任何修改,而是采用远程分支的确切版本。
To be sure that you have the latest state of the remote branch, use git fetch <remote>
before either checking out or resetting.
为确保您拥有远程分支的最新状态,请git fetch <remote>
在签出或重置之前使用。
回答by paulvantuyl
I don't have enough rep to comment, but I'd like to add an example for those still struggling. In my example, I'm using Github as my remote and copying the branch to my Linux server environment/terminal.
我没有足够的代表发表评论,但我想为那些仍在苦苦挣扎的人添加一个例子。在我的示例中,我使用 Github 作为我的远程并将分支复制到我的 Linux 服务器环境/终端。
First, you'll want to run git fetch
To make sure you're up to date.
首先,您需要运行git fetch
以确保您是最新的。
Using the example of
使用的例子
git checkout -b <my_new_branch> <remote>/<branch_name>
git checkout -b <my_new_branch> <remote>/<branch_name>
With a remote branch named “picklerick”
使用名为“picklerick”的远程分支
I ran
我跑了
git checkout -b picklerick remotes/origin/picklerick
git checkout -b picklerick remotes/origin/picklerick
I probably could have just used remote/picklerick
for my remote path, but this worked for me.
我可能只用于remote/picklerick
我的远程路径,但这对我有用。