我遇到的情况是,在功能分支中所做的某些更改并未反射(reflect)在主分支中,即使该分支已 merge 到主分支中。我不知道为什么。为简单起见,假设此提交具有哈希“A”和更改的文件“file”
这可能最好通过以下命令来说明:
$ git checkout master
$ git branch --contains A
* master
feature_branch
$ git log file | grep A
(no output)
$ git checkout feature_branch
$ git log file | grep A
A
谁能解释一下这是怎么回事?更重要的是,将来有什么办法可以防止这种情况发生吗?
编辑:
正如一些人所提到的,以下确实显示了提交:
$ git checkout master
$ git log --follow file | grep A
A
但问题是...该文件未重命名。所以这也不能完全解释事情......
最佳答案
您是恶意 merge 的受害者。
这是重现它的方法
git init testrepo
cd testrepo
touch initial
git add initial
git commit -m 'initial commit'
git checkout -b feature_branch
echo "A" >> file
git add file
git commit -m 'file committed'
git checkout master
现在进行交互式 merge ,就好像存在 merge 冲突一样
git merge --no-commit --no-ff feature_branch
并移动文件 file
(恶意 merge )。
testrepo (master|MERGING)
git mv file someOtherFile
git commit
现在您将看到分支 master 包含引入文件 file
9469682
)
git branch --contains 9469682
feature_branch
* master
但是 git log 不会显示它,因为它被移动了
git log -- file
(no output)
使用
git log --follow -- file
提交再次出现。
同时请记住, merge 会带来更多的邪恶。如果 file
的内容也发生了很大变化,即使 git log --follow
也不会检测到它,因为重命名阈值。
在这种情况下使用 git log --follow --find-renames=
调整重命名阈值。
If generating diffs, detect and report renames for each commit. For following files across renames while traversing history, see --follow. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file’s size). For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed. Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.
关于Git "missing"提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990752/