繁体   English   中英

Git拉动与未分级的更改

[英]Git pull with unstaged changes

在未进行暂存的更改时尝试进行git pull将失败,并说您可以随后提交或隐藏。 我想一种解决方法是先进行git stash,git pull和git stash pop。 但是,还有其他方法可以做到这一点吗? 如果要进行未暂存的更改,我想强制执行git pull,但是当关闭的文件不覆盖已修改的文件时? 又名 如果我有一个包含文件“ derp1”,“ derp2”,“ derp3”的存储库,并在本地修改“ derp1”,则git pull将关闭并覆盖除“ derp1”文件之外的所有内容。

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

我想如果它发生在子模块上,这也可能会有所不同。

似乎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")

但是,在发生冲突的情况下(请记住,栏中有一个unstaget文件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

拉失败,按预期方式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM