git 使用来自跟踪的远程分支的更改更新本地分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11278497/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:03:47  来源:igfitidea点击:

Update a local branch with the changes from a tracked remote branch

gitbranchgit-branchremote-branch

提问by skyork

I have a local branch named 'my_local_branch', which tracks a remote branch origin/my_remote_branch.

我有一个名为“ my_local_branch”的本地分支,它跟踪远程分支origin/my_remote_branch

Now, the remote branch has been updated, and I am on the 'my_local_branch' and want to pull in those changes. Should I just do:

现在,远程分支已更新,我在 ' my_local_branch' 上并希望引入这些更改。我应该这样做吗:

git pull origin my_remote_branch:my_local_branch

Is this the correct way?

这是正确的方法吗?

采纳答案by VonC

You have set the upstream of that branch

您已经设置了该分支的上游

(see:

(看:

git branch -f --track my_local_branch origin/my_remote_branch
# OR (if my_local_branch is currently checked out):
$ git branch --set-upstream-to my_local_branch origin/my_remote_branch

(git branch -f --trackwon't work if the branch is checked out: use the second command git branch --set-upstreaminstead, or you would get "fatal: Cannot force update the current branch.")

git branch -f --track如果分支被检出将不起作用:改用第二个命令git branch --set-upstream,否则你会得到“ fatal: Cannot force update the current branch.”)

That means your branch is already configuredwith:

这意味着您的分支已经配置了:

branch.my_local_branch.remote origin
branch.my_local_branch.merge my_remote_branch

Git already has all the necessary information.
In that case:

Git 已经拥有所有必要的信息。
在这种情况下:

# if you weren't already on my_local_branch branch:
git checkout my_local_branch 
# then:
git pull

is enough.

足够。



If you hadn't establish that upstream branch relationship when it came to push your 'my_local_branch', then a simple git push -u origin my_local_branch:my_remote_branchwould have been enough to push andset the upstream branch.
After that, for the subsequent pulls/pushes, git pullor git pushwould, again, have been enough.

如果您在推送您的 ' my_local_branch'时没有建立上游分支关系,那么一个简单的git push -u origin my_local_branch:my_remote_branch就足以推送设置上游分支。
在那之后,对于后续的拉/推,git pull或者git push再一次,已经足够了。

回答by Amber

You don't use the :syntax - pullalways modifies the currently checked-out branch. Thus:

您不使用:语法 -pull始终修改当前签出的分支。因此:

git pull origin my_remote_branch

while you have my_local_branchchecked out will do what you want.

当你my_local_branch签出时会做你想做的。

Since you already have the tracking branch set, you don't even need to specify - you could just do...

由于您已经设置了跟踪分支,因此您甚至不需要指定 - 您只需执行...

git pull

while you have my_local_branchchecked out, and it will update from the tracked branch.

当您my_local_branch签出时,它将从跟踪的分支更新。