【问题标题】:git - How can I pull and merge with my uncommitted changes?git - 如何拉取和合并我未提交的更改?
【发布时间】:2016-02-05 17:34:18
【问题描述】:

在处理一些未提交的文件时,我需要提取新代码。有冲突,所以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:如何提取和合并未提交的更改?我需要继续工作,我还没准备好提交,但我想要外部代码?

问题 2:我最终做了一个stash,然后是一个pull。我现在如何合并对新拉取的更改?如何在不破坏 pull 的新更改的情况下应用我的存储?

【问题讨论】:

  • 提交然后拉取

标签: git


【解决方案1】:

在深入了解合并之前,我需要注意的是,对于“从远程获取最新更改”任务,有两种类似的解决方案。 更多信息请参考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

此命令不会影响您使用来自 fetchfamilyfetchpull,...)的任何命令获得的提交。

【讨论】:

    【解决方案2】:

    Git 提供了有关其功能的出色文档。对于这种情况,您需要 stach,您可以通过以下几个示例查找它: https://git-scm.com/book/en/v1/Git-Tools-Stashing

    【讨论】:

      【解决方案3】:

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

      git stash
      git pull
      git stash pop
      

      【讨论】:

      • 这不会覆盖他们所做的更改吗?我需要将我正在进行的工作与我开始以来所做的更改合并。
      • @SRobertJames Git 的设计方式是很难覆盖和意外丢失一些更改。为了能够“破坏”某些东西,您必须使用特殊的非默认键或参数。
      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多