在不覆盖数据的情况下进行 git checkout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1291459/
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-10 06:54:24  来源:igfitidea点击:

To git checkout without overwriting data

gitgit-mergegit-checkout

提问by Léo Léopold Hertz ??

How can you git-checkoutwithout overwriting the data?

你怎么能git-checkout不覆盖数据?

I run

我跑

 git checkout master

I get

我得到

error: Entry 'forms/answer.php' would be overwritten by merge. Cannot merge.

This is surprising, since I did not know that Git merges when I git-checkout. I have always run after the command separately git merge new-feature. This seems to be apparently unnecessary if Git merges at checkout.

这很令人惊讶,因为我不知道 Git 在我git-checkout. 我一直在单独运行命令git merge new-feature。如果 Git 在结帐时合并,这显然是不必要的。

回答by Karl Voigtland

Git is warning you that forms/answers.php has changes in your working copy or index that have not been committed.

Git 警告您, forms/answers.php 在您的工作副本或索引中有尚未提交的更改。

You can use git-stashto save your changes then git-stash applyto restore them.

你可以使用git-stash来保存你的更改,然后git-stash apply来恢复它们。

The common use case of git-stashis that you are working on changes but then must temporarily checkout a different branch to make a bug fix. So you can stashyour changes in your index and working copy, checkout the other branch, make the bug fix, commit, checkout the original branch, and git-stash applyto restore your changes and pick-up where you left off.

git-stash的常见用例是您正在处理更改,但必须临时检出不同的分支以修复错误。因此,您可以将更改存储在索引和工作副本中,签出另一个分支,修复错误,提交,签出原始分支,然后git-stash 应用来恢复更改并从中断的地方继续。

回答by Jakub Nar?bski

Git does a 2-way merge of uncomitted changeswhen switching branches (using git checkout <branch>), but ordinarily it does only trivial (tree-level) merge.

Git在切换分支时(使用)对未提交的更改进行 2 路合并git checkout <branch>,但通常它只进行微不足道的(树级)合并。

Besides git-stashsolution by Karl Voigtland, you can give additional options to git checkout, choosing one of the following options:

除了git-stashKarl Voigtland 的解决方案,您还可以为git checkout提供其他选项,选择以下选项之一:

  • Tell git to try harder to mergeuncomitted changes into branch you switch to with -m/ --mergeoption. With this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

  • Tell git to overwriteuncomitted changes, throwing away local changes with -foption. Warning: uncomitted changes will be lost!

  • 告诉 git更加努力地将未提交的更改合并到您使用-m/--merge选项切换到的分支中。使用此选项,当前分支、您的工作树内容和新分支之间的三向合并完成,您将在新分支上。

  • 告诉 git覆盖未提交的更改,使用-f选项丢弃本地更改。警告:未提交的更改将丢失!

回答by Evgeny

You can do a git reset --softto make your HEADpoint to the new branch, but leave all the files as they are (including the ones that were changed in the new branch). Then you can use git checkoutto checkout just the files that you really want from the new branch.

您可以执行 agit reset --softHEAD指向新分支,但保留所有文件原样(包括在新分支中更改的文件)。然后,您可以使用git checkout从新分支中检出您真正想要的文件。

       git reset [<mode>] [<commit>]
           This form resets the current branch head to <commit> and possibly updates the index (resetting it to the
           tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted, defaults to --mixed.
           The <mode> must be one of the following:

           --soft
               Does not touch the index file or the working tree at all (but resets the head to <commit>, just like
               all modes do). This leaves all your changed files "Changes to be committed", as git status would put
               it.