【问题标题】:Update a local branch with the changes from a tracked remote branch使用来自跟踪的远程分支的更改更新本地分支
【发布时间】:2012-07-01 23:19:03
【问题描述】:

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

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

git pull origin my_remote_branch:my_local_branch

这是正确的方法吗?

【问题讨论】:

    标签: git branch git-branch remote-branch


    【解决方案1】:

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

    (见:

    git branch -f --track my_local_branch origin/my_remote_branch # 或者(如果 my_local_branch 当前已签出): $ 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.”)

    这意味着您的分支是 already configured 与:

    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 就足够了。

    【讨论】:

    • OP 提到他们已经在跟踪远程分支。
    • @Amber 因此我的回答是:git pull 就足够了。
    • 第一个命令git branch -f --track master origin/master返回错误:fatal: Cannot force update the current branch.
    • @MarkKramer 是的,我已经编辑了答案以使其更清楚,如果当前已签出本地分支,则将使用第二个命令。
    • 您还应该将其更改为--set-upstream-to--set-upstream 已弃用并将被删除。
    【解决方案2】:

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

    git pull origin my_remote_branch
    

    当你有 my_local_branch 签出时,你会做你想做的事。

    由于您已经设置了跟踪分支,因此您甚至不需要指定 - 您可以这样做...

    git pull
    

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

    【讨论】:

    • 这应该是正确的答案。就这么简单。
    • 谢谢,它对我有用。确实,这是一个正确的答案。
    【解决方案3】:

    注意:我是 git 新手。

    当我执行“git pull”时,我通常会看到“错误:您对以下文件的本地更改将被合并覆盖:”“请在合并之前提交您的更改或存储它们。” (因为我做了一些我并不真正关心的微小的临时更改。)

    如果我从远程提取,我通常不关心我的更改。我只想要团队推送的最新消息。 (我有时会使用“stash”来保留一些更改。)

    那么,我如何从远程获取最新信息并清除我的任何本地更改:

    git reset --hard(用于当前分支)

    git reset --hard origin/master(用于返回 master)

    然后:

    git pull(将当前远程文件拉到我的本地)

    【讨论】:

    • 如果用户确实关心更改,则使用git stash 保留未提交的更改将是明智的第一步。
    猜你喜欢
    • 2016-09-12
    • 2018-03-24
    • 2013-04-30
    • 2014-10-07
    • 2021-07-31
    • 2011-07-27
    • 2016-01-16
    • 2011-06-20
    相关资源
    最近更新 更多