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 uncommitted 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?
Using stash
, then pull
, last 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.
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
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
, ...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With