Coding Challenges: Git Fundamentals
8 HackerRank-style challenges, auto-graded right in your browser
— no local install needed, standard library only. This
chapter teaches git itself, not Python syntax, so every challenge
is a Python function that simulates or reasons about
real git behavior — predicting what git log,
git merge, or git rebase would actually do,
given a history represented as plain data. Write your function in
the code box, then click Run Tests to check it
against a set of test cases (including edge cases). Stuck? Every
challenge has a Show Solution reveal at the bottom.
1 Is a Fast-Forward Possible? Easy
Write a function is_fast_forward_merge(target_commits, source_commits). Both arguments are lists of commit ids representing a branch's full history from the root commit to its tip, in order. A fast-forward merge of source_commits into target_commits is possible only when target_commits is an exact, in-order prefix of source_commits (the target branch hasn't diverged -- every one of its commits also appears, in the same order, at the start of the source branch's history). Return True or False.
is_fast_forward_merge(["c1"], ["c1", "c2"]) → Trueis_fast_forward_merge(["c1", "c3"], ["c1", "c2"]) → False
Show Solution
def is_fast_forward_merge(target_commits, source_commits):
n = len(target_commits)
if n > len(source_commits):
return False
return list(source_commits[:n]) == list(target_commits)
2 Does the Merge Create a New Commit? Easy
Write a function merge_produces_new_commit(is_fast_forward). A fast-forward merge just moves the target branch's pointer forward -- no new commit object is created. A three-way merge (when the branches have diverged) always creates a new merge commit. Given whether the merge is a fast-forward, return True if it produces a new commit, else False.
is_fast_forward is a boolean.
merge_produces_new_commit(False) → Truemerge_produces_new_commit(True) → False
Show Solution
def merge_produces_new_commit(is_fast_forward):
return not is_fast_forward
3 Build a Conflict Marker Block Easy
Write a function build_conflict_block(current_branch_line, incoming_branch_line, incoming_branch_name) that returns the 5 lines git would show for a single-line merge conflict, as a list of strings, in git's real order: the <<<<<<< HEAD marker, the current branch's (HEAD's) content, a ======= divider, the incoming branch's content, and a >>>>>>> <branch name> marker.
build_conflict_block("MAIN-CHANGE", "FEATURE-CHANGE", "feature") →["<<<<<<< HEAD", "MAIN-CHANGE", "=======", "FEATURE-CHANGE", ">>>>>>> feature"]
Show Solution
def build_conflict_block(current_branch_line, incoming_branch_line, incoming_branch_name):
return [
"<<<<<<< HEAD",
current_branch_line,
"=======",
incoming_branch_line,
">>>>>>> " + incoming_branch_name,
]
4 What's Staged but Not Committed? Easy
Write a function staged_but_not_committed(added_files, committed_files) that returns the list of filenames that have been git add-ed but not yet included in a commit -- i.e. the filenames present in added_files but not in committed_files, preserving added_files's order.
added_files may contain names not present in committed_files and vice versa.
staged_but_not_committed(["a.py", "b.py"], ["a.py"]) → ["b.py"]
Show Solution
def staged_but_not_committed(added_files, committed_files):
return [f for f in added_files if f not in committed_files]
5 Pick the Right Git Command Medium
Write a function pick_git_command(scenario) that returns the git command (as a plain string) that matches a described scenario. Support exactly these scenario strings, mapped to these commands: "see what changed but not staged yet" → "git diff"; "see what changed and is staged" → "git diff --staged"; "undo a commit that has already been pushed and shared" → "git revert"; "temporarily set aside uncommitted changes" → "git stash"; "see history of commits" → "git log"; "create and switch to a new branch in one step" → "git checkout -b". For any scenario string not in this list, return "unknown scenario".
scenario is a string, matched exactly.
pick_git_command("see history of commits") → "git log"
Show Solution
def pick_git_command(scenario):
mapping = {
"see what changed but not staged yet": "git diff",
"see what changed and is staged": "git diff --staged",
"undo a commit that has already been pushed and shared": "git revert",
"temporarily set aside uncommitted changes": "git stash",
"see history of commits": "git log",
"create and switch to a new branch in one step": "git checkout -b",
}
return mapping.get(scenario, "unknown scenario")
6 Commit Count After a Rebase Medium
Write a function commit_count_after_rebase(target_commit_count, source_new_commit_count). When you rebase a feature branch onto a target branch, git replays only the feature branch's commits that aren't already on the target -- one at a time -- on top of the target branch's current tip, producing a single linear history. Given how many commits the target branch has, and how many NEW commits the feature branch added since it diverged, return the total number of commits in the resulting linear history after the rebase.
commit_count_after_rebase(2, 3) → 5
Show Solution
def commit_count_after_rebase(target_commit_count, source_new_commit_count):
return target_commit_count + source_new_commit_count
7 The Safe Undo Choice Medium
Write a function safe_undo_choice(already_pushed_and_shared). When a commit is still local and nobody else has it, git reset is a fine way to undo it. Once a commit has already been pushed and other people have pulled it, rewriting history with reset breaks everyone else's copy -- git revert (which adds a new commit instead of erasing history) is the safe choice instead. Return the string "revert" or "reset", whichever is safe for the given situation.
already_pushed_and_shared is a boolean.
safe_undo_choice(True) → "revert"safe_undo_choice(False) → "reset"
Show Solution
def safe_undo_choice(already_pushed_and_shared):
return "revert" if already_pushed_and_shared else "reset"
8 Can the Reflog Recover It? Medium
Write a function can_recover_with_reflog(reflog_has_entry, garbage_collected). After a git reset --hard moves a branch backward, the commits it moved away from stop showing in git log but are NOT deleted right away -- they're still reachable through git reflog, and can be recovered with git reset --hard <reflog-hash>, as long as two things are both true: the reflog still has an entry pointing at them, and git's garbage collector hasn't already cleaned them up. Return True only when recovery is genuinely still possible.
can_recover_with_reflog(True, False) → Truecan_recover_with_reflog(True, True) → False
Show Solution
def can_recover_with_reflog(reflog_has_entry, garbage_collected):
return reflog_has_entry and not garbage_collected