git checkout 而不覆盖数据

发布于 2024-08-02 07:49:35 字数 368 浏览 13 评论 0原文

如何git-checkout而不覆盖数据?

我运行

git checkout master

我得到

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

这很令人惊讶,因为我不知道当我git-checkout<时Git会合并/代码>。 我总是在命令 git merge new-feature 之后单独运行。 如果 Git 在结帐时合并,这显然是不必要的。

How can you git-checkout without overwriting the data?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫离伤花 2024-08-09 07:49:35

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

您可以使用 git-stash 保存更改,然后< strong>git-stash apply 来恢复它们。

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

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

You can use git-stash to save your changes then git-stash apply to restore them.

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

浅笑轻吟梦一曲 2024-08-09 07:49:35

当切换分支时(使用 git checkout),Git 对未提交的更改进行双向合并,但通常它只进行简单的(树级)合并。

除了 git-stash Karl Voigtland 的解决方案< /a>,您可以为 git checkout 提供其他选项,选择以下选项之一options:

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

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

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

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

  • Tell git to try harder to merge uncomitted changes into branch you switch to with -m / --merge option. 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 overwrite uncomitted changes, throwing away local changes with -f option. Warning: uncomitted changes will be lost!

始于初秋 2024-08-09 07:49:35

您可以执行 git reset --soft 以使您的 HEAD 指向新分支,但保留所有文件不变(包括在新分行)。然后,您可以使用 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.

You can do a git reset --soft to make your HEAD point 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 checkout to checkout just the files that you really want from the new branch.

       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.
踏雪无痕 2024-08-09 07:49:35

Jakub 还提到了 git checkout --merge,但在撰写本文时,我不推荐此选项。就我而言,它失败并出现“错误:脏索引:无法合并”并销毁了我的整个工作副本,而没有将其备份到任何存储或类似的东西中。不要使用它,而是使用传统的储藏室。

Jakub has also mentioned git checkout --merge, but at the time of writing, I disrecommend this option. In my case, it failed with an "error: Dirty index: cannot merge" and destroyed my entire working copy, without backing it up in any stash or something like this. Don't use it, rather use a traditional stash instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文