Git忽略合并时删除的文件 [英] Git ignores deleted file on merge

查看:820
本文介绍了Git忽略合并时删除的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个存储库。有时,我想将其他的内容合并到 main 中。但是,合并忽略已删除的文件。让我通过一个例子来解释它:

  mkdir -p test / main test / other 

cd测试/其他/
git init
触摸一个两三
git add。
git commit -m加一,二和三。

cd ../main/
git init
触摸四个
git add。
git commit -m加四。

其他添加到 main as remote。

  git remote add other ../other/ 
git取其他

合并其内容。

  git merge --squash other / master 
git commit -m合并其他。

正确添加文件。现在,移除其他中的文件。

  cd ../other / 
git rm two
git commit -m删除两个。

将变更合并为 main

  cd ../main/ 
git fetch other
git merge --squash other / master

合并后 git status 说:

 #在分支master上
没有提交(工作目录干净)

我希望合并删除 two ,因为它已在其他 code>。我在做什么错了?

解决方案

这里的问题是您使用压扁提交。



当您执行 merge --squash 时,您将放弃分支的所有历史记录。你还没有真正合并分支 - 你只是应用了其历史的简明表述。因此,如果您稍后执行 merge --squash ,git将重新应用分支历史记录中的所有提交(因为两个分支没有共同的祖先)。



执行第一个 合并--squash 时,您创建一个提交包含创建一个,两个和三个的 main >。因此, main 的历史首先是创建四个,然后是创建一个,两个和三个。

当你执行第二个 merge --squash 时,添加一个提交,其中包含(实际上)创建一个,两个和三个以及删除两个。这两个提交在一起(压扁!)的网络是创建一个和三个。所以git自动合并创建一个和三个提交与您当前的回购状态 - 留下四个文件存在。内容合并会自动成功,因为文件one和three在双方都是相同的。



您应该使用真实合并或樱桃采摘而不是压扁如果你想保持一个回购与远程的最新。 合并--squash 与通过远程存储库中的所有新提交进行播放并将其集成到此处不同。


I have two repositories. From time to time, I want to merge the content of other into main. However the merge ignores deleted files. Let me explain it through an example:

mkdir -p test/main test/other

cd test/other/
git init
touch one two three
git add .
git commit -m "Add one, two and three."

cd ../main/
git init
touch four
git add .
git commit -m "Add four."

Add other to main as remote.

git remote add other ../other/
git fetch other

Merge its content.

git merge --squash other/master
git commit -m "Merge other."

It adds the files correctly. Now, remove a file in other.

cd ../other/
git rm two
git commit -m "Remove two."

Merge changes into main.

cd ../main/
git fetch other
git merge --squash other/master

After the merge git status says:

# On branch master
nothing to commit (working directory clean)

I would expect the merge to delete two, as it was deleted in other. What am I doing wrong?

解决方案

The issue here is your use of squash commits.

When you do a merge --squash, you abandon all of the history of a branch. You haven't really "merged the branch" - you've just applied a condensed representation of its history. So if you do a later merge --squash, git will reapply all the commits in the branch's history (since the two branches have no common ancestor).

When you perform the first merge --squash, you create a commit on main which contains "Create one, two, and three". So the history of main is first "create four", then "create one, two, and three".

When you do the second merge --squash, you add a commit which consists of (in effect) "Create one, two, and three" plus "Remove two". The net of those two commits together (squashed!) is "Create one and three". So git auto-merges the "Create one and three" commit with your current repo state - leaving you with four files present. The content merge succeeds automatically because the files "one" and "three" are identical on both sides.

You should use "real" merges or cherry-picking instead of squashing if you want to keep a repo up-to-date with a remote. A merge --squash is not the same thing as "play through all the new commits in a remote repository and integrate them here".

这篇关于Git忽略合并时删除的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