Each exercise starts with a broken or messy repository. Your job is to fix it using the git commands you have learned. Read the setup output and the starting state of the repo before doing anything.
When you finish each exercise, run the commands listed in the Submit section for that exercise and paste the output into a plain text file named answers.txt. Label each answer clearly.
=== Exercise 1 ===
<paste output here>
=== Exercise 2 ===
<paste output here>
Upload answers.txt and the entire application-day folder (zipped) to Gradescope. The zip must include the exercise/ subdirectory for each exercise you completed.
cd 01-merge-conflict
source setup.sh
cd exercise
Two branches both added a file called file.txt with different content. Git cannot merge them automatically. Both lines need to end up in the file.
- Run
git log --oneline --graph --allto see the two branches and where they diverged. - Merge
merge-conflict-branch1intomaster. - Open
file.txtin an editor. Remove the conflict markers and keep both lines. - Stage
file.txtand complete the merge. - Run
git log --oneline --graphto confirm the merge commit is there.
git log --oneline --graph
cat file.txt
cd 02-reset
source setup.sh
cd exercise
The repository has 10 commits, numbered 1 through 10. You will use reset to step backward through history and observe what changes in each mode. Then you will use revert to undo a specific commit without rewriting history.
- Run
git log --onelineto see the starting state. - Run
git reset --soft HEAD~1. Checkgit statusandgit log --oneline. Where did commit 10 go? - Run
git reset --mixed HEAD~1. Checkgit statusandgit log --oneline. What is different from--soft? - Run
git reset --hard HEAD~1. Checkgit statusandgit log --oneline. What changed compared to--mixed? Note that9.txtand10.txtare still present even though their commits are gone. Why? - You are now at commit 7. Run
git revert HEAD~1to undo commit 6. Accept the default commit message. - Run
git log --oneline. You should see 8 commits. The most recent should be a revert commit.
git log --oneline
ls
cd 03-detached-head
source setup.sh
cd exercise
You opened an old commit to inspect something and git is now telling you that HEAD is detached. Any commits you make in this state are not attached to any branch and git will warn they may be discarded.
- Run
git status. Read git's description of the current state. - Run
git log --oneline --graph --all. Find where HEAD is and wheremasteris. - Create a file called
hotfix.txt, add it, and commit it with the messagehotfix: emergency patch. This commit is now floating with no branch pointing to it. - Run
git log --oneline --graph --allagain to see the floating commit. - Create a branch called
hotfixat your current position to save that commit:git branch hotfix - Switch back to master:
git checkout master - Cherry-pick the hotfix commit onto master.
- Run
git log --oneline --graph --allto confirm the final state.
git log --oneline --graph --all
ls
cd 04-save-my-commit
source setup.sh
cd exercise
The repository was reset to an early commit. A file called holygrail.txt is gone from the working directory. The commit that added it still exists in git's object store, it just is not reachable from any branch. git reflog records every position HEAD has been at, including ones that are no longer in the branch history.
- Run
git log --onelineto see the current short history. - Run
lsto confirmholygrail.txtis missing. - Run
git reflogto find the commit with messagefound the holy grail. - Use
git cherry-pick <sha>to bring that commit onto master. - Run
lsandcat holygrail.txtto confirm the file is back.
git log --oneline
ls
cat holygrail.txt
cd 05-bad-commit
source setup.sh
cd exercise
One of the five commits on master introduced a file called badfile that should never have been committed. You do not know which commit it was. Use git bisect to find it by binary search, then use git revert to undo it without rewriting history.
A commit is bad if badfile exists in the working directory. A commit is good if it does not.
- Run
git log --onelineto see the history. - Start bisect:
git bisect start - Mark the current state as bad:
git bisect bad - Find the first commit SHA from
git log --onelineand mark it good:git bisect good <sha> - For each commit git checks out, run
lsto check whetherbadfileexists. Mark itgit bisect goodorgit bisect badaccordingly. - When bisect identifies the bad commit, run
git bisect resetto return to HEAD. - Use
git revert <bad-commit-sha>to revert the bad commit. Accept the default commit message. - Run
lsto confirmbadfileis gone. Rungit log --onelineto confirm the revert commit is there.
git log --oneline
ls