Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 78 ยท Practice and Review

Practice Problems: Commits, Branches and Merges

Fifteen graded Git exercises on commits, staging, undo, branches, merges, conflicts, stash, tags and cherry-pick, each with a hint and a verified runnable solution.

Reading about Git is not the same as using it. These fifteen problems cover the everyday core of the tool: commits and staging, undoing small mistakes, branches, merges, conflicts, stashing, tags, cherry-pick and revert. They start easy and get a little harder. Each problem states a goal, gives a hint, and ends with a complete solution that builds a fresh repository and prints the result.

Work like this: read the problem, close the solution, and try it on your own machine or in a scratch script. Only then open the hint, and only after that run the solution. If your result differs, compare the commands and think about why. Every solution is a self-contained script, so you can also edit it and run it again.

Every script exports a fixed author and date so that output is reproducible. Real work does not need that, you only need user.name and user.email configured once. Hashes are avoided in the expected output because only the commit subjects matter here.

Part 1: Commits and staging

Problem 1: Your first commit

Create a repository, add a file README.md containing the line Hello, commit it with the message Add README, and show the history in one line per commit.

Hint

You need git init, git add, git commit -m, and git log --oneline.

Bash Solution 1: first commit

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.

A clean ## main status line with no files below it means nothing is left to commit.

Problem 2: Commit only one of two files

You changed a.txt and b.txt, but the two changes belong in separate commits. Commit a.txt first with the message Update a, leave b.txt modified and uncommitted, and prove it with git status -s.

Hint

git add takes paths. Only what is in the index goes into the commit.

Bash Solution 2: commit one file only

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: Stage only part of a file

notes.txt has 30 lines. You changed line 2 and line 29. Commit only the change to line 2, and leave the change to line 29 as an unstaged edit.

Hint

git add -p walks through the hunks and asks y (stage), n (skip). Changes far apart become separate hunks. In a script you can feed the answers on standard input.

Bash Solution 3: git add -p with scripted answers

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: Fix the message of the last commit

You committed with the message Ad README (typo). Change it to Add README without creating a second commit.

Hint

git commit --amend -m "new message" replaces the last commit. This is safe only if you have not shared the commit.

Bash Solution 4: amend the message

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: Bring back a file you deleted by accident

You ran rm config.ini but have not committed. Get the file back with its committed content.

Hint

git restore <path> copies the file from the index (or from HEAD) into the working tree.

Bash Solution 5: restore a deleted file

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: Unstage a file you added by mistake

You ran git add . and accidentally staged secret.env. Unstage it without deleting it.

Hint

git restore --staged <path> removes the file from the index but leaves the file on disk.

Bash Solution 6: unstage without deleting

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.

secret.env is back to untracked (??), and the file is still there. See .gitignore for keeping such files out of git add . for good.

Part 2: Branches and merges

Problem 7: A feature branch with a fast-forward merge

Create a branch login from main, add one commit on it (Add login page), switch back, and merge it. The history should stay a straight line, with no merge commit.

Hint

If main did not move, Git simply slides the pointer forward (a fast-forward). Check that no commit has two parents with git log --merges.

Bash Solution 7: fast-forward merge

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 8: Force a merge commit

Do the same as Problem 7, but this time main must record a merge commit so the feature's history stays visible. Print the graph.

Hint

git merge --no-ff -m "message" branch.

Bash Solution 8: merge with --no-ff

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: Merge two diverged branches

main and docs both moved on: main added code.txt, docs added guide.txt. Merge docs into main and list the files in the result.

Hint

The changes do not overlap, so Git merges automatically and creates a merge commit.

Bash Solution 9: a three-way merge

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: Resolve a merge conflict

Both branches changed the same line of greeting.txt (Hello on main, Hi on friendly). Merge friendly into main and resolve the conflict by keeping the text Hello, friend. Finish the merge with a commit.

Hint

A conflicted merge stops and puts markers (<<<<<<<, =======, >>>>>>>) into the file. Edit the file to the final content, git add it, then git commit. See Resolving Merge Conflicts.

Bash Solution 10: conflict, resolve, commit

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: Stop tracking a file but keep it on disk

debug.log was committed by mistake. Remove it from the repository (not from your disk) and make sure it never shows up in git status again.

Hint

Ignoring a file does not untrack it. Use git rm --cached first, then add the pattern to .gitignore.

Bash Solution 11: untrack and ignore

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: Saving, tagging and copying work

Problem 12: Interrupt your work with a stash

You are editing app.txt when a bug report arrives. Stash the edit, show that the tree is clean, then bring the edit back.

Hint

git stash push -m "name" saves tracked changes and cleans the tree; git stash pop re-applies the newest stash and drops it.

Bash Solution 12: stash and pop

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 13: Tag a release

Tag the current commit as v1.0.0 with an annotated tag whose message is First release. Then list tags and show what the tag points to.

Hint

git tag -a v1.0.0 -m "...", then git tag -n lists tags with their messages, and git show --no-patch v1.0.0 shows the tag object and the commit.

Bash Solution 13: an annotated tag

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: Copy one commit to another branch

feature has three commits. Only the middle one (Fix typo in docs) is needed on main. Copy just that commit.

Hint

git cherry-pick <commit>. Find the commit with git log --oneline feature or git log --grep.

Bash Solution 14: cherry-pick one commit

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 commit on main has a new hash, because its parent differs, but the same message and change.

Problem 15: Undo a commit that is already shared

A teammate already pulled your commit Break the build. Undo its effect without rewriting history, and show that the file is back to its old content while the history keeps all three commits.

Hint

git revert <commit> creates a new commit that applies the opposite change. Add --no-edit to accept the default message.

Bash Solution 15: revert a shared commit

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 file is correct again and nothing was erased. For more, see Reverting Commits Safely.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In Problem 3, why can git add -p stage line 2 without line 29?

    Patch mode splits a diff into hunks (groups of nearby changed lines) and asks about each hunk.
  2. 2. What is the difference between git restore file and git restore --staged file?

    restore without --staged overwrites the working file from the index; with --staged it resets the index entry and leaves your edits on disk.
  3. 3. When does a merge create no merge commit by default?

    If the current branch is an ancestor of the branch being merged, Git just moves the pointer (fast-forward) unless --no-ff is given.
  4. 4. You committed debug.log by mistake and added *.log to .gitignore, but it still shows as tracked. What is missing?

    Ignore rules only affect untracked files. A file already in the index stays tracked until you remove it with git rm --cached.
  5. 5. Why does a cherry-picked commit get a different hash than the original?

    A commit hash is computed from its content, tree, parents, author and committer data, so a new parent means a new hash.

Summary

  • git add decides what the next commit contains; git add -p lets you choose hunks inside a file.
  • git commit --amend fixes the last commit; use it only before you share it.
  • git restore discards working-tree edits, git restore --staged unstages, and git rm --cached stops tracking a file while keeping it on disk.
  • A merge fast-forwards when it can; --no-ff forces a merge commit; overlapping edits produce a conflict you resolve, git add and commit.
  • git stash parks edits, git tag -a marks releases, git cherry-pick copies single commits, and git revert undoes a commit without rewriting history.

Next: Practice Problems: Rewriting, Remotes and Internals