gpt4 book ai didi

Git master 分支 merge 后根本没有新文件

转载 作者:太空狗 更新时间:2023-10-29 12:53:00 25 4
gpt4 key购买 nike

假设我有 2 个分支,masterother

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

现在我进入 master 分支,将文件添加到不同的目录,然后提交它们。然后我 merge 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 中。澄清一下,这些文件不是新的。但我认为如果文件不存在, merge 至少会将文件复制到 master!

最佳答案

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

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

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 master 分支 merge 后根本没有新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com