Skip to content

Instantly share code, notes, and snippets.

@ganwell
Last active October 2, 2019 02:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganwell/7682988 to your computer and use it in GitHub Desktop.
Save ganwell/7682988 to your computer and use it in GitHub Desktop.
GIt branching, bugfixing, backporting
$> mkdir test
$> cd test/
$> git init
Initialized empty Git repository in /home/ganwell/Repositories/test/.git/
$> git checkout -b stable
Switched to a new branch 'stable'
$> git commit --allow-empty -m "* initial revision"
[stable (root-commit) 32d8782] * initial revision
$> git commit --allow-empty -m "* other revision"
[stable bc2220b] * other revision
$> git commit --allow-empty -m "* release v1.0"
[stable ffa548b] * release v1.0
$> git tag v_1.0
$> git checkout -b feature_for_v1.1
Switched to a new branch 'feature_for_v1.1'
$> git commit --allow-empty -m "* first in feature"
[feature_for_v1.1 cd71cf3] * first in feature
$> git commit --allow-empty -m "* second in feature"
[feature_for_v1.1 746c9e5] * second in feature
$> git commit --allow-empty -m "* third in feature"
[feature_for_v1.1 af44dd3] * third in feature
$> git checkout stable
Switched to branch 'stable'
$> git checkout -b dev
Switched to a new branch 'dev'
$> git merge feature_for_v1.1
Updating ffa548b..af44dd3
Fast-forward
$> echo test dev branch
test dev branch
$> git checkout stable
Switched to branch 'stable'
$> git merge dev
Updating ffa548b..af44dd3
Fast-forward
$> git tag v_1.1
$> echo bugfix und backport on v1.0
bugfix und backport on v1.0
$> git checkout v_1.0
Note: checking out 'v_1.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at ffa548b... * release v1.0
$> git checkout -b version_1.0
Switched to a new branch 'version_1.0'
$> git commit --allow-empty -m "* bugfix in v1.0"
[version_1.0 de06628] * bugfix in v1.0
$> git commit --allow-empty -m "* bugfix in v1.0 more"
[version_1.0 2dcfc34] * bugfix in v1.0 more
$> git cherry-pick --allow-empty v_1.0..feature_for_v1.1
[version_1.0 77a7196] * first in feature
[version_1.0 4a5c399] * second in feature
[version_1.0 4fb2348] * third in feature
$> git lg
* 4fb2348 (3 minutes ago) Jean-Louis Fuchs (HEAD, version_1.0) * third in feature
* 4a5c399 (3 minutes ago) Jean-Louis Fuchs * second in feature
* 77a7196 (3 minutes ago) Jean-Louis Fuchs * first in feature
* 2dcfc34 (30 seconds ago) Jean-Louis Fuchs * bugfix in v1.0 more
* de06628 (41 seconds ago) Jean-Louis Fuchs * bugfix in v1.0
| * af44dd3 (3 minutes ago) Jean-Louis Fuchs (tag: v_1.1, stable, feature_for_v1.1, dev) * third in feature
| * 746c9e5 (3 minutes ago) Jean-Louis Fuchs * second in feature
| * cd71cf3 (3 minutes ago) Jean-Louis Fuchs * first in feature
|/
* ffa548b (4 minutes ago) Jean-Louis Fuchs (tag: v_1.0) * release v1.0
* bc2220b (4 minutes ago) Jean-Louis Fuchs * other revision
* 32d8782 (4 minutes ago) Jean-Louis Fuchs * initial revision
$> git tag v_1.0.1
$> git checkout stable
Switched to branch 'stable'
$> git merge version_1.0
Merge made by the 'recursive' strategy.
$> git lg
* 912a491 (19 seconds ago) Jean-Louis Fuchs (HEAD, stable) Merge branch 'version_1.0' into stable
|\
| * 4fb2348 (4 minutes ago) Jean-Louis Fuchs (tag: v_1.0.1, version_1.0) * third in feature
| * 4a5c399 (4 minutes ago) Jean-Louis Fuchs * second in feature
| * 77a7196 (4 minutes ago) Jean-Louis Fuchs * first in feature
| * 2dcfc34 (2 minutes ago) Jean-Louis Fuchs * bugfix in v1.0 more
| * de06628 (2 minutes ago) Jean-Louis Fuchs * bugfix in v1.0
* | af44dd3 (4 minutes ago) Jean-Louis Fuchs (tag: v_1.1, feature_for_v1.1, dev) * third in feature
* | 746c9e5 (4 minutes ago) Jean-Louis Fuchs * second in feature
* | cd71cf3 (4 minutes ago) Jean-Louis Fuchs * first in feature
|/
* ffa548b (5 minutes ago) Jean-Louis Fuchs (tag: v_1.0) * release v1.0
* bc2220b (5 minutes ago) Jean-Louis Fuchs * other revision
* 32d8782 (6 minutes ago) Jean-Louis Fuchs * initial revision
$>
@ganwell
Copy link
Author

ganwell commented Nov 27, 2013

Of course in this example a simple merge instead of the cherry-pick would be sufficient. BUT if you want to backport from version 1.3 to version 1.0 you need:

git checkout v_1.0
git checkout -b version_v1.0
git cherry-pick v1.2..feature_for_v1.3

And a simple merge would pull in complete v1.1, v1.2.

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