Git 主分支在合并后没有新文件

14
假设我有2个分支,masterother。我进入other分支,添加了2个文件,并提交并推送到远程仓库。现在我切换到master分支,在不同的目录下添加了文件并提交它们。然后我合并了other分支。问题是我在other中添加的文件没有显示出来。Git显示已经是最新的,但实际上缺少文件。我该如何强制master添加other中的文件或手动添加它们? 编辑Karl: 据我所知,我已经按照上述步骤操作了,尽管未显示的更改已经存在几周了。我今天才意识到他们不存在。
$ git branch
*other
master
$ git add .
$ git commit -m 'cool new features'
$ git push origin other
$ git checkout master
$ git merge other
$ git add .
$ git commit -m 'merged cool new features from other'
$ git push origin master

我去Github上查看,但是文件不在那里。其他的文件已经被提交并显示出来了,但是两个文件夹的内容不匹配。这些文件存在于other中,但不存在于master中。需要澄清的是,这些文件并不是新的。但是我认为合并后至少会将文件复制到master中,如果它们不存在的话!


你好,你确定你的文件已经提交了吗?在合并过程中,Git不会将它们隐藏起来。你能把你执行的所有命令都发一下吗?这样可以帮助找出问题所在。 - Simon Boudrias
git add .,git commit -m 'something',git push,git checkout,git merge。这些命令可能已经被使用多次,并且顺序也可能不同。 - AJcodez
如果你在other分支中执行git status会发生什么? - Simon Boudrias
在分支master中,执行git branch --merged命令,如果没有列出other分支,则表示other分支尚未与master合并。 - Karthik Bose
git branch --merged 显示 *其他,正如预期的一样。 git status 显示没有要提交的内容(工作目录干净)。 - AJcodez
1
我认为这篇Stack Overflow的帖子中提到了其中一个原因。 - user3290525
4个回答

15
有些晚了,但是对于其他用户遇到这个问题时,我将描述一下AJcodez所发现的问题。如果你在git checkout master; git merge other期间或之后删除了一些master中的新文件(可能忘记了这一事实),之后再次执行git checkout master; git merge other,那么“新”文件将不会重新出现在master中,因为它们不是新文件。合并只关心相对于合并基础的更改,合并基础是两个分支都可以到达的最年轻提交。在第二次合并期间,合并基础与第一次合并期间不同,在上述情况下,第二次合并期间的合并基础是第一次合并期间other提交的末尾。如果other此后没有更改新文件,则master中的删除是最新的更改,因此删除新文件后的状态将成为第二次合并后的状态。
如果这似乎不方便,您可以通过创建一个临时分支(我们称其为merge_branch),使用--squashother合并到其中,提交,将其合并到master中,并删除该临时分支来绕过此问题。这可能不是理想的解决方案(例如,已处理的合并冲突可能必须再次处理),但是原始情况可能已经是错误的结果。以下是代码示例:
git log other # find the commit where 'other' originally branched off
git checkout -b merge_branch COMMIT_ID # go there & create branch
# without '--squash', the following merge would simply be a fast-forward
# merge and wouldn't solve our problem, because 'merge_branch' would then
# point to the same commit as 'other' and so the later merge into
# 'master' would produce the same unsatisfactory result.
git merge --squash other # does not create a new commit by itself
git commit # better add an explanation for what you did and why
# 'merge_branch' now contains everything you did in 'other' since it
# branched off from 'master', but squashed into a single new commit
git checkout master
git merge merge_branch
git branch --delete merge_branch

兄弟,你太专业了,对Git的工作原理了如指掌!救了我的一天,虽然我还不能完全理解你所说的内容,但我知道这是真的,而且这是一个非常难以捕捉和解决的问题!!! - pinpinokio
1
不客气。如果你想知道更多,我建议阅读git-merge-manpage。我自己在阅读那个手册并进行了大量实验之前也无法理解这个问题。 - user2845840
也许有点晚了,但解决的快速方法很简单:1)删除分支2)从服务器检出。这样你就可以重新获取那些文件了。 - Eli Fry

4

像这样:

karl@Bielefeldt-Server:~/stackoverflow$ git init .
Initialized empty Git repository in /home/karl/stackoverflow/.git/
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit common files"
[master (root-commit) 89a5cd0] commit common files
 0 files changed
 create mode 100644 common_file_a
 create mode 100644 common_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git checkout -b other
Switched to a new branch 'other'
karl@Bielefeldt-Server:~/stackoverflow$ mkdir other
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit other files"
[other 9c7409c] commit other files
 0 files changed
 create mode 100644 other/other_file_a
 create mode 100644 other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git checkout master
Switched to branch 'master'
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit master files"
[master 3558768] commit master files
 0 files changed
 create mode 100644 master_file_a
 create mode 100644 master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git merge other
Merge made by the 'recursive' strategy.
 0 files changed
 create mode 100644 other/other_file_a
 create mode 100644 other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b  other
karl@Bielefeldt-Server:~/stackoverflow$ ls other
other_file_a  other_file_b

如果你得到不同的结果,可能是由于你漏掉了某个步骤,或者在某个地方多做了一步,或者你遇到了一些错误,但你没有告诉我们,比如合并冲突。除非你像我上面那样发布 确切 的命令和输出,否则我们无法知道为什么这么基本的东西对你不起作用。


1
发生了这样的事情,只有做一个


git merge other

这还不够。在合并之前,我需要从other分支拉取更改

git checkout other
git pull
git checkout first

然后我就能够

git merge other

1
我通过在主分支创建同名的空文件来解决了这个问题:
假设分支“other”包含一个新文件newfile.txt,但由于某种原因没有与“master”合并。
git checkout master
touch newfile.txt
git add newfile.txt
git commit -m "create newfile.txt"
git merge other

这有点脏,但是能够运行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接