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

AJc*_*dez 9 git

假设我有2个分支,master并且other.

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

现在我进入master分支,将文件添加到不同的目录,然后提交它们.然后我合并other.

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

如何强制master添加文件other或以某种方式手动添加它们?

编辑卡尔:

据我所知,我做了以下工作,虽然没有出现的变化是几周之久.我刚才意识到他们今天不在.

$ 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
Run Code Online (Sandbox Code Playgroud)

我继续使用Github,文件不存在.其他文件已提交并显示,但两个文件夹没有匹配的内容.文件存在于other但不在master.澄清一下,这些文件并不新鲜.但我认为合并至少会复制文件,master如果它们不存在的话!

use*_*840 8

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

如果这看起来不方便,您可以通过创建一个临时分支(我们称之为merge_branch)、使用 合并other到它--squash、提交、合并到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
Run Code Online (Sandbox Code Playgroud)


Kar*_*ldt 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
Run Code Online (Sandbox Code Playgroud)

如果您得到不同的结果,则可能是您错过了某个步骤,或者在某处添加了额外的步骤,或者您遇到了某种您没有告诉我们的错误,例如合并冲突。我们无法知道为什么如此基本的东西对你不起作用,除非你发布你得到的确切命令和输出,就像我上面所做的那样。