Skip to content

Instantly share code, notes, and snippets.

@rca
Last active February 25, 2022 05:33
Show Gist options
  • Save rca/c6eed10f07204955daba821635b6fe6c to your computer and use it in GitHub Desktop.
Save rca/c6eed10f07204955daba821635b6fe6c to your computer and use it in GitHub Desktop.
An explanation of `git rebase --onto`

git rebase onto fu

Looking at the following git history:

[0][~/Projects/openslate/thing(env/dev-auth:feb0ee9)]
$ git log
commit feb0ee98c8b77e929b9cc23442c5664c9d4986c9 (HEAD -> env/dev-auth)  # this is `3.` in the command below
Author: Roberto Aguilar <[email protected]>
Date:   Thu Sep 26 00:25:15 2019 -0400

    Accidental commit 3

commit 7592d4d1ab1a101ad654abc097672ab9bfecbd8a
Author: Roberto Aguilar <[email protected]>
Date:   Thu Sep 26 00:24:50 2019 -0400

    Accidental commit 2

commit 89fe221406158f5d069cbd669feddff28e307b4f
Author: Roberto Aguilar <[email protected]>
Date:   Thu Sep 26 00:24:08 2019 -0400

    Accidental commit 1

commit 58f4606f37359829c5b4f945a1a4c4a17c4428b4 (origin/env/dev-auth)  # this is `2.` in the command below
Merge: df3ebc5 3fee1eb
Author: Roberto Aguilar <[email protected]>
Date:   Thu Sep 26 00:01:42 2019 -0400

    The commit that accidental commits were commited on top of

[...]

There are a few commits that were accidentally made on top of env/dev-auth that should have been made on a feature branch named experiments/sso-login-app (1. in the command below). In order to fix the problem git rebase —onto can be used, but it’s confusing. Here’s what the parameters mean:

$ git rebase --onto experiments/sso-login-app origin/env/dev-auth env/dev-auth
                    \_______________________/ \_________________/ \__________/
                                 |                     |                |
1. the branch that will contain  |                     |                |
the final resulting rebase  -----+                     |                |
                                                       |                |
2. the branch/commit that additional commits were      |                |
accidentally made on top of  --------------------------+                |
                                                                        |
3. the local branch containing the accidental commits  -----------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment