【Git Snippets】Disable fast forward merging by default

331 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

The Git snippet collection contains a variety of short tips and tricks for all currently maintained versions of git. It includes most commonly-used commands and covers various use-cases in the form of simplified documentation, complete with multiple examples.

Short code snippets for all your development needs.

Target: Disables the default fast forwarding on merge commits.

Use git config --add merge.ff false to disable fast-forward merging for all branches, even if it is possible.

You can use the --global flag to configure this option globally.

$ git config [--global] --add merge.ff false

Example

$ git config --global --add merge.ff false

$ git checkout my-branch
$ git merge master

Now, merge operation will never fast forward even if it's possible!

Former merge (Fast-forward)

$ git-log
* a8c017f (HEAD -> master) update on 2021-10-09
* 3a6eba4 (origin/master) 2021年10月 9日  8:47:28
* 4975bc2 update
* ceecd3d hello git cm
*   fc60abe Merge remote-tracking branch 'origin'
|\
| * 38c5cfb users
* | dd7a2ca account
|/
* f6f067d add .gitignore.
* e990479 add remote.txt at gitee
* 620fea2 (origin/main, dev2) update on dev2
* fa343b7 comment

$ git checkout dev1
Switched to branch 'dev1'

$ git merge master
Updating 7163b62..a8c017f
Fast-forward
 .gitignore | 0
 dev2.txt   | 0
 readme.txt | 8 +++++---
 remote.txt | 1 +
 4 files changed, 6 insertions(+), 3 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 dev2.txt
 create mode 100644 remote.txt
 
$ git-log
* a8c017f (HEAD -> dev1, master) update on 2021-10-09
* 3a6eba4 (origin/master) 2021年10月 9日  8:47:28
* 4975bc2 update
* ceecd3d hello git cm
*   fc60abe Merge remote-tracking branch 'origin'
|\
| * 38c5cfb users
* | dd7a2ca account
|/
* f6f067d add .gitignore.
* e990479 add remote.txt at gitee
* 620fea2 (origin/main, dev2) update on dev2
* fa343b7 comment

Configured merge (Merge made by the 'recursive' strategy)

$ git-log
* a8c017f (HEAD -> dev1, master) update on 2021-10-09
* 3a6eba4 (origin/master) 2021年10月 9日  8:47:28
* 4975bc2 update
* ceecd3d hello git cm
*   fc60abe Merge remote-tracking branch 'origin'
|\
| * 38c5cfb users
* | dd7a2ca account
|/
* f6f067d add .gitignore.
* e990479 add remote.txt at gitee
* 620fea2 (origin/main, dev2) update on dev2
* fa343b7 comment

$ git checkout dev2
Switched to branch 'dev2'
Your branch is up to date with 'origin/main'.

$ git merge master
Merge made by the 'recursive' strategy.
 .gitignore | 0
 remote.txt | 1 +
 2 files changed, 1 insertion(+)
 create mode 100644 .gitignore
 create mode 100644 remote.txt

$ git-log
*   270d59c (HEAD -> dev2) Merge branch 'master' into dev2
|\
| * a8c017f (master, dev1) update on 2021-10-09
| * 3a6eba4 (origin/master) 2021年10月 9日  8:47:28
| * 4975bc2 update
| * ceecd3d hello git cm
| *   fc60abe Merge remote-tracking branch 'origin'
| |\
| | * 38c5cfb users
| * | dd7a2ca account
| |/
| * f6f067d add .gitignore.
| * e990479 add remote.txt at gitee
|/
* 620fea2 (origin/main) update on dev2
* fa343b7 comment

Now, do you clear the difference of 'Fast-forward' and 'recursive' strategy ?

🌊Wish you can benefit from this article.
🍺And welcome to leave your thoughts in the comments section that we can discuss and share ideas with each other.