合并后,Git master分支根本没有新文件

时间:2012-11-29 02:57:41

标签: git

假设我有2个分支,masterother

我进入other分支,添加2个文件,提交并推送。

现在我进入master分支,将文件添加到其他目录,然后提交它们。然后我合并other

问题是我在other中添加的文件没有显示出来。 Git说这是最新的,但事实并非如此!文件丢失。

如何强制masterother中添加文件或以某种方式手动添加文件?

编辑卡尔:

我尽我所知做了以下事情,虽然没有出现的变化是几周之久。我刚刚意识到他们今天不在那里。

$ 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,如果它们不存在的话!

4 个答案:

答案 0 :(得分:3)

像这样:

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 :(得分:3)

有点晚了,但对于发现这个问题的其他用户,我将描述AJcodez会发生问题的情况。如果您执行git checkout master; git merge other,在此期间或之后删除master中的某些新文件(可能会忘记这一事实),然后再次执行git checkout master; git merge other,那么“新的“文件不会再出现在master中,因为它们不是新的。合并仅关注与merge-base相关的更改,这是通过两个分支可以访问的最年轻的提交。在第二次合并期间,合并基础与第一次合并期间不同,在所描述的场景中第二次合并期间的合并基础是第一次合并期间other的提示。如果从那时起other没有更改新文件,则master中的删除是最新的更改,因此删除新文件将是第二次合并后的状态。

如果这看起来不方便,你可以通过创建一个临时分支(让我们称之为merge_branch),使用other--squash合并到其中,提交,合并到{ {1}}并删除临时分支。它可能不是理想的解决方案(例如,已经解决的合并冲突可能必须再次解决),但是原来的情况可能已经是错误的结果了。这就是我在代码中的意思:

master

答案 2 :(得分:1)

我通过在master分支创建具有相同名称的空文件解决了这个问题:

假设,分支other包含一个新文件newfile.txt,该文件未以某种方式合并到master

git checkout master
touch newfile.txt
git add newfile.txt
git commit -m "create newfile.txt"
git merge other

有点脏,但有效。

答案 3 :(得分:0)

对我来说,只做一次

git merge other

还不够。在合并和

之前,我必须other分支中拉出更改
git checkout other
git pull
git checkout first

然后我能够

git merge other