【问题标题】:git reset to a local commit head; need to pull from origin keeping local changesgit 重置为本地提交头;需要从原点拉出保持本地更改
【发布时间】:2020-12-11 05:49:51
【问题描述】:

我有一个本地的feature/branch。当我从remote/origin 中提取时,它会覆盖我的本地更改,通常它会向我显示来自合并的冲突....如何设置本地 git 分支,以便在合并远程分支时获得冲突报告和能解决这些冲突吗?

更新:

使用git branch -vv 我可以看到分支已合并到remote branch origin/master 中并且有一个拉取请求ID。在上面进行了许多更改,当前feature/branch 的一些工作和一些更改已被覆盖。在其更改被覆盖之前,我将 feature/branch 移回了之前的提交。

我可以看到 git diff master 的变化,但我希望能够看到 冲突 并在 vscode 中解决它们,而不是滚动查看 git diff master 的终端输出。

我可以将local feature/branchremote feature/branch 分离,以便git 处理git pull origin master 上的冲突吗?

【问题讨论】:

  • 你正在运行什么命令来拉取?

标签: git git-branch git-merge git-pull git-fetch


【解决方案1】:

从远程获取最新更改的另一种方法是首先git fetch 这会将更改带到您的本地系统。然后检查使用 git diff 所做的更改,如果您想包含这些更改并保持更改,请使用:

git stash -u # u flag is used to include untracked files
git rebase
git stash apply

【讨论】:

    【解决方案2】:

    我假设有一个本地“master”已配置为“针对”远程源,并且有一个名为feature/branch 的本地分支由 master 创建,没有对应的删除。要检查(一种初始检查,与答案没有直接关系),请使用:

    git branch -vv
    

    您应该看到您的本地master 有一个远程分支对应origin/master(在方括号中列出),而feature/branch 没有任何远程分支。

    现在就解决方​​案而言:

    您可以完全避免合并。假设你当前的分支是feature/branch:

    # get the information about the latest changes from "origin". 
    # This doesn't change your local filesystem, so you can run it as often as you with 
    git fetch origin
    
    # make sure you don't have any uncommitted changes. If you do, stash them or commit if you need, Fo the sake of example, I assume you did three (3) local commits with ids (as if its a sha1) 'a', 'b' and 'c'.
    
    git status
    
    # now when you know that there are no commits and the status is empty, you can:
    
    git rebase origin/master
    

    最后一个命令接受在 origin/master 中发生的提交(假设这些是提交 'x'、'y' 和 'z')并执行变基:

    feature/branch 看起来像:

    'commont-parent-commit` --> 'x' --> 'y' --> 'z' --> 'a*' --> 'b*' --> 'c*'
    

    这里a* 在“逻辑上”与a 相同,但具有不同的sha1。

    如您所见,这里完全没有合并提交。

    如果在此过程中出现冲突,您必须解决它们,然后输入git rebase --continue

    注意,还有一个命令 git pull --rebase 也可以使用(它是一个 fetch + rebase 而不是众所周知的公式 pull = fetch + merge),它是一种更“紧凑”的方式来做同样的事情,但是我发现上面描述的方式对于从变基开始的人来说更容易理解。

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 2016-07-28
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多