git 如何从现有的远程分支创建本地分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24301914/
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 create a local branch from an existing remote branch?
提问by tempuser
I want to create a branch from an existing remote branch (let's say remote-A) and then commit the changes to the repository.
我想从现有的远程分支(假设是 remote-A)创建一个分支,然后将更改提交到存储库。
I have used the below commands to create a local branch from the existing remote-A
我使用以下命令从现有的远程 A 创建本地分支
$git checkout remote-A
git branch
master
* remote-A
Now I have created local-B from Remote A using the below commands
现在我已经使用以下命令从远程 A 创建了本地 B
git branch local-B
git checkout local-B
How do I make sure the changes I have on local-B are on top of remote-A so that when I push local-B to the remote repo, the changes are on top of remote-A?
如何确保我对本地 B 的更改位于远程 A 之上,以便当我将本地 B 推送到远程存储库时,更改位于远程 A 之上?
回答by xploreraj
Old post, still I'd like to add what I do.
旧帖子,我仍然想补充我所做的。
1. git remote add <remote_name> <repo_url>
2. git fetch <remote_name>
3. git checkout -b <new_branch_name> <remote_name>/<remote_branch_name>
This series of commands will
这一系列命令将
- create a new remote,
- fetch it into your local so your local git knows about its branches and all,
- create a new branch from the remote branch and checkout to that.
- 创建一个新的遥控器,
- 将它提取到您的本地,以便您的本地 git 知道它的分支和所有内容,
- 从远程分支创建一个新分支并结帐到该分支。
Now if you want to publish this new local branch to your remote and set the upstream url also
现在,如果您想将此新的本地分支发布到您的远程并设置上游 url
git push origin +<new_branch_name>
git push origin +<new_branch_name>
Also, if only taking in remote changes was your requirement and remote already exists in your local, you could have done, instead of step 2 and 3,
此外,如果您只需要进行远程更改,并且本地已经存在远程更改,那么您可以完成,而不是第 2 步和第 3 步,
git pull --rebase <remote_name> <remote_branch_name>
and then opted for
git mergetool
(needs configurations separately) in case of any conflicts, and follow console instructions from git.
然后git mergetool
在发生任何冲突时选择
(需要单独配置),并按照 git 的控制台说明进行操作。
回答by Vladimir Dimitrov
you want to create branch on base of remote-A, make changes on it and then push them on remote-A?
您想在 remote-A 的基础上创建分支,对其进行更改,然后将它们推送到 remote-A?
git checkout -b remote-A
git pull origin remote-A
git checkout -b remote-B
make changes on remote-B
在远程 B 上进行更改
git commit -a -m 'describe changes on remote-B branch'
git checkout remote-A
git merge remote-B
git push origin remote-A
回答by ali jafargholi
This should work:
这应该有效:
git checkout --track origin/<REMOTE_BRANCH_NAE>
git checkout --track origin/<REMOTE_BRANCH_NAE>
回答by ionutioio
To make sure your changes are on top, you must not pull from remote. you must fetch and rebase. il will be something like this:
为了确保您的更改在最上面,您不能从远程拉取。你必须获取和变基。我会是这样的:
fetch->stash->rebase->stash pop->commit->push