- to have a branch based on a new parent/more up to date parent,
- to alter past commits in a branch
git rebase <newbase>
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.
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
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
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)