git merge和git merge --no-ff有什么区别?

本文翻译自:What is the difference between `git merge` and `git merge --no-ff`?

Using gitk log , I could not spot a difference between the two. 使用gitk log ,我无法发现两者之间的差异。 How can I observe the difference (with a git command or some tool)? 如何观察差异(使用git命令或某些工具)?


#1楼

参考:https://stackoom.com/question/c3HB/git-merge和git-merge-no-ff有什么区别


#2楼

Graphic answer to this question 这个问题的图形答案

Here is a site with a clear explanation and graphical illustration of using git merge --no-ff : 这是一个使用git merge --no-ff的清晰解释和图形说明的站点

git merge --no-ff和git merge之间的区别

Until I saw this, I was completely lost with git. 直到我看到这个,我对git完全迷失了。 Using --no-ff allows someone reviewing history to clearly see the branch you checked out to work on. 使用--no-ff可以使查看历史记录的人清楚地看到您签出的分支以进行工作。 (that link points to github's "network" visualization tool) And here is another great reference with illustrations. (该链接指向github的“网络”可视化工具),这是另一个带有插图的出色参考 This reference complements the first one nicely with more of a focus on those less acquainted with git. 该参考文献很好地补充了第一个参考文献,更多地侧重于那些不熟悉git的读者。


Basic info for newbs like me 像我这样的新手的基本信息

If you are like me, and not a Git-guru, my answer here describes handling the deletion of files from git's tracking without deleting them from the local filesystem, which seems poorly documented but often occurrence. 如果您像我一样,而不是Git-guru,那么我在这里的答案描述了如何处理git跟踪中的文件删除操作,而不是从本地文件系统中删除文件,这似乎文献记载不多,但经常发生。 Another newb situation is getting current code , which still manages to elude me. 另一个新情况是获取当前代码 ,但仍然设法使我难以理解。


Example Workflow 工作流程示例

I updated a package to my website and had to go back to my notes to see my workflow; 我将软件包更新到了​​我的网站,不得不回到笔记中才能看到我的工作流程; I thought it useful to add an example to this answer. 我认为向该答案添加示例非常有用。

My workflow of git commands: 我的git命令工作流程:

git checkout -b contact-form
(do your work on "contact-form")
git status
git commit -am  "updated form in contact module"
git checkout master
git merge --no-ff contact-form
git branch -d contact-form
git push origin master

Below: actual usage, including explanations. 下图:实际用法,包括说明。
Note: the output below is snipped; 注意:以下输出被截断; git is quite verbose. git非常冗长。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   ecc/Desktop.php
#       modified:   ecc/Mobile.php
#       deleted:    ecc/ecc-config.php
#       modified:   ecc/readme.txt
#       modified:   ecc/test.php
#       deleted:    passthru-adapter.igs
#       deleted:    shop/mickey/index.php
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ecc/upgrade.php
#       ecc/webgility-config.php
#       ecc/webgility-config.php.bak
#       ecc/webgility-magento.php

Notice 3 things from above: 注意上面的三件事:
1) In the output you can see the changes from the ECC package's upgrade, including the addition of new files. 1)在输出中,您可以看到ECC软件包升级后的更改,包括添加新文件。
2) Also notice there are two files (not in the /ecc folder) I deleted independent of this change. 2)还要注意,我删除了两个文件(不在/ecc文件夹中),与此更改无关。 Instead of confusing those file deletions with ecc , I'll make a different cleanup branch later to reflect those files' deletion. ecc将这些文件删除与ecc混淆,我稍后将创建一个不同的cleanup分支以反映这些文件的删除。
3) I didn't follow my workflow! 3)我没有遵循我的工作流程! I forgot about git while I was trying to get ecc working again. 我在尝试再次使ecc工作时忘记了git。

Below: rather than do the all-inclusive git commit -am "updated ecc package" I normally would, I only wanted to add the files in the /ecc folder. 下图:我通常不会将包罗万象的git commit -am "updated ecc package" ,而是只想将文件添加到/ecc文件夹中。 Those deleted files weren't specifically part of my git add , but because they already were tracked in git, I need to remove them from this branch's commit: 这些删除的文件不是我git add一部分,但是由于已经在git中对其进行了跟踪,因此我需要从该分支的提交中删除它们:

