Skip to content
Tutorials Center
My learning

Topics

Lesson 77 of 78 ยท Practice and Review

Practice Problems: Rewriting, Remotes and Internals

Fifteen advanced Git exercises on squashing, rebase, reflog recovery, filter-branch, bare remotes, force-with-lease, plumbing commands and bisect, with verified solutions.

These fifteen problems are the second half of the practice set. They go past everyday commits and into the parts of Git that make people nervous: rewriting history, recovering lost work, working with a remote that has other people on it, and building commits by hand with plumbing commands. If you have not done the basic problems, start there.

Every problem has a goal, a hint and a complete solution script. Each script builds its own repository in a temporary directory, so nothing you try can hurt a real project. Remotes are simulated with local bare repositories, which behave like a hosting server for all purposes of clone, fetch and push. Try each problem yourself before you run the solution, and read the real output carefully: the order of the lines is often the answer.

The rewriting problems (1 to 7) change history. In a real project do this only on commits nobody else has pulled. The remote problems (8 to 12) show exactly what happens when you forget that rule.

Part 1: Rewriting history

Problem 1: Squash five commits into one

A branch has five commits wip 1 ... wip 5 on top of Base. Replace them with a single commit Add the feature, keeping the final file content.

Hint

Move the branch back with git reset --soft HEAD~5 (changes stay staged), then commit once.

Bash Solution 1: squash with reset --soft

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 2: Drop a commit from the middle

History is A, B (oops), C, D. Remove B completely while keeping C and D. Do it with an interactive rebase, but script the todo list so no editor opens.

Hint

git rebase -i <base> lists commits oldest first. Change the word pick to drop on the line you want to remove. Set GIT_SEQUENCE_EDITOR to a sed command that edits the todo file. Choose changes that touch different files so no conflict arises.

Bash Solution 2: drop a commit with a scripted todo list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 3: Fold a fix into the commit it belongs to

Commit Add login has a typo that you fix later in a separate commit. Fold the fix into Add login so the history shows just two commits (Base, Add login), without opening an editor.

Hint

git commit --fixup=<commit> creates a commit named fixup! .... Then git rebase -i --autosquash <base> moves it next to its target and marks it fixup. GIT_SEQUENCE_EDITOR=true accepts the todo list unchanged.

Bash Solution 3: fixup and autosquash

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 4: Make history linear by rebasing

main got a commit while you worked on topic. Rebase topic onto main and show a straight-line history without any merge commit.

Hint

On topic, run git rebase main. Afterwards a fast-forward merge on main is possible.

Bash Solution 4: rebase then fast-forward

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 5: Move a branch to a new base with rebase --onto

topic2 was started from topic1, but topic1 is not ready. Move topic2 so that it sits directly on main, leaving topic1's commits behind.

Hint

git rebase --onto <new-base> <old-base> <branch> takes the commits after <old-base> up to <branch> and replays them on <new-base>.

Bash Solution 5: rebase --onto

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 6: Recover commits lost to reset --hard

You ran git reset --hard HEAD~2 and two commits seem gone. Get them back.

Hint

The reflog records every place HEAD has been. git reflog shows entries such as HEAD@{1}. Reset to the entry from before your mistake.

Bash Solution 6: rescue with the reflog

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 7: Remove a file from the whole history

secret.txt was committed in the first commit and deleted later, but it is still in history. Rewrite all commits so that no commit contains it, then prove it.

Hint

git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch secret.txt' -- --all rewrites every commit. Set FILTER_BRANCH_SQUELCH_WARNING=1 to silence the deprecation pause. git filter-repo is the modern tool, but it is a separate program.

Bash Solution 7: filter-branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The rewrite keeps a backup of the old branch under refs/original/, and the reflog still remembers it too. The old commits, and the secret, stay in the object database until you delete those references, expire the reflog and run git gc --prune=now. --prune-empty dropped the now-empty "Remove secret" commit. Once a secret was pushed, assume it is compromised and change it. See Amending, Splitting and Rewriting Commits.

Part 2: Remotes

All remote problems use a local bare repository named remote.git as the server, and one or two clones standing in for teammates.

Problem 8: Create a remote, clone it, push

Create a bare repository, clone it as alice, make a commit and push it with upstream tracking. Show that git status reports the branch as up to date.

Hint

git init --bare remote.git, git clone remote.git alice, then git push -u origin main.

Bash Solution 8: bare remote, clone, push -u

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 9: See new work with fetch, then integrate it

