Git Merge - 不完整、丢失的文件和文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3472804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Merge - Incomplete, Missing files and Folders
提问by Quang Van
I was trying to merge a dev branch into master.
我试图将一个开发分支合并到主分支。
git checkout master
git pull . dev
Everything seemed to go well, although there were conflicts I fixed these and commited. But when I checked this newly merged working tree is missing a lot of folders and files from dev.
一切似乎都很顺利,尽管我解决了这些冲突并提交了冲突。但是当我检查这个新合并的工作树时,它缺少来自 dev 的许多文件夹和文件。
git status // Shows conflicts & doesn't list some files/folders.
git commit -a
Created commit 55ffdd1: Merge branch 'dev' into master
git diff dev --name-status
Produces:
产生:
D folders/lm.gif
D folders/lmh.gif
...
So the files/folders that didn't show up on 'git status'. It also didn't show up at the end when I fixed the merged conflicts.
所以没有出现在“git status”上的文件/文件夹。当我修复合并的冲突时,它也没有出现在最后。
Also when I try to merge again it says:
此外,当我再次尝试合并时,它说:
git merge dev
Already up-to-date.
Yet the master branch clearly is missing files/folders from the dev branch. Why is that? Shouldn't that folder and all it's contents be added? 'folders' is being tracked on the dev branch, so shouldn't it have gotten pulled over when I did the merge?
然而,主分支显然缺少来自开发分支的文件/文件夹。这是为什么?不应该添加该文件夹及其所有内容吗?“文件夹”正在开发分支上被跟踪,所以当我进行合并时它不应该被拉过来吗?
When there was a merge conflict earlier did git stop the merge process and skipped a bunch of files/folders?
当早些时候发生合并冲突时,git 是否停止了合并过程并跳过了一堆文件/文件夹?
The dev branch had quite a lot of changes, could I have messed something up with git in the past that now certain files/folders wouldn't have merged?
dev 分支有很多变化,我是否可以在过去用 git 搞砸一些现在某些文件/文件夹不会合并的东西?
(When I first created the dev branch I didn't know what I was doing and did crazy things like reset, reverts etc.)
(当我第一次创建 dev 分支时,我不知道自己在做什么,做了一些疯狂的事情,比如重置、恢复等。)
Hoping one of you git guru's here on stack overflow knows the answer. :)
希望你们中的一位 git guru 在堆栈溢出时知道答案。:)
Thanks, Quang
谢谢,广
Answer
回答
Thanks Walter, Yes this is what happened.
谢谢沃尔特,是的,这就是发生的事情。
After doing some investigating I found out it was a little complicated what happened. It turned out that.
经过一番调查,我发现发生的事情有点复杂。事实证明。
- dev branched from master, it had all the files.
- A third branch, lets call it "cleaned", branched off of dev.
- cleaned branch deleted all the files.
- cleaned branch merged (or something?) with master. At this point the files were deleted off of master. And the 'cleaned' branch disappeared.
- on dev, the files are still there and I continued adding to this branch, editing files for quite some time.
- dev merged with master and all the files that was deleted previously by cleaned were gone.
- dev 从 master 分支,它拥有所有文件。
- 第三个分支,我们称之为“已清理”,从 dev 分支出来。
- 清理分支删除了所有文件。
- 已清理分支与 master 合并(或其他什么?)。此时,文件已从 master 中删除。“清理过的”分支消失了。
- 在 dev 上,文件仍然存在,我继续添加到这个分支,编辑文件很长一段时间。
- dev 与 master 合并,之前被cleaned 删除的所有文件都消失了。
Hope this helped someone besides me who learned quite a lot about how to use git log, git show, git reflog trying to debug what happened.
希望这能帮助除了我之外的其他人,他们学到了很多关于如何使用 git log、git show、git reflog 来调试发生的事情。
Thanks!
谢谢!
How to Fix
怎么修
So this is what I did to merge all of the contents in dev that was previously deleted back onto master.
所以这就是我将 dev 中先前删除的所有内容合并回 master 所做的工作。
- Got all the files/folders that was deleted (on dev but not on the newly merged master)
git diff dev --name-status | grep D > deleted_files
- Output list of all files and folders
git log --name-status > file_history
Gonna be using this to figure out the last updated version of the deleted file. - Go through the list of deleted_files one by one, find the the most updated version in file_history and restore it. Example:
git checkout 25b8a44 view.php
25b8a44... was the commit with the last updated version of view.php. I triedcherry-pick
and just a straightgit checkout dev view.php
but i found explicitly using the commit id, it merge more of it's history. (Including the commit that cause the file to be deleted in the first place.) - Once all the deleted files are back a quick check
git diff dev --name-status | grep D
shows that all files are copied over. Then like Walter said an ammend commitgit commit --amend
:)
- 得到了所有被删除的文件/文件夹(在 dev 上,而不是在新合并的 master 上)
git diff dev --name-status | grep D > deleted_files
- 所有文件和文件夹的输出列表
git log --name-status > file_history
将使用它来找出已删除文件的最后更新版本。 - 一一浏览deleted_files列表,在file_history中找到最新的版本并恢复。示例:
git checkout 25b8a44 view.php
25b8a44... 是 view.php 的最新更新版本的提交。我尝试过cherry-pick
,git checkout dev view.php
但我发现明确使用提交 id,它合并了更多的历史。(包括首先导致文件被删除的提交。) - 一旦所有已删除的文件恢复,快速检查
git diff dev --name-status | grep D
显示所有文件都已复制。然后就像沃尔特说的修正提交git commit --amend
:)
采纳答案by Walter Mundt
It sounds like git thinks that the missing files were deleted on the master at some point between where it branches from dev and the pre-merge head. That's the only reason I can think of for why git would silently drop tracked files during a merge.
听起来 git 认为丢失的文件在 master 上从 dev 分支到预合并头之间的某个点被删除了。这是我能想到的为什么 git 会在合并期间默默地删除跟踪文件的唯一原因。
Unfortunately, I don't know a good way to fix that. I'd probably just re-add all the deleted files from the dev branch manually (or with a little bit of bash scripting), and then to a git commit --amend
to revise the merge commit to include them.
不幸的是,我不知道解决这个问题的好方法。我可能只是手动(或使用一点 bash 脚本)从 dev 分支重新添加所有已删除的文件,然后git commit --amend
修改合并提交以包含它们。