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

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

现在,远程分支已经更新了,我在“ my_local_branch”上,想要引入这些更改。我应该这么做吗:

git pull origin my_remote_branch:my_local_branch

这是正确的方法吗?

419084 次浏览

你已经设置了上游的那个分支

(见:

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 --track将无法工作: 使用第二个命令 git branch --set-upstream-to,否则将得到“ fatal: Cannot force update the current branch.”)

这意味着你的分行是 已经配置好了:

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

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

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

就够了。


如果你没有建立上游分支关系,当它来推动你的“ my_local_branch”,那么一个简单的 git push -u origin my_local_branch:my_remote_branch将足以推动 还有设置上游分支。
在此之后,对于随后的拉/推,git pullgit push就足够了。

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

git pull origin my_remote_branch

当你有 my_local_branch检查将做你想要的。

因为您已经有了跟踪分支集,所以您甚至不需要指定-您只需要..。

git pull

当你有 my_local_branch签出,它将从追踪的分支更新。

注意: 我是个 Git 新手。

当我执行“ git pull”操作时,我通常会看到“ error: Your local change to the following files would be overwrite by merge:”“请在合并之前提交您的更改或者隐藏它们。”(因为我做了一些我并不关心的小小的临时调整。)

我通常不关心我的变化,如果我从远程拉。我只想知道团队的最新进展。(我有时会使用“隐藏”来保留一些变化。)

所以,我要做的就是从远程中提取最新的数据并删除本地的任何更改:

Git 复位—— hard (针对当前分支)

或者

Git 复位——硬源/master (用于返回 master)

然后:

Git pull (将当前远程文件拉到本地)

因为有人不小心搞砸了本地犯罪。

删除本地脏分支

git branch -D master

然后从远程重建一个分支

git checkout -b master origin/master