Skip to content

8. Advanced Git Commands

Now that you’re comfortable with the everyday workflow, let’s look at the commands that make you feel like you truly own your history. These tools are powerful, and a few of them rewrite history, so keep in mind the golden rule: don’t rewrite history that has already been shared with others.

Rebasing is a way to move or reshape a series of commits. Watch how a rebase replays your feature commits on top of the latest main:

1 / 2
Step 1 of 2

feature branched off B. Meanwhile main moved on to C. The two lines have diverged.

The interactive mode (-i) is especially useful for cleaning up your local history before sharing it: squashing tiny “fix typo” commits, reordering work, or rewriting messages so your branch tells a clear story.

To start an interactive rebase over your last few commits, count how many commits back you want to edit and use HEAD~n:

Terminal window
git rebase -i HEAD~3

Git opens an editor listing those commits, oldest at the top, each with a command in front of it:

pick a1b2c3d Add login form
pick d4e5f6a Fix typo in label
pick 7g8h9i0 Add form validation

You change the word in front of each commit to tell Git what to do:

CommandWhat it does
pickKeep the commit as is
rewordKeep the commit but edit its message
editPause to amend the commit’s content
squash / sMerge into the previous commit, combining messages
fixup / fLike squash, but discard this commit’s message
dropDelete the commit entirely

For example, to fold the typo fix into the commit before it, you’d write:

pick a1b2c3d Add login form
fixup d4e5f6a Fix typo in label
pick 7g8h9i0 Add form validation

Save and close the editor, and Git replays the commits according to your instructions, producing a cleaner history.

If a commit doesn’t replay cleanly, the rebase pauses on a conflict, just like a merge. Resolve the files, stage them, and continue, or back out of the whole operation:

Terminal window
git add <resolved-files>
git rebase --continue # resume after fixing the conflict
git rebase --abort # give up and return to where you started

Both integrate changes from one branch into another, but they tell different stories:

  • git merge preserves the exact history and adds a merge commit. It’s honest about what happened but can get noisy.
  • git rebase replays your commits on top of another branch, producing a straight, linear history as if you’d started from the latest code.

Neither is “correct,” it’s a team preference. Many teams rebase their feature branches to stay current, then merge into main.

Reflog and recovering lost commits (git reflog)

Section titled “Reflog and recovering lost commits (git reflog)”

Here’s a reassuring secret: in Git, it’s surprisingly hard to truly lose work. Even after a “bad” reset or a rebase gone wrong, your old commits usually still exist, they just aren’t reachable by any branch anymore. The reflog is your safety net.

The reflog records every move HEAD makes: every commit, checkout, reset, merge, and rebase. To see it:

Terminal window
git reflog

You’ll get something like:

e98d9c8 HEAD@{0}: reset: moving to HEAD~1
8f0cd01 HEAD@{1}: commit: Add form validation
299a903 HEAD@{2}: commit: Fix typo in label

Imagine you ran git reset --hard HEAD~1 and panicked because a commit vanished. You can see in the reflog exactly where HEAD was just before, and jump back to it:

Terminal window
git reset --hard HEAD@{1}

Or, if you’d rather bring the lost commit back as a new branch without touching your current one:

Terminal window
git branch recovered 8f0cd01

The reflog is local to your machine, and it is a recovery tool for your own mistakes, not a backup of the remote. Entries also expire: reachable ones after 90 days by default (gc.reflogExpire), and entries that are no longer reachable from any branch, which is exactly the lost-commit case above, after 30 days (gc.reflogExpireUnreachable). So the reflog can rescue you from a recent mistake, but don’t treat it as long-term storage.

Sometimes you want just one specific commit from another branch, not the whole branch. A classic example: a bug was fixed on main, and you need that same fix on a release branch without merging everything else.

That’s what cherry-picking does. Press the button to copy a single commit across branches:

1 / 2
Step 1 of 2

A bug was fixed on the fix branch (commit X). We want only that single commit on main, not the whole branch.

Find the commit hash you want (with git log), then:

Terminal window
git cherry-pick <commit-hash>

Git takes the changes from that commit and applies them as a brand new commit on your current branch. You can pick several at once:

Terminal window
git cherry-pick <hash1> <hash2> <hash3>

If the commit doesn’t apply cleanly, you’ll get a conflict to resolve, just like a merge. Fix the files, stage them, and continue:

Terminal window
git add <resolved-files>
git cherry-pick --continue

Or back out entirely:

Terminal window
git cherry-pick --abort

You’re in the middle of something, your files are half-edited, and suddenly you need to switch branches to handle an urgent fix. You don’t want to commit unfinished work, but Git won’t let you switch with a messy working directory. This is exactly what git stash is for.

Watch your changes get tucked away and then restored:

1 / 3
Step 1 of 3

You have uncommitted edits in app.js, but you suddenly need a clean working directory to switch branches.

📂 Working Directory
📄 app.js
📦 Stash stack
empty
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.

Think of the stash as a clipboard for work in progress: a quick, local convenience, not a place to store work long term. Stashing tucks your uncommitted changes away safely and gives you a clean working directory:

Terminal window
git stash

Your changes are now saved on a stack, and your working directory matches the last commit. Go do your urgent work, switch branches freely, then come back and restore your changes:

Terminal window
git stash pop

pop applies your most recent stash and removes it from the stack. If you’d rather keep the stash around after applying it, use git stash apply instead.

A few useful variations:

Terminal window
git stash list # See everything you've stashed
git stash push -m "message" # Stash with a descriptive label
git stash drop # Delete the latest stash
git stash clear # Delete all stashes

The older git stash save "message" still works but is deprecated, so prefer git stash push -m going forward.

By default, git stash only saves tracked files. If you also want to include new, untracked files, add the -u flag:

Terminal window
git stash -u