15

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SRobertJames
  • 8,210
  • 14
  • 60
  • 107

3 Answers3

26

Using stash, then pull, last stash pop.

git stash
git pull
git stash pop
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Won't that override the changes _they_ made? I need to merge in my work in progress to the changes that have been made since I started. – SRobertJames Nov 05 '15 at 15:05
  • @SRobertJames Git is designed the way where it is very hard to overwrite and accidentally loose some changes. To be able to "break" something you will have to use special NON default keys or parameters. – Victor Yarema Nov 07 '15 at 13:59
2

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

Community
  • 1
  • 1
Victor Yarema
  • 1,183
  • 13
  • 15
-1

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

PeCosta
  • 537
  • 4
  • 13