【问题标题】:What's the git command to pull in all changes from a remote repo?从远程仓库中提取所有更改的 git 命令是什么?
【发布时间】:2011-08-03 09:57:54
【问题描述】:

我有一个从远程存储库派生的存储库。我做了一些更改,更新了远程存储库,现在我想从远程存储库中提取所有更改,而不关心本地存储库中的任何内容。以前我一直在删除我的本地存储库,然后做一个简单的分叉和克隆。必须有更好的方法来做到这一点。什么是魔法命令?

【问题讨论】:

  • 这里的大部分答案都建议拉动,也就是合并。如果您愿意删除您的存储库,我很确定这不是您想要的。
  • 没错,我不关心合并。 reset --hard 是我想要的。谢谢!

标签: git


【解决方案1】:

如果我对您的理解正确,您想丢弃本地分支上的提交(如果您已经提交的话)。如果是这样,那么你想要:

# fetch updated branch(es) from origin
git fetch origin

# reset, including your work tree, to origin's master branch
# make sure you have your master branch checked out first!
# and also that you don't mind throwing away local commits or
# uncommitted modifications
git reset --hard origin/master

【讨论】:

    【解决方案2】:

    一些假设:master 是旧分支,您可以在其中提交一些更改。现在other 是来自远程origin 的新结帐。

    git fetch origin
    git checkout -b other origin/master
    

    git diff other..master
    

    你可以比较这两个分支。终于有了

    git checkout other
    git merge master
    

    你合并它们。这里另一个有用的工具是cherry-pick,你可以只将一些有趣的提交合并到一个分支中

    git cherry-pick <commit>
    

    【讨论】:

    • 能否将命令添加到diff,然后合并两个分支?
    【解决方案3】:

    (考虑到您的分支是master,远程命名为origin

    首先更新您的origin

    git fetch origin

    现在,如果您自上次更新后还没有提交,您可以这样做:

    git rebase master origin/master

    如果你完成了一些提交,这个 fast-forward 变基将不起作用。在这种情况下,您可以这样做:

    git branch -d master (to remove your local master branch)

    git checkout -b master origin/master


    合并前的差异:

    如果您想在执行merge 之前查看发生了什么变化:

    git fetch origin (always to bring the changes from the remote)

    git diff master origin/master

    【讨论】:

    • Git 不会急于删除已签出的分支。你的 rebase 也没有按照你的想法做——它使用 master 作为基础。我想你的意思是git rebase origin/master master。但当然,我不会使用 rebase,只需 git merge --ff-only origin/master
    【解决方案4】:

    一个简单的方法是分支,像这样:

    git commit -m "All my latest stuff I don't care about"
    git branch newstuff refs/remotes/origin/master 
    git pull
    

    现在你已经拥有了所有的新东西。当然这是假设你想保留旧的东西。

    【讨论】:

      【解决方案5】:
      猜你喜欢
      • 2017-02-11
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2014-05-15
      • 2014-09-27
      相关资源
      最近更新 更多