合并分支:为什么git合并--no-ff没有合并一切?

时间:2017-11-14 21:41:56

标签: git merge gitlab

  • 第一次使用git并遇到合并问题。工作流程如下:

  • 在gitlab上创建了存储库,每个成员都在本地链接

  • 将两个文本文件添加到master分支 - recipe_book.txt和ingredients.txt
  • 编辑了两个文件并添加,提交并推送了
  • 从大师创建了两个分支 - 素食和披萨面食
  • 在pizza-pasta分支中,两个文件都被重写(不仅仅是略微修改),添加了提交和推送。
  • 在素食分支中,两个文件都被重写(不仅仅是略微修改),添加了提交和推送。
  • on master - master和pizza-pasta合并,这导致了快速合并(如下所示) - 恢复提交合并前 没有快进合并,但只有部分披萨意大利面文件的内容被添加到主文件的内容中。

此任务的目标是三个不同分支中的两个文件(文件必须保留相同的名称,但内容必须完全不同),最后必须合并所有分支,以便主分支中的文件包含最初是在三个分支中的每个分支。

使用的命令摘要(文件的编辑由每个参与者手动和本地完成):

    git add ingredients.txt
    git commit -m "adding blank file ingredients.txt"
    git add recipe_book.txt
    git commit -m "adding blank file recipe_book.txt"
    git push -u origin master
    git add ingredients.txt
    git commit -m "updating file ingredients.txt with content"
    git add recipe_book.txt
    git commit -m "updating file recipe_book.txt with content"
    git push -u origin master
    git branch vegetarian
    git push origin vegetarian
    git branch pizza-pasta
    git push origin pizza-pasta
    git checkout vegetarian
    git add ingredients.txt
    git commit -m "updating file ingredients.txt with completely new content"
    git add recipe_book.txt
    git commit -m "updating file recipe_book.txt with completely new content"
    git push -u origin vegetarian
    git checkout pizza-pasta
    git add ingredients.txt
    git commit -m "updating file ingredients.txt with completely new content"
    git add recipe_book.txt
    git commit -m "updating file recipe_book.txt with completely new content"
    git push -u origin pizza-pasta
    git checkout master
    git merge master pizza-pasta \\ here i made the fast-forward merge mistake
    git reset --hard fe6fff
    git reset --soft HEAD@{1}
    git commit -m "reverting to pre-merge"
    git push -u origin master
    git merge --no-ff master pizza-pasta \\ here i made the merge with no fast-forward 

1 个答案:

答案 0 :(得分:0)

如果素食分支通过删除/完全更改所有行来编辑recipe_book.txt,然后添加回素食行,那么合并回主人将看起来像"覆盖" (但实际上只是一个fast-forward merge)。

$ git init
Initialized empty Git repository in /code/test-git/.git/
$ touch recipe_book.txt
$ echo steak > recipe_book.txt
$ cat recipe_book.txt
steak
$ git add recipe_book.txt
$ git commit -m "steak"
[master (root-commit) 25b1f57] steak
 1 file changed, 1 insertion(+)
 create mode 100644 recipe_book.txt
$ git checkout -b veg
Switched to a new branch 'veg'
$ echo tofu > recipe_book.txt
$ cat recipe_book.txt
tofu
$ git commit -am "tofu"
[veg e765cb4] tofu
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
Switched to branch 'master'
$ cat recipe_book.txt
steak
$ git merge veg
Updating 25b1f57..e765cb4
Fast-forward
 recipe_book.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
$ cat recipe_book.txt
tofu