简体   繁体   English

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

[英]git - How can I pull and merge with my uncommitted changes?

While working with some uncommitted files, I need to pull new code. 在处理一些未提交的文件时,我需要提取新代码。 There's a conflict, so git refuses to pull: 有冲突,所以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.

Question 1: How can I pull and merge with my uncommitted changes? 问题1:如何提取和合并我未提交的更改? I need to keep on working, I'm not ready to commit, but I want the external code? 我需要继续工作,我还没准备好提交,但我想要外部代码?

Question 2: I ended up doing a stash followed by a pull . 问题2:我最后做了一个stash然后pull How do I now merge in my changes to the new pull? 我现在如何将我的更改合并到新拉? How do I apply my stash without clobbering the new changes of the pull? 如何在不破坏拉动的新变化的情况下应用我的藏品?

Using stash , then pull , last stash pop . 使用stash ,然后pull ,最后一个stash pop

git stash
git pull
git stash pop

Before going deeper into merges I need to pay your attention that there are two similar solutions for the task "get latest changes from remote". 在深入合并之前,我需要注意有两个类似的解决方案,用于“从远程获取最新更改”的任务。 For more information please refer to git pull VS git fetch git rebase . 有关更多信息,请参阅git pull VS git fetch git rebase I prefer the rebase since it doesn't produce redundant merge commits. 我更喜欢rebase,因为它不会产生冗余的合并提交。

Do not be afraid to make a commit. 不要害怕做出承诺。 You can easily do anything you like with it (modify it with git commit --amend , discard it and pop all changes into worktree with git reset HEAD~1 ) until you push it anywhere. 你可以轻松地用它做任何你喜欢的事情(用git commit --amend修改它,丢弃它并用git reset HEAD~1所有更改弹出到worktree),直到你把它推到任何地方。

Answer 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

Answer 2 答案2

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

This command doesn't affect commits that you get using any command from fetch family ( fetch , pull , ...). 此命令不会影响使用fetch familyfetchpull ,...)中的任何命令获得的提交。

Git provides excellent documentation on their functions. Git提供了有关其功能的出色文档。 For this case you would need stach, you can look it up with several examples at: https://git-scm.com/book/en/v1/Git-Tools-Stashing 对于这种情况,你需要stach,你可以在以下几个例子中查找: https ://git-scm.com/book/en/v1/Git-Tools-Stashing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM