Skip to content

Instantly share code, notes, and snippets.

@albertpark
Last active August 5, 2022 04:35
Show Gist options
  • Save albertpark/119025f32ef4e036f7bee3e8d045ceea to your computer and use it in GitHub Desktop.
Save albertpark/119025f32ef4e036f7bee3e8d045ceea to your computer and use it in GitHub Desktop.

Git Merge To FF Or Not To FF

Created May 13, 2020

Intro

In git merge there are several ways to merge branches however, the most commonly used options are --ff-only and --no-ff. The --ff-only option will resolve the merge as a fast-forward when possible. When not possible, it refuses to merge and will exit with a non-zero status. Best examples used by this options are branches from features and hotfixes.

Merge develop to master

For my case I would like to keep my working branch (develop) separate than the release branch (master). Thus I will be using --no-ff option to keep them separate.

Branches

Check the available branches:

→ $ git fetch origin -v
POST git-upload-pack (132 bytes)
From https://github.com/albertpark/multi-remote
 = [up to date]      develop    -> origin/develop
 = [up to date]      master     -> origin/master

Here is an alternative output when working on a fresh repository:

$ git fetch origin
remote: Counting objects: 106, done.
remote: Compressing objects: 100% (93/93), done.
Receiving objects:  73% (78/106)
Receiving objects: 100% (106/106), 16.46 KiB | 0 bytes/s, done.
Resolving deltas: 100% (36/36), done.
From https://github.com/albertpark/multi-remote
 * [new branch]      develop    -> origin/develop
 * [new branch]      master     -> origin/master
 * [new tag]         v0.1.0      -> v0.1.0
 * [new tag]         v1.0.0-rc.1 -> v1.0.0-rc.1
 * [new tag]         v1.0.0-rc.2 -> v1.0.0-rc.2

Merge master branch

Be on the develop branch and merge with the master branch. We are ensuring that the master is up to date without any merge issues:

→ $ git switch develop
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
→ $ git merge master
Already up to date.

Merge develop branch

Switch to the master branch and merge to the develop branch with the --no-ff option:

→ $ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
→ $ git merge --no-ff develop
Merge made by the 'recursive' strategy.
 LICENSE              |   2 +-
 README.md            |  93 ++++++++++++++++++++---
 bin/git-multi-remote | 208 +++++++++++++++++++++++++++++++++++++++++++--------
 remote.conf          |   5 +-
 4 files changed, 263 insertions(+), 45 deletions(-)

Tags and final push

Best practice is to always create a release version:

→ $ git tag -a v1.1.0 -m "Releasing 1.1.0"

Finally, update the remote with all the new changes and the tag with the --follow-tags flag:

→ $ git push -u origin master --follow-tags
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 357 bytes | 178.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To https://bitbucket.org/albertpark/multi-remote.git
   64d4e98..8ec9531  master -> master
 * [new tag]         v1.1.0 -> v1.1.0
Branch 'master' set up to track remote branch 'master' from 'origin'.
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 357 bytes | 357.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for master, visit:
remote:   https://gitlab.com/albertpark/multi-remote/-/merge_requests/new?merge_request%5Bsource_branch%5D=master
remote:
To https://gitlab.com/albertpark/multi-remote.git
   64d4e98..8ec9531  master -> master
 * [new tag]         v1.1.0 -> v1.1.0
Branch 'master' set up to track remote branch 'master' from 'origin'.
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 357 bytes | 178.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/albertpark/multi-remote.git
   64d4e98..8ec9531  master -> master
 * [new tag]         v1.1.0 -> v1.1.0
Branch 'master' set up to track remote branch 'master' from 'origin'.
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 357 bytes | 178.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Analyzing objects... (2/2) (6 ms)
remote: Storing packfile... done (183 ms)
remote: Storing index... done (49 ms)
To https://albertpark.visualstudio.com/multi-remote/_git/multi-remote
   64d4e98..8ec9531  master -> master
 * [new tag]         v1.1.0 -> v1.1.0
Branch 'master' set up to track remote branch 'master' from 'origin'.

Tip

There is a config option which will also push tags automatically:

→ $ git config --global push.followTags true

Keep in mind the --follow-tags only pushes annotated tags.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment