Git pull with unstaged changes

时间:2014-06-12 23:30:56

标签: git

当您进行非分段更改时尝试git pull将失败,表示您可以提交或存储。我想一个解决方法是git stash,git pull,然后是git stash pop。但是,有没有其他方法可以做到这一点?如果有非分段更改,我想强制git pull,但如果正在关闭的文件不覆盖修改后的文件,则? AKA。如果我有一个包含文件的回购" derp1"," derp2"," derp3"并修改" derp1"在本地,git pull将降低并覆盖除" derp1"之外的所有内容。文件。

我假设git stash + pull + stash pop已经实现了这个目标?还有更好的方法吗?

我认为如果它发生在子模块上,这也可以有所不同。

1 个答案:

答案 0 :(得分:4)

似乎最后一个版本的git做你要求的事情

请参阅以下命令:

(命令前的[foo]$表示它在foo目录中运行)

[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.

我在这里初始化了两个repos foobarbarfoo

的克隆

现在让我们模拟一个非冲突的变化

[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")

如果我们现在拉,一切顺利

[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")

但是有一个相互矛盾的变化(请记住,我们在条形图中只有file2的非正规文件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

拉动按预期失败