Git Foundations / Rebase Unpublished Work

Tidy commits before sharing

Lesson 22 of 33. Open to read. No account needed.

Why Tidy Up

Real work produces messy history: "wip" commits, a typo fixed three commits later, a debugging file added and then removed. Before you share a branch, you can turn that into a few clear commits that each make sense on their own. Reviewers can follow them, and anyone undoing a change later can undo exactly one idea.

Interactive rebase, git rebase -i, lets you edit your unshared commits before you share them: combine them, reword their messages, reorder them or drop them.

The Todo List

Start it with the commit your work is based on, usually the branch you started from:

git rebase -i main

Git opens your configured editor with a todo list: one line per commit on your branch that main does not have, oldest first. Illustrative start of the list:

pick 3d7dd12 # Add tomato soup recipe
pick 9a1c0e4 # wip
pick 5b2f7aa # Add debug print

Older Git versions show the same lines without the #. Below the commits, a block of # lines explains the commands; Git ignores those lines.

Change the word at the start of a line to choose what happens to that commit. Leave the ID alone: Git uses it, not the message, to find the commit.

CommandWhat happens to the commit
pickKept as it is
rewordKept; Git opens the editor so you can change its message
squashCombined into the commit above it; you edit the combined message
fixupCombined into the commit above it; its own message is thrown away
dropRemoved, with its changes

Lines run from top to bottom, so you can also reorder commits by moving lines. Save and close the editor to start. Git replays the commits as you asked, stopping to open the editor for each reword and squash.

Staying Safe

Interactive rebase rewrites commits, so every edited commit gets a new ID. Use it only on commits nobody else has yet, for the same reason as any rebase.

You can back out at every stage:

  • While the todo list is open: delete every line and save. Git reports that there is nothing to do and changes nothing.
  • While the rebase is stopped, for example on a conflict: run git rebase --abort.
  • After it finishes: git reset --hard ORIG_HEAD returns the branch to its tip from before the rebase, provided you have not run another command that changes ORIG_HEAD, such as a reset.

Check the result before moving on: git log --oneline for the new commits, and git diff ORIG_HEAD to confirm the final content is what you expected.

Exercise: Tidy a Messy Unpublished History

The soup branch has six commits for one recipe. You will turn them into two: the recipe itself, and the change that lists it in the recipe book.

Starting state: Git configured as in the configuration lesson, including an editor you are comfortable using.

  1. Run the setup:

    mkdir -p ~/git-practice/interactive-rebase
    cd ~/git-practice/interactive-rebase
    git init -b main
    cat > recipes.md <<'EOF'
    # Recipe book
    EOF
    git add recipes.md
    git commit -m "Add recipe book"
    git switch -c soup
    cat > soup.md <<'EOF'
    # Tomato soup
    
    Ingredients:
    - 4 tomatos
    EOF
    git add soup.md
    git commit -m "Add tomato soup recipe"
    cat >> soup.md <<'EOF'
    - 1 onion
    
    Steps:
    1. Chop everything and simmer for 20 minutes.
    EOF
    git commit -am "wip"
    cat > debug.txt <<'EOF'
    Checking the step numbers.
    EOF
    git add debug.txt
    git commit -m "Add debug print"
    cat > soup.md <<'EOF'
    # Tomato soup
    
    Ingredients:
    - 4 tomatoes
    - 1 onion
    
    Steps:
    1. Chop everything and simmer for 20 minutes.
    EOF
    git commit -am "Fix typo"
    git rm debug.txt
    git commit -m "Remove debug print"
    cat >> recipes.md <<'EOF'
    - Tomato soup
    EOF
    git commit -am "Update index"
    
  2. Look at the messy history:

    git log --oneline main..soup
    

    main..soup means "commits on soup that are not on main". Six commits are listed, newest first.

  3. Start the interactive rebase:

    git rebase -i main
    

    Your editor opens with six pick lines, oldest first. Change the first word of each line so the list reads like this, keeping your own IDs:

    pick 3d7dd12 # Add tomato soup recipe
    fixup 9a1c0e4 # wip
    drop 5b2f7aa # Add debug print
    fixup 0c41d2e # Fix typo
    drop 8e9f310 # Remove debug print
    reword 61ab7c5 # Update index
    

    The "wip" and typo commits fold into the recipe commit, the debug file's two commits disappear, and "Update index" gets a better message. Save and close.

    Git then opens the editor again for the reword, showing "Update index". Replace that line with List the soup in the recipe book, then save and close.

  4. Check the result:

    git log --oneline main..soup
    git diff ORIG_HEAD
    

    Two commits remain: "Add tomato soup recipe" and "List the soup in the recipe book". git diff ORIG_HEAD prints nothing: the files are exactly what they were before tidying, because the debug file was added and removed, and every other change was kept.

  5. Practise undoing a finished tidy, then do it again:

    git reset --hard ORIG_HEAD
    git log --oneline main..soup
    git rebase -i main
    

    After the reset, all six commits are back. Edit the todo list and the reword message as in step 3 to tidy them again.

Success: git log --oneline main..soup shows the two intended commits with their messages, and git diff ORIG_HEAD shows the content is unchanged.

If something differs:

What you seeLikely causeNext step
An unfamiliar editor openedGit's editor setting is not the one you expectedCheck git config --global core.editor; the configuration lesson shows how to set it
Nothing to doThe todo list was saved emptyRun git rebase -i main again
A CONFLICT during the rebaseReordered or dropped commits change the same linesResolve as in the previous lesson, or git rebase --abort
cannot 'fixup' without a previous commitfixup or squash is on the first linegit rebase --abort, then start again with pick on the first line
debug.txt is still in the folderOnly one of its two commits was droppedgit reset --hard ORIG_HEAD and try again
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf interactive-rebase
    ls
    

Independent task: before cleaning up, add a bread recipe in two commits:

cat > bread.md <<'EOF'
# Flatbread

Flour, water and salt.
EOF
git add bread.md
git commit -m "Add bread recipe"
cat >> recipes.md <<'EOF'
- Flatbread
EOF
git commit -am "Add bread to the recipe book"

Combine these two into one commit with the message "Add bread recipe and list it", using squash so that you write the combined message yourself. Leave the two soup commits as they are.

Check Your Understanding

  1. Why should you tidy commits before pushing a branch rather than after?
  2. What is the difference between squash and fixup?

Answer and Reasoning

Independent task

Run git rebase -i main. The list now has four lines. Leave the two soup commits and "Add bread recipe" as pick, and change the last line to squash:

pick 3d7dd12 # Add tomato soup recipe
pick 61ab7c5 # List the soup in the recipe book
pick 4f0a8b2 # Add bread recipe
squash 7c2d915 # Add bread to the recipe book

Your IDs will differ. After you save, the editor opens with both messages under # This is a combination of 2 commits. Replace them with the single line Add bread recipe and list it, then save and close. git log --oneline main..soup shows three commits, and the newest one changes both bread.md and recipes.md.

Understanding check

  1. Tidying rewrites commits, giving them new IDs. Before you push, nobody else has the originals, so nothing breaks. After pushing, others may already have the old commits or have built on them; replacing them would make their history disagree with yours.
  2. Both combine a commit into the one above it. squash opens the editor with both messages so you can write a combined one. fixup keeps only the message of the commit above and discards its own, without asking.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy