【问题标题】:Git master branch simply does not have new files after mergeGit master 分支合并后根本没有新文件
【发布时间】:2012-11-29 02:57:40
【问题描述】:

假设我有 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 没有理由在合并期间将它们隐藏起来。您可以发布您所做的所有命令吗? (这将有助于找出问题)
  • git add ., git commit -m 'something', git push, git checkout, git merge。可能现在和很多次都在每个订单中
  • 如果你在other 分支中执行git status 会发生什么?
  • master分支中,执行git branch --merged,如果没有列出other分支,则other分支不会与master合并。
  • 我认为this SO post 有它发生的原因之一。

标签: git


【解决方案1】:

有点晚了,但是对于发现这个问题的其他用户,我将描述一个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 的工作原理!拯救我的一天,我仍然无法完全理解你在说什么,只看到它是真的,这是一个很难抓住然后解决的问题!!!
  • 不客气。如果您想了解更多信息,我建议您阅读 git-merge-manpage。在阅读该手册页并进行大量实验之前,我自己无法理解这个问题。
【解决方案2】:

像这样:

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

如果您得到不同的结果,您要么错过了一个步骤,在某处放置了一个额外的步骤,要么您遇到了某种您没有告诉我们的错误,例如合并冲突。我们无法知道为什么这么基本的东西不适合你,除非你发布 exact 命令和你得到的输出,就像我在上面所做的那样。

【讨论】:

    【解决方案3】:

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

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

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

    这有点脏,但可以。

    【讨论】:

      【解决方案4】:

      碰巧我只做一个

      git merge other
      

      还不够。在合并之前,我必须other 分支中提取更改

      git checkout other
      git pull
      git checkout first
      

      然后我就可以了

      git merge other
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-14
        • 2013-09-23
        • 1970-01-01
        • 2016-04-30
        • 2016-07-03
        • 2016-09-20
        • 2020-07-16
        • 1970-01-01
        相关资源
        最近更新 更多