如何使 git merge 处理对我的工作树的未提交更改?

发布于 2024-07-17 21:01:55 字数 465 浏览 7 评论 0原文

我和一位同事目前都在主分支上工作。 我的工作树中有一些我不想提交的代码(调试语句等)。 现在,如果他对其中一些相同的文件提交更改,我无法合并它们:

$ git merge origin/master
Updating 1b8c5c6..eb44c23
error: Entry 'blah.java' not uptodate. Cannot merge.

来自颠覆背景,我习惯于当我从存储库中提取更改时自动合并我的工作树,如果存在冲突,我会自动合并工作树。手动解决它们。

我发现在 git 中执行此操作的最快方法是:

$ git stash
$ git merge origin/master
$ git stash pop

本质上,删除未提交的更改,进行合并,然后重新应用更改。 我如何告诉 merge 自动将我的工作树与我试图引入的更改合并?

A co-worker and I are both working on the master branch at the moment. I have some code in my working tree that I don't want to commit (debugging statements and the like). Now if he commits changes to some of those same files, I can't merge them:

$ git merge origin/master
Updating 1b8c5c6..eb44c23
error: Entry 'blah.java' not uptodate. Cannot merge.

Coming from a subversion background, I'm used to having my working tree automatically merged when I pull changes from the repository and if there are conflicts, I resolve them manually.

The quickest way I have found to do this in git is:

$ git stash
$ git merge origin/master
$ git stash pop

Essentially, removing my uncommitted changes, doing the merge and then re-applying the changes. How can I tell merge to automatically merge my working tree with the changes I'm trying to pull in?

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

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

发布评论

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

评论(4

逆光下的微笑 2024-07-24 21:01:55

忘记你从颠覆中学到的一切。

始终在引入外部更改之前进行提交。

想象一下,您有一个大部分工作的树 - 也许并不完美,但您正在取得一些进展。 然后你去进行合并,你引入的代码造成了严重破坏(本身就有错误,有太多冲突需要处理,等等......)。 如果你能撤销它不是很好吗?

如果你承诺,你就可以。 如果你不这样做,你只会受苦。

请记住:您提交的内容不一定是您推动的内容,但您不提交的内容很容易就会丢失。

只要做安全、简单的事情,尽早承诺并经常承诺即可。

Forget everything you ever learned from subversion.

Always commit before introducing external changes.

Imagine you had a mostly-working tree -- maybe not perfect, but you're making some progress. Then you go to do a merge and the code you're bringing in just wreaked havoc (was buggy itself, too many conflicts to deal with, etc...). Wouldn't it be nice if you could just undo that?

If you commit, you can. If you don't, you're just going to suffer.

Remember: What you commit doesn't have to be what you push, but what you don't commit you can easily lose.

Just do the safe and easy thing and commit early and commit often.

流殇 2024-07-24 21:01:55

据我所知,您能做的最好的事情就是使用 git stash 已有的功能。 我也觉得很奇怪,合并只想处理干净的树。

As far as I can tell, the best you can do is what you already have with git stash. I too find it strange that merge wants to deal only with clean trees.

眼泪淡了忧伤 2024-07-24 21:01:55
  • 如果本地工作未提交
    • 并且您引入了远程分支中不存在的全新文件:
    • 或者受本地工作影响的文件与受您需要从远程拉取的更改影响的文件零重叠:
      • 你很幸运:git pull 将“正常工作”
    • 否则:
      • 如果您的本地更改与您正在拉取的更改没有重叠:
        • git stash 可以工作:
          • git stash 保存
          • git pull
          • git stash pop
      • 如果您的本地更改与您正在拉取的更改有一些重叠:
        • git stash 将需要手动解决冲突:
          • git stash 保存
          • git pull
          • git stash pop
          • 解决合并冲突
          • git 重置
          • git stash drop
  • 如果本地工作已提交
    • 并且受您本地工作影响的文件与受
      • 你很幸运:git pull 将“正常工作”
      • 但是:由于更清晰的历史记录,git pull --rebase 将“工作得更好”
      • 没有合并提交; 您的更改将在上游更改之后提交
    • 否则:
      • git pull 需要手动解决冲突:
        • git pull
        • 解决合并冲突
        • git add FILE 为每个冲突的文件
        • git 提交
      • 由于更清晰的历史记录,

      • git pull --rebase 仍然可以“工作得更好”
        • 但是,解决合并冲突可能会困难得多

有关详细说明,请参阅:https:// happygitwithr.com/pull-tricky.html

  • If local work is uncommitted
    • And you've introduced completely new files that don’t exist in the remote branch:
    • Or the files affected by your local work have ZERO overlap with the files affected by the changes you need to pull from the remote:
      • You're in luck: git pull will "just work"
    • Otherwise:
      • If your local changes have NO overlap with changes you are pulling:
        • git stash will work:
          • git stash save
          • git pull
          • git stash pop
      • If your local changes have SOME overlap with changes you are pulling:
        • git stash will require manual conflict resolution:
          • git stash save
          • git pull
          • git stash pop
          • resolve merge conflicts
          • git reset
          • git stash drop
  • If local work is committed
    • And the files affected by your local work have ZERO overlap with the files affected by
      • You're in luck: git pull will "just work"
      • However: git pull --rebase will "work even better" because of a cleaner history
      • there is no merge commit; your changes will be committed after upstream changes
    • Otherwise:
      • git pull will require manual conflict resolution:
        • git pull
        • resolve merge conflicts
        • git add FILE for each conflicting FILE
        • git commit
      • git pull --rebase could still "work even better" because of a cleaner history
        • however, resolving merge conflicts could be much harder

For a detailed explanation, please see: https://happygitwithr.com/pull-tricky.html

四叶草在未来唯美盛开 2024-07-24 21:01:55

您无法告诉 git merge 合并对本地存储库进行了更改的文件的更改。 这可以防止您在合并失败时丢失更改。

使用 CVS 和 SVN 合并方法,如果您在更新之前没有手动复制文件并且在合并时打乱了文件,则必须手动重新编辑才能恢复到良好状态。

如果您在合并之前提交更改或隐藏它们,则一切都是可逆的。 如果合并进展不顺利,您可以尝试多种方法来解决问题,然后选择最有效的一种。

如果您确实提交了实验性或调试更改,则可以使用 git rebase 在通过 git merge 获得的提交之后移动它们,以便更轻松地删除它们或避免意外地将它们推送到存储库。

请注意,在已推送到共享存储库的分支上使用 git rebase 会给从该存储库拉取的每个人带来痛苦。

在这些情况下,我更喜欢使用 git stash,但我仅在合并更改我已编辑但未提交的文件时才使用它。

You cannot tell git merge to merge changes on files that have changes with respect to your local repository. This protects you from losing your changes on those times when a merge goes badly.

With the CVS and SVN approach to merging, if you did not manually copy your files before the update and it scrambled them on merge, you have to manually re-edit to get back to a good state.

If you either commit your changes or stash them before doing a merge, everything is reversible. If the merge does not go well you can try several ways of making it work out and go with the one that works best.

If you do commit experimental or debug changes, you might use git rebase to move them after the commits you get via git merge to make it easier to get rid of them or to avoid pushing them to a repository accidentally.

Note that using git rebase on a branch you have pushed to a shared repository will cause grief for everyone who is pulling from that repository.

I prefer to use git stash in these cases, but I only use it if the merge changes files that I have edited and not committed.

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