git rebase - ghdrako/doc_snipets GitHub Wiki

Rebase the feature branch onto the master branch using the following commands.

$ git checkout feature
$ git rebase master

# or shorter

git rebase master feature # nie trzeba sie  chekoutowac

This moves the entire feature branch on top of the master branch. It does this by re-writing the project history by creating brand new commits for each commit in the original (feature) branch.

---c1(main)---c2-----c3(*,another)
git rebase another main
---c1------c2-----c3(*,another,main)

Git rebase - changes the base of the branch.

Base of a brancha - the latest commit which is in common with the branch and its parent branch.

git rebase <target branch>

This command rebases the current branch onto the target branch. This means the current branch is now based on the last commit on the target branch. Consequently, the git commit-graph will look like the branch never separated from the original branch, and the commits were based on the current base.

$ git checkout feature
$ git rebase master  # git rebase <target branch>

git rebase actually does is:

  • Relocates the branch head pointer to the target branch pointer. (feature now points to the master)
  • Makes new duplicate commits on the branch. These commits are essentially the same have the same code changes as the original commits, but have different SHA1 values.
  • The older commits are still present on the graph in a detached state.

git rebase --onto

git rebase --onto <newparent> <oldparent>
      Before                           After
    A---B---C---F---G (branch)        A---B---C---F---G (branch)
             \                                     \
              D---E---H---I (HEAD)                  E---H---I (HEAD)

git rebase --onto F D  

Rebase the commit reachable from HEAD whose parent is D (so E) on top of F. In other words, change the parent of E from D to F. The syntax of git rebase --onto is then git rebase --onto <newparent> <oldparent>.

Another scenario where this comes in handy is when you want to quickly remove some commits from the current branch without having to do an interactive rebase:

          Before                       After
    A---B---C---E---F (HEAD)        A---B---F (HEAD)

git rebase --onto B E

In this example, in order to remove C and E from the sequence you would say git rebase --onto B E, or rebase HEAD on top of B where the old parent was E.

  1. git rebase --onto with 3 arguments
git rebase --onto <newparent> <oldparent> <until>

Allows you to rebase an arbitrary range of commits on top of another one.

          Before                                     After
    A---B---C---F---G (branch)                A---B---C---F---G (branch)
             \                                             \
              D---E---H---I (HEAD)                          E---H (HEAD)

git rebase --onto F D H

In this case, we want to rebase the exact range E---H on top of F, ignoring where HEAD is currently pointing to. We can do that by saying git rebase --onto F D H, which means:

Rebase the range of commits whose parent is D up to H on top of F.

The syntax of git rebase --onto with a range of commits then becomes git rebase --onto . The trick here is remembering that the commit referenced by is included in the range and will become the new HEAD after the rebase is complete.

⚠️ **GitHub.com Fallback** ⚠️