git - 我如何拉取并合并我的未提交更改?

16

在处理一些未提交的文件时,我需要拉取新的代码。但是出现了冲突,所以 Git 拒绝了拉取:

处理未提交文件时,拉取新代码出现冲突,Git 拒绝拉取:

error: Your local changes to the following files would be overwritten by merge:
        ...
Please, commit your changes or stash them before you can merge.

问题1:我如何在保留未提交更改的情况下pull并merge?我需要继续工作,但我还没有准备好commit,但我想要外部代码?

问题2:我最终使用了stash,然后是pull。现在我该如何将我的更改合并到新的pull中?如何应用我的stash而不覆盖pull的新更改?


1
只需提交然后拉取。 - Andrew C
3个回答

28

使用 stash,然后 pull,最后 stash pop

git stash
git pull
git stash pop

那样做不会覆盖他们所做的更改吗?我需要将我的正在进行中的工作合并到自从我开始以来所做的更改中。 - SRobertJames
Git 的设计可以有效防止误操作,很难覆盖或丢失一些更改。如果想要“破坏”某些内容,需要使用特殊的非默认按键或参数。 - Victor Yarema

2
在深入了解合并之前,我需要提醒您注意有两种类似的解决方案可以“从远程获取最新更改”。 更多信息请参考git pull VS git fetch git rebase。 我更喜欢使用rebase,因为它不会产生冗余的合并提交。
不要害怕进行提交。您可以随意对其进行修改(使用git commit --amend进行修改),或者将其丢弃并将所有更改推回工作树中(使用git reset HEAD~1), 直到您将其推送到任何地方。

答案 1

git add .                           # stage all changes for commit
git commit -m 'Tmp'                 # make temporary commit
git fetch $RemoteName               # fetch new changes from remote
git rebase $RemoteName/$BranchName  # rebase my current working branch
    # (with temporary commit) on top of fethed changes
git reset HEAD~1                    # discard last commit
    # and pop all changes from it into worktree

答案 2

git stash pop    # this retrieves your changes
    # from last stash and put them as changes in worktree

这个命令不会影响你使用fetch 系列命令(如fetch, pull, ...)获取的提交记录。


-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接