git重置为本地提交头;需要从原点撤出并保持本地变化

问题描述

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

更新:

使用git branch -vv,我可以看到该分支已合并到remote branch origin/master中,并且具有拉取请求ID。在顶部进行了许多更改,在当前feature/branch上进行了一些工作,并且对某些更改进行了覆盖。我将feature/branch移回了先前的提交,然后才覆盖了更改。

我可以看到git diff master中的更改,但是我希望能够看到冲突并在vscode中解决它们,而不是滚动浏览{{1} }。

我可以将git diff masterlocal feature/branch分离,以便git拾取remote feature/branch上的冲突吗?

解决方法

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

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

我假设有一个本地“ 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

最后一条命令接收在原始服务器/主服务器中发生的提交(假设它们是提交'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),它是一种更“紧凑”的方式,但我发现上述方法对于重新定级的人们来说更容易理解。