Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 767
SRobertJames Avatar asked Nov 05 '15 01:11

SRobertJames


2 Answers

Using stash, then pull, last stash pop.

git stash
git pull
git stash pop
like image 139
xdazz Avatar answered Sep 29 '22 11:09

xdazz


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

like image 37
Victor Yarema Avatar answered Oct 03 '22 11:10

Victor Yarema