Skip to content

Instantly share code, notes, and snippets.

@padawin
Last active January 30, 2020 22:58
Show Gist options
  • Save padawin/f0a59fded97d171b519b5426dabb7bed to your computer and use it in GitHub Desktop.
Save padawin/f0a59fded97d171b519b5426dabb7bed to your computer and use it in GitHub Desktop.
rebase-exercises

Rebase

Why?

  • to have a branch based on a new parent/more up to date parent,
  • to alter past commits in a branch

Notation

git rebase <newbase>

Exercise 1: update branch against parent

When to do it: for example to get the latest master in your branch.

What to do:

  • make sure the parent is up to date. If not, update it.
  • go on the branch to update.
  • rebase it against the parent

Branch for the exercise: exercise1 to be rebased against master.

Exercise 2: update branch with latest remote version

When to do it: when your branch and the remote diverged because someone else pushed commits on it. Typical case of team work.

What to do:

  • fetch the remote where the branch is
  • go on the branch to update (if not on it already)
  • if the branch is behind, pull; otherwise rebase or pull --rebase

Branch for the exercise: exercise2

Extra instructions:

  • wait for my signal
  • pull
  • add a commit or two in the branch
  • wait for my signal
  • try to push (must fail)
  • update branch using rebase method previously described

Exercise 3: remove commits from a branch

When to do it:

  • when the commits are not needed anymore (and still in a development branch!)
  • when a branch has been accidentally created on top of another instead of its desired parent (usually master)

What to do:

  • make sure the parent is up to date. If not, update it.
  • go on the branch to update
  • rebase interactively against the parent

Branch for the exercise: exercise3

Exercise 4: completely rework your branch

When to do it: You committed your work, the code is ready to be reviewed, but you have commits all around the show:

  • too many commits to fix spaces which could be grouped in one,
  • some commits have typo in their messages,
  • some commits are missing changes,
  • some commits could be reordered

Can also be used to fix a commit introducing a bug (discovered with git bisect)

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