如何在 git 中从一个远程分支拉到另一个分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30107836/
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 pull from one remote branch to another branch in git?
提问by hidemyname
For safety reason, my supervisor doesn't allow me to pull his work directly from the master branch. So I created one branch remotely named 'subbranch' and clone it to my local repository. How could I let 'subbranch' keep up to date with the master branch? I just know how to use the GUI to do it. How to do it with command line?
出于安全原因,我的主管不允许我直接从 master 分支拉他的工作。因此,我创建了一个名为“subbranch”的远程分支并将其克隆到我的本地存储库。我怎样才能让“子分支”与主分支保持同步?我只知道如何使用 GUI 来做到这一点。如何用命令行做到这一点?
回答by rgulia
If you have not done it already clone the repository from master and then create a local branch in your clone, tracking to your upstream branch. For example
如果你还没有这样做,它已经从 master 克隆存储库,然后在你的克隆中创建一个本地分支,跟踪到你的上游分支。例如
git clone https://github.com/rgulia/myclone
cd myclone
git checkout -b subbranch origin/subbranch
When you need to work in your clone, make sure you are on your branch.
当您需要在您的克隆中工作时,请确保您在您的分支上。
git branch # your current branch is highlighted with a '*'
Work on your branch normally, by committing changes to your branch. Push changes to your remote branch.
通过向您的分支提交更改,正常在您的分支上工作。将更改推送到远程分支。
git commit
git push origin subbranch
From time to time you want to update with the changes from master
有时您想使用 master 的更改进行更新
git fetch # get the changes from the remote repository.
git merge master # merge them into your branch
git push origin subbranch # push your changes upstream
The git fetch applies to all branches, including master. The git merge creates a commit in your branch. Since you always work in your branch, you never commit nor push to master.
git fetch 适用于所有分支,包括 master。git merge 在你的分支中创建一个提交。由于您始终在您的分支中工作,因此您从不提交或推送到 master。
You might have to resolve conflicts. See this excellent tutorial. http://www.vogella.com/tutorials/Git/article.html#mergeconflict
您可能需要解决冲突。看到这个优秀的教程。 http://www.vogella.com/tutorials/Git/article.html#mergeconflict
I hope this helps.
我希望这有帮助。
回答by Kartik Goyal
git branch --set-upstream-to remote-branch # to track your local branch with remote branch
git pull --rebase # to get the latest changes of remote branch
git branch --set-upstream-to remote-branch # 使用远程分支跟踪本地分支
git pull --rebase # 获取远程分支的最新变化
回答by Sumit Kumawat
- List item
- 项目清单
I'm assuming two branches master and demo.Merge Master code to demo Step 1 A.go to the master->git checkout Master. B.pull all the new changes from Master branch. Step 2 A. now got to demo branch->git checkout demo Step 3 A.merge the changes of Master branch into Demo branch git merge Master B.Then push your updated code to git git push origin demo
我假设有两个分支 master 和 demo.Merge Master code to demo Step 1 A.go to the master->git checkout Master。B.从主分支拉取所有新的变化。Step 2 A. 现在进入demo branch->git checkout demo Step 3 A.将Master分支的变化合并到Demo branch git merge Master B.然后将你更新的代码推送到git git push origin demo