Advanced Git

Author

Dawa Ometto

Published

January 11, 2024

Note
  • Date: Thursday, 11th January 2024
  • Time: 15:00-17:00
  • Location: Bucheliuszaal 6.18, University Library (Utrecht Science Park)
  • Presentation: Advanced Git
  • Presenter: Dawa Ometto

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.

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:

  1. Clone this repository to do the exercises in.
  2. Branching
  3. Conflict resolution
  4. Pull Requests
  5. The Staging Area

If you have time left, proceed with the exercises from the Intermediate/Advanced section below, focusing on the following:

  1. Exercise2 Staging and Stashing
  2. Exercise7 RebaseAndAmend
  3. Exercise4 Merging and ReReRe

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
  1. Create a subdirectory subdir and create a file hello2.txt in it, with content such as Hello to you too!.
  2. 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
  1. The exercise in the tutorial has you use git commit --amend to change a commit’s message. Can you also use amend to change the contents of the files within the commit?
  2. 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 use rebase -i to split this commit into two atomic commits? (Hint: you’ll need more commands than just rebase). Solution.
  3. Similarly, can you use rebase -i to completely delete an unwanted commit, as well?
  4. 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

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

Exercise9 Advanced Tools

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!