Git pull with unstaged changes

git
10,062

It seems that last versions of git do what you ask

see the following commands :

(the [foo]$ before a command indicates it is run in the foo directory)

[tmp]$ git --version
git version 2.0.0
[tmp]$ git init foo
Initialized empty Git repository in /tmp/foo/.git/
[tmp]$ cd foo
[foo]$ echo hello > file1
[foo]$ echo world > file2
[foo]$ git add .
[foo]$ git commit -m "first commit"
[master (root-commit) 5f7d6b3] first commit
 2 files changed, 2 insertions(+)
 create mode 100644 file1
 create mode 100644 file2
[foo]$ cd ..
[tmp]$ git clone foo bar
Cloning into 'bar'...
done.

Here I have initialized two repos foo and bar, bar being a clone of foo

Now lets simulate a non conflicting change

[tmp]$ cd foo
[foo]$ echo Hello > file1
[foo]$ git commit -am "correct hello > Hello"
[master 183c4a5] correct hello > Hello
 1 file changed, 1 insertion(+), 1 deletion(-)
[foo]$ cd ../bar
[bar]$ echo "world !" > file2
[bar]$ git st 
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file2

no changes added to commit (use "git add" and/or "git commit -a")

If we pull now, everything goes smoothly

[bar]$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /tmp/foo
   5f7d6b3..183c4a5  master     -> origin/master
Updating 5f7d6b3..183c4a5
Fast-forward
 file1 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[bar]$ git st
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file2

no changes added to commit (use "git add" and/or "git commit -a")

However with a conflicting change (remember, we have an unstaget file file2 in bar containing only the word world)

[bar]$ cd ../foo/
[foo]$ echo World > file2
[foo]$ git commit -am "add World"
[master 7cb10c9] add World
 1 file changed, 1 insertion(+), 1 deletion(-)
[foo]$ cd ../bar/
[bar]$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /tmp/foo
   183c4a5..7cb10c9  master     -> origin/master
Updating 183c4a5..7cb10c9
error: Your local changes to the following files would be overwritten by merge:
    file2
Please, commit your changes or stash them before you can merge.
Aborting

The pull fails as expected

Share:
10,062
Peter
Author by

Peter

Updated on June 18, 2022

Comments

  • Peter
    Peter almost 2 years

    Attempting a git pull when you have unstaged changes will fail, saying you can commit or stash then. I suppose a workaround is to git stash, git pull, then git stash pop. However, is there an alternative way to do this? I would like to forcefully git pull if there are unstaged changes, but only if the files being brought down do not override the modified files? AKA. if I have a repo with the files "derp1", "derp2", "derp3" and modify "derp1" locally, a git pull will bring down and overwrite everything except the "derp1" file.

    I assume a git stash + pull + stash pop achieves this already? And is there a better way?

    I suppose this could also work differently if it occurs on a submodule.

  • Ahmed Aboelyazeed
    Ahmed Aboelyazeed almost 9 years
    This doesn't answer the OP's question
  • twotwotwo
    twotwotwo over 8 years
    Yeah--answer is about unpushed committed changes; question is about unstaged changes.
  • Fredszaq
    Fredszaq over 8 years
    Read again the answer, it IS about unstaged changes. I've added a few clarifications and changed a missleading commit message in the end