Advanced Git
New Year, New Git Skills!
So you’ve started using git for version control but are still puzzled by things as rebasing, merge conflict resolution, and pull request workflows? Then this Programming Café is for you! We’ll help you practice with intermediate-to-advanced uses of git, and also showcase some less known but handy ways in which you can use git during your development process.
Basic knowledge of git (e.g. cloning a repository and staging/committing your changes) is assumed for the audience of the presentation.
For beginners, we have a set of suggested exercises (and helpers) to get started with Git and GitHub.
For advanced users, we may also delve into some of git’s “plumbing” to help you understand just what is going on when you type “git commit”.
Slides
Exercises
The exercises below are available as a separate file, if more convenient.
New to git?
If you’re completely new to git, or need to refresh your memory, just start with the basics!
Beginners
If you’re a beginner, we recommend that you do the following exercises/tutorials from the CodeRefinery course Introduction to version control with Git:
- Clone this repository to do the exercises in.
- Branching
- Conflict resolution
- Pull Requests
- The Staging Area
If you have time left, proceed with the exercises from the Intermediate/Advanced section below, focusing on the following:
Intermediate/Advanced
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!
Exercise1 SimpleCommit
Learn about git’s internals by inspecting a commit under the hood.
Additional exercises
- Create a subdirectory
subdir
and create a filehello2.txt
in it, with content such asHello to you too!
. - Commit
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
Exercise2 Staging and Stashing
Exercise Two - Staging and Stashing
Learn to exercise precise control over what is added to your commits, with git add -p
and git stash
.
Additional exercise
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.
Exercise7 RebaseAndAmend
Exercise Seven - Rebase and Amend
Additional exercises
- The exercise in the tutorial has you use
git commit --amend
to change a commit’s message. Can you also useamend
to change the contents of the files within the commit? - Most difficult: the exercise in the tutorial has you use
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 userebase -i
to split this commit into two atomic commits? (Hint: you’ll need more commands than justrebase
). Solution. - Similarly, can you use
rebase -i
to completely delete an unwanted commit, as well? - Oops! You just deleted a commit and it’s all gone from your branch. This is one of the ways in which you could (accidentally) lose work while rebasing. But git wouldn’t be git if you couldn’t undo a rebase. Can you figure out how to restore your previous commit? Hint Solution
Exercise4 Merging and ReReRe
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!
Exercise9 Advanced Tools
Notes:
cherry-pick
can be very useful for creating atomic git histories. It essentially does the inverse ofrebase
: 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 tocherry-pick
the latter on yourmain
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!