The letter A styled as Alchemists logo. lchemists
Published February 15, 2020 Updated February 16, 2020

Git Rebase Pick

Demonstrates using rebase to organize your commit history.


# Hello and welcome to the Alchemists Screencasts!
# Today, we'll look at Git Rebase Pick.
# Being able to _pick_ commits is the first thing you learn when rebasing:

git rebase --interactive

# If it helps, here are the commits being rebased (via Git Log):

git log --graph --pretty=format:"%C(yellow)%h%C(reset) %G? %C(bold blue)%an%C(reset) %s%C(bold cyan)%d%C(reset) %C(green)%cr.%C(reset)" documentation...master

# That was a lot to type, so we'll use my `gl` alias from this point forward:

gl documentation...master

# As you can see above, same output but with less typing.
# Let's rebase one more time to study the Git documentation:

git rebase --interactive

# Many commands are listed in the help text but we'll focus on *pick*.
# Here's the previously selected documentation for a quick recap:
#   p, pick <commit> = use commit
#   These lines can be re-ordered; they are executed from top to bottom.

# Using `p` or `pick` informs Git to *use* that commit when rebasing.
# 💡 I have Git Rebase Abbreviations turned on.
# 💡 This is why you see `p` instead of `pick`.
# 💡 See the *Git Rebase Abbreviations* screencast for details.

# You might be thinking that *picking* a commit seems boring.
# True, it is the most basic action you can do when rebasing.
# Reordering commits is where `pick` becomes powerful, though:

git rebase --interactive

# Notice *contribution* became the first commit on the feature branch:

gl documentation...master

# Picking and reordering commits is a powerful feature.
# This helps tell a compelling, linear, story with your commits.
# Especially when completing certain aspects of your implementation out-of-order.
# I sometimes find myself in this predicament as well.
# Using Git Rebase to reorder commits ensures a linear/logical story. 🎉

# That's not all, though.
# There is a another power to *picking* commits when rebasing.
# That is the ability to insert commits from another branch.
# To explain, let's look at our existing branches:

git switch legal
gl legal...master
git switch documentation

# Notice the `legal` branch had a single commit which added licensing.
# It would be nice to add that commit to our `documentation` branch.
# With *pick* we can do exactly that!
# Like cherry picking, we'll use the SHA of the commit from the `legal` branch:

git rebase --interactive

gl documentation...master

# Notice we have *four* commits on the `documentation` branch now.
# Even better, the *license* commit was added as the second-to-last commilt. 🎉
# This is a great way to combine a related set of commits when rebasing.

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