$ git checkout -b ecc
$ git add ecc/*
$ git reset HEAD passthru-adapter.igs
$ git reset HEAD shop/mickey/index.php
Unstaged changes after reset:
M       passthru-adapter.igs
M       shop/mickey/index.php

$ git commit -m "Webgility ecc desktop connector files; integrates with Quickbooks"

$ git checkout master
D       passthru-adapter.igs
D       shop/mickey/index.php
Switched to branch 'master'
$ git merge --no-ff ecc
$ git branch -d ecc
Deleted branch ecc (was 98269a2).
$ git push origin master
Counting objects: 22, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (14/14), 59.00 KiB, done.
Total 14 (delta 10), reused 0 (delta 0)
To git@github.com:me/mywebsite.git
   8a0d9ec..333eff5  master -> master



Script for automating the above 自动化上述脚本

Having used this process 10+ times in a day, I have taken to writing batch scripts to execute the commands, so I made an almost-proper git_update.sh <branch> <"commit message"> script for doing the above steps. 在一天中使用此过程十次以上之后,我开始编写批处理脚本来执行命令,因此我制作了几乎正确的git_update.sh <branch> <"commit message">脚本来完成上述步骤。 Here is the Gist source for that script. 这是该脚本的要点源

Instead of git commit -am I am selecting files from the "modified" list produced via git status and then pasting those in this script. 我不是从git commit -am而是从通过git status生成的“已修改”列表中选择文件,然后将其粘贴到此脚本中。 This came about because I made dozens of edits but wanted varied branch names to help group the changes. 发生这种情况是因为我进行了数十次编辑,但希望使用不同的分支名称来帮助对更改进行分组。


#3楼

The --no-ff option ensures that a fast forward merge will not happen, and that a new commit object will always be created . --no-ff选项确保不会发生快速向前合并,并且始终会创建一个新的提交对象 This can be desirable if you want git to maintain a history of feature branches. 如果您希望git维护功能分支的历史记录,则可能需要这样做。 git merge --no-ff和git mergeIn the above image, the left side is an example of the git history after using git merge --no-ff and the right side is an example of using git merge where an ff merge was possible. 在上图中,左侧是使用git merge --no-ff之后的git历史的示例,右侧是使用ff merge可以使用git merge的示例。

EDIT : A previous version of this image indicated only a single parent for the merge commit. 编辑 :此图像的先前版本仅指示合并提交的单亲。 Merge commits have multiple parent commits which git uses to maintain a history of the "feature branch" and of the original branch. 合并提交有多个父提交 ,git用来维护“功能分支”和原始分支的历史记录。 The multiple parent links are highlighted in green. 多个父链接以绿色突出显示。


#4楼

这是一个古老的问题,在其他帖子中也对此进行了微妙的提及,但对我而言,单击此按钮的原因是, 非快进合并将需要单独的commit


#5楼

Merge Strategies 合并策略

Explicit Merge : Creates a new merge commit. 显式合并 :创建一个新的合并提交。 (This is what you will get if you used --no-ff .) (如果使用--no-ff这将是您得到的。)

在此处输入图片说明

Fast Forward Merge: Forward rapidly, without creating a new commit: 快速转发合并:快速转发 ,而无需创建新的提交:

在此处输入图片说明

Rebase : Establish a new base level: 重新设定 :建立新的基本等级:

在此处输入图片说明

Squash: Crush or squeeze (something) with force so that it becomes flat: 壁球:挤压或挤(东西)与力,使其变得平坦:

在此处输入图片说明


#6楼

The --no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward. --no-ff标志使合并始终创建一个新的提交对象,即使合并可以通过快进来执行。 This avoids losing information about the historical existence of a feature branch and groups together all commits that together added the feature 这样可以避免丢失有关要素分支历史存在的信息,并将所有添加了要素的提交分组在一起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值