Bob pushes a commit. In Alice's clone, download it with fetch (without changing her branch), show that she is one commit behind, then bring it in with a fast-forward only merge.

Hint

git fetch updates origin/main, not main. Compare with git log main..origin/main or git status -sb (which prints [behind 1]). Then git merge --ff-only origin/main.

Bash Solution 9: fetch, inspect, fast-forward

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 10: A rejected push

Alice and Bob both start from the same commit. Bob pushes first. Alice commits and tries to push: show that Git rejects it, then fix it with pull --rebase and push successfully.

Hint

The error says fetch first or non-fast-forward: the server has commits Alice does not. Do not force. Rebase her commit on top of Bob's with git pull --rebase.

Bash Solution 10: rejected push, pull --rebase, push

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 11: Force-push safely after an amend

Alice amended a commit that she had already pushed. A normal push is refused. Update the remote with the safest possible forced push and show the server's new history.

Hint

git push --force-with-lease overwrites the remote branch only if it still points where your origin/main says it does. Plain --force has no such check.

Bash Solution 11: --force-with-lease

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 12: Delete a remote branch and prune stale references

Alice pushes a branch temp and then deletes it on the server. Bob still lists origin/temp. Make Bob's list match the server.

Hint

git push origin --delete temp removes it on the server. Other clones keep their remote-tracking branch until git fetch --prune.

Bash Solution 12: push --delete and fetch --prune

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Part 3: Internals and detective work

Problem 13: Compute a blob hash by hand

Without asking Git to store anything, compute the object ID of a file containing exactly hello and a newline, using only printf and sha1sum, then confirm that Git agrees.

Hint

A blob object is hashed as the text blob , the size in bytes, a NUL byte, then the content. The text hello plus newline is 6 bytes.

Bash Solution 13: the blob hash by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 14: Build a commit with plumbing commands

Create a commit on a new repository without using git add or git commit. Use hash-object, update-index, write-tree, commit-tree and update-ref, then show that git log and git show can read it.

Hint

Steps: store the content as a blob (hash-object -w), put it in the index (update-index --add --cacheinfo 100644,<blob>,<path>), turn the index into a tree (write-tree), create the commit (commit-tree <tree> -m ...), and point the branch at it (update-ref refs/heads/main <commit>). See Git Objects.

Bash Solution 14: a commit built by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Problem 15: Find the commit that broke a test automatically

Ten commits each write a number into value.txt. A check passes while the number is below 7. Let Git find the first bad commit without you testing by hand.

Hint

git bisect start, mark the known bad commit (bad HEAD) and a known good one, then git bisect run <command>. The command must exit 0 for good and non-zero (1 to 124) for bad. Finish with git bisect reset.

Bash Solution 15: git bisect run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bisect needs about log2(10), roughly 3 to 4 tests instead of ten. See Finding Bugs with git bisect.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. After git reset --hard HEAD~2 you want the two commits back. Where do you look?

    The reflog records previous positions of HEAD, so the old tip can still be reached for a long time.
  2. 2. What does git rebase --onto main topic1 topic2 do?

    --onto takes the new base, the old base (exclusive) and the branch; only the commits between them are moved.
  3. 3. Why does git push --force-with-lease protect teammates better than --force?

    If someone pushed in the meantime, the lease check fails and your push is refused instead of erasing their work.
  4. 4. What is the correct input to hash a blob by hand?

    Git hashes a header (type and size, ending in a NUL byte) followed by the content, which is why identical files always have identical blob IDs.
  5. 5. A bisect run command must exit with which code to mark a commit as good?

    Exit 0 means good, 1 to 127 (except 125) means bad, and 125 means skip this commit.

Summary

  • reset --soft plus a new commit squashes; interactive rebase with a scripted todo (GIT_SEQUENCE_EDITOR) drops, reorders and folds commits; commit --fixup with --autosquash automates folding.
  • rebase makes history linear; rebase --onto moves a branch to a new base.
  • The reflog and ORIG_HEAD bring back commits after a bad reset or rebase.
  • filter-branch (or filter-repo) removes a file from all history, but a pushed secret must be treated as leaked.
  • A rejected push means the server has work you lack: fetch, integrate (rebase or merge), push. Use --force-with-lease only for rewrites of your own branch.
  • Objects are content-addressed: a blob ID is the SHA-1 of a header plus the content, and plumbing commands can build a commit step by step.
  • git bisect run finds a breaking commit in a logarithmic number of tests.

Next: Git Interview Questions and Answers