git checkout 自动合并本地修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12822727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:36:36  来源:igfitidea点击:

git checkout automatically merges local modifications

gitmergegit-checkout

提问by Razer

I tried following commands on the shell

我尝试在 shell 上执行以下命令

git init

echo "test1" > test1.txt
git add test1.txt
git commit -a -m "test1"

echo "test2" >> test1.txt

git branch test
git checkout test

text.txtnow contains:

text.txt现在包含:

test1
test2

After checkout to branch testall local modifications from masterget merged.

结帐后将test所有本地修改从master合并中分支出来。

Why?

为什么?

I expected that gitrefuses checkout to testbecause of the local changes. I expected that git asks for a commit or stashthe local changes.

我预计由于本地更改而git拒绝结帐test。我希望 git 要求提交或stash本地更改。

Edit: I used a bash script to execute this commands. I get following output:

编辑:我使用 bash 脚本来执行此命令。我得到以下输出:

r@r:/tmp/test$ ./createrepo 
Initialized empty Git repository in /tmp/test/.git/
[master (root-commit) 0407f5b] test1
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
M       test1.txt
Switched to branch 'test'

回答by twalberg

gittries hard to not lose possibly valuable data. In this case, it's not actually merging branches, as the changes in question have not been committed. Rather, when you do a git checkout, it attempts to preserve newly made but not committed yet modifications, so it checks out the commit you are requesting, and adds your uncommitted changes. If you really want to discard the uncommitted changes, use git checkout -f, or git checkoutfollowed by git reset --hard HEAD. Sometimes, if the changes you have not committed yet can't be merged into what you're checking out cleanly, you'll get an error message and the checkout will fail.

git努力不丢失可能有价值的数据。在这种情况下,它实际上并不是合并分支,因为有问题的更改尚未提交。相反,当您执行 a 时git checkout,它会尝试保留新创建但尚未提交的修改,因此它会检查您请求的提交,并添加您未提交的更改。如果您真的想放弃未提交的更改,请使用git checkout -f, 或git checkout后跟git reset --hard HEAD。有时,如果您尚未提交的更改无法完全合并到您要检出的内容中,您将收到一条错误消息并且检出将失败。

回答by Lucia Pasarin

Have you used any option with git checkout command? I ask because the behaviour you are describing seems like when "git checkout -f" is used.

您是否在 git checkout 命令中使用过任何选项?我问是因为您所描述的行为似乎在使用“git checkout -f”时。

回答by Mars

git checkoutreplaceslocally changed files.

git checkout替换本地更改的文件。

git mergemergeslocal & incoming changes, alerting to any conflicts.

git merge合并本地和传入的更改,提醒任何冲突。

Neither if these commands will remove local files that do not exist in the incoming commit.

这些命令是否会删除传入提交中不存在的本地文件。

Only git reset --hardwill completely wipe out local changes.

只会git reset --hard彻底抹去局部变化。