World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Tidy history with interactive rebase

You will learn: to fix up, reword, drop and reorder your own commits before sharing.

The problem

git log --oneline on gallery: debug log, fix typo, Add photo captions, Add gallery page, then the main commits. Terminal output: $ git log --oneline 6961f7b (HEAD -> gallery) debug log 87ddf3e fix typo fe5cd49 Add photo captions 5ab6383 Add gallery page 270a76e (main) Add style a8feb63 Add day 1 page 6d3a9b1 Add README

Your gallery branch works. But look at that history! Can you tidy it before anyone sees it?

Watch

git-rebase-todo
pick 5ab6383 # Add gallery page
pick 978a45a # fix typo
pick e2df039 # captions
The to-do list Git opens: three lines, oldest first. pick 5ab6383 Add gallery page, pick 978a45a fix typo, pick e2df039 captions.

git rebase -i main opens a to-do list of your commits, oldest on top. Change the word in front, and Git replays the list. That’s an interactive rebase: git rebase -i: tidy your own commits before you share them..

Watch

Three commits: Add gallery page, fix typo, captions. fix typo melts into Add gallery page. captions gets a new name: Add photo captions.

The words: pick keeps a commit. reword gives it a new message. squash: Join a commit into the one before it, and keep both messages. joins it into the one above. fixup: Like squash, but this commit's message is thrown away. joins it and drops its message. drop deletes it.

Try it

Fold “fix typo” into the card above it. Rename “captions” to “Add photo captions”.

Each card is one commit. Pick an action. Drag the grip or use the arrows to reorder.

Top = oldest. Git replays the cards from the top down.

  • 5ab6383 Add gallery page
    keep it
  • 978a45a fix typo
    keep it
  • e2df039 captions
    keep it
History before Commits from newest: captions, fix typo, Add gallery page, Add style, Add day 1 page, Add README.
6d3a9b1a8feb63270a76e5ab6383978a45ae2df039

Predict

You drop the “debug log” commit. It added debug.txt. What happens to that file?

Pick one answer.

Try it

Now a messier branch. Move “fix typo” right under “Add gallery page” and fix it up. Drop “debug log”.

Each card is one commit. Pick an action. Drag the grip or use the arrows to reorder.

Top = oldest. Git replays the cards from the top down.

  • 5ab6383 Add gallery page
    keep it
  • fe5cd49 Add photo captions
    keep it
  • 87ddf3e fix typo
    keep it
  • 6961f7b debug log
    keep it
History before Commits from newest: debug log, fix typo, Add photo captions, Add gallery page, Add style, Add day 1 page, Add README.
6d3a9b1a8feb63270a76e5ab6383fe5cd4987ddf3e6961f7b

Check

Good time to tidy with git rebase -i?

Swipe right for good, left for needs work. Or use the buttons.

1 / 4

Your own branch, before you open a pull request

Your own branch, before you open a pull request

You did it!

You can now tidy your commits before you share them.

Go deeper (optional)

In a real terminal, git rebase -i main opens this list in your editor. Edit the words, save and close. Got lost? git rebase --abort. Tip: git commit --fixup <commit> marks a fix early. Later, git rebase -i --autosquash main puts it in place for you.