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.
Cannot pull with rebase: You have unstaged changes. Please commit or stash them. Git stash command is kind of commit stored changes to temporary space and clean working copy. Then your working copy will be reverted to HEAD.It is a good place to store uncommitted changes.
You'll often want to do two things: change the commit message, or change the actual content of the commit by adding, removing and modifying files. error: cannot rebase: You have unstaged changes.
To retrieve changes out of the stash and apply them to the current branch you're on, you have two options: git stash apply STASH-NAME applies the changes and leaves a copy in the stash. git stash pop STASH-NAME applies the changes and removes the files from the stash.
Use git fetch to get all available branches. Then, git pull origin <branch> to get latest changes.
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
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