How can I pull and merge with my uncommited changes?

While working with some uncommitted files, I need to pull new code. There's a conflict, so git refuses to pull:

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 uncommited changes? 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 . 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?


使用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. I prefer the rebase since it doesn't produce redundant merge commits.

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.

Answer 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

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 , ...).


Git provides excellent documentation on their functions. 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

链接地址: http://www.djcxy.com/p/45116.html

上一篇: git pull是否总是创建一个合并提交?

下一篇: 我怎样才能将我的无法改变的变化拉上并合并起来?