If you’re completely new to git, or need to refresh your memory, just start with the basics!
If you’re a beginner, we recommend that you do the following exercises/tutorials from the CodeRefinery course Introduction to version control with Git:
If you have time left, proceed with the exercises from the Intermediate/Advanced section below, focusing on the following:
If you are a more advanced git
user and are already familiar with the concepts of branching and merging, we recommend that you pick exercises from the Advanced Git In Depth course by Nina Zakharenko that cover topics you don’t know yet.
Use the following repository to complete the exercises: https://github.com/nnja/advanced-git-exercises
We recommend first picking the exercises listed below. For some of these topics, we’ve also added additional challenges!
Learn about git’s internals by inspecting a commit under the hood.
subdir
and create a file hello2.txt
in it, with content such as Hello to you too!
.subdir/hello2.txt
Now, using at most the following three commands, can you output the contents of the file you just committed, hello2.txt
?
git log
git ls-tree
git cat-file
Exercise Two - Staging and Stashing
Learn to exercise precise control over what is added to your commits, with git add -p
and git stash
.
Can you figure out how to use git stash
to stash everything that is not staged for commit? (Hint: you’ll need to pass two options to git stash
). Solution.
Exercise Seven - Rebase and Amend
git commit --amend
to change a commit’s message. Can you also use amend
to change the contents of the files within the commit?rebase -i
to squash some commits. This allows you to combine a number of commits that each contain too little to amount to an atomic commit into one. But suppose you have a commit in your history that contains too much to amount to an atomic commit – i.e. a commit that changes two logically distinct bits of code. How can you use rebase -i
to split this commit into two atomic commits? (Hint: you’ll need more commands than just rebase
). Solution.rebase -i
to completely delete an unwanted commit, as well?Reuse Recorded Resolution is a feature that lets you automate merge conflict resolution (for similar/recurring merge conflicts). This can be especially handy when rebasing, as rebasing can require you to solve the same merge conflict multiple times!
Notes:
cherry-pick
can be very useful for creating atomic git histories. It essentially does the inverse of rebase
: rather than replaying the changes on the current branch on top of (typically) main
(rebase), it replays the changes from a commit on a different branch on top of the current branch. If your feature branch is a mess, but contains some commits that are nice atomic gems, you may be able to cherry-pick
the latter on your main
branch!git bisect
is a great debugging tool, especially when used in its automatic mode in conjunction with unit tests. Give the example here a go!