Intermediate Git Tutorial
This is an opinionated tutorial targeting people who have a basic understanding of the git CLI but want to understand more advanced concepts. This guide assumes you know how to clone a repo, add commits, and create branches. I also will assume you know the basics of a UNIX/Linux shell.
Getting Started
When you are learning a new git operation I strongly advise doing it from the CLI first then using your preferred GUI later. The overwhelming majority of git GUIs (especially GitHub desktop) encourage you to use workflows that are not particularly good and hide away some of the most important tools in git like rebase. If you are stuck on Windows, the git CLI can be accessed from the “git bash” program. This will plop you in a standard UNIX style shell which you can access the git program.
Official git documentation
The official git documentation is really well written and takes the form of two items. The first is the pro git book which does a great job of teaching you everything you wanted to know about git and more and is intended as a starting point for new users. Once you have a passing understanding of git they provide excellent documentation in the form of UNIX style man pages. You can access these by running the following command in your shell or on the git website
man git-<subcommand>
# eg if you wanted to look at the docs for git-rebase you would do
man git-rebase
The difference between git and GitHub
If you already understand the difference feel free to skip this section.
Microsoft has been on a quest to conflate git and GitHub into the same thing as with tools like GitHub desktop which has led to a large number of newer developers who do not actually understand the difference.
When people say git, they are referring to the program that runs on your computer. It is accessed with the git binary from a terminal or by a GUI which uses libgit2.1 git does not care at all about where you store your code. You could store it on GitHub or another website like Codeberg, host something yourself like git.notaslug.org, or not publish it anywhere at all! Websites that host git repos along with additional tooling are typically called “git forges”.
Git actually has no concept of what Github calls a pull request2. Git was originally built for the linux kernel which uses an entirely email driven workflow. I actually prefer this style of workflow but it seems like almost everyone else in the world disagrees with me. Check out git-send-email.io if you are curious about email driven workflows.
Git also has no concept of a fork. We will go over this a bit more shortly.
When you interface with GitHub specific features in something like GitHub desktop or JetBrains IDEs, what it is going on under the hood is the application is actually creating an API request to GitHub and preforming that action on GitHub’s servers.
Clone the tutorial repo
With all that nonsense out of the way let’s start by cloning the tutorial repository. This contains some commits we can work with.
git clone 'https://git.notaslug.org/git-tutorial.git'
You will see this is a basic repo with a few text files, a hello world program with a bug, and a README.
Remotes
What is a remote?
A remote is just a key value pair of a name to a link so you don’t have to type the entire link every time you do a git operation. That is it.
If you don’t believe me go your favorite git repo and open up .git/config3 in your favorite text editor. On my system this looks like:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
# TODO: Change this to HTTPS URL - if i publish this i stg
url = git@git.notaslug.org:git-tutorial.git
fetch = +refs/heads/*:refs/remotes/origin/*
Remotes don’t actually even need to be for the same repository. For instance I could add the remote for linux and ffmpeg to the same clone of a repository and git would not care.
Pulling in changes from another remote
If you are using a github GitHub style website and you have cloned your fork rather than the original repository by mistake an alternative to syncing changes in the GitHub UI is the following:
# Try it yourself!
git remote add upstream https://git.notaslug.org/git-tutorial-upstream.git
# Option 1 (if you don't have local changes)
git fetch upstream
git merge --ff-only upstream/master
# Option 1 shortcut
git pull --ff-only upstream master
# Option 2 (if you have local changes)
git fetch upstream
git rebase upstream/master
# Option 2 shortcut
git pull --rebase upstream master
DO NOT USE MERGE-BASED GIT PULL4. It makes your commit history incredibly ugly and everyone will laugh at you. Some projects like the Linux kernel even forbid them. Of course Microsoft has made this the default in their UI. In general, just don’t do anything other than squash merges on GitHub. If you want to do a normal merge or a rebase merge do them locally and close the PR with a nice thank you message like the curl project.
Rebase
You may have noticed above that I advised using git rebase when pulling changes from an upstream. This makes it such that your local changes will be the newest changes in the git history rather than intermixed with upstream changes. This makes it really easy to clean up your branch history (and take advantage of other git features)!
Using rebase to rebase
The most simple use case for git rebase is to take all of the commits unique to a branch and re-apply them to another branch (or a later version of the same branch). This is easiest to understand graphically:
Before rebase:
Branch 1: a - b - c - d - e
Branch 2: \ f - g
Branch 3: \ h - i
Rebasing branch 3 onto branch 1
Branch 1: a - b - c - d - e
Branch 3: \ h - i
Branch 2: \ f - g
Rebasing Branch 3 onto Branch 2:
Branch 1: a - b - c - d - e
Branch 2: \ f - g
Branch 3: \ h - i
Hopefully this is understandable. This can be done by checking out the branch you want to rebase and running the git rebase command. Try it below!
# Checkout our outdated branch
git checkout my-feature-branch
git rebase master
Fixing commit history
If you messed up the last commit (eg. forgot to git add something or typoed the commit message) you can always amend the commit5:
git commit --amend
If there is a more complicated problem you can use an interactive rebase. This is done by running the following command
# HEAD~5 means "the last five commits" starting from the one
# checked out
git rebase -i HEAD~5
This will open your text editor. The contents of your editor should explain what to do next. I don’t really have much else to add beyond what should be right in front of your face right now.
I would advise experimenting with interactive rebasing a bit. If you want to learn more about rebases check out git-rebase.io.
Force pushing
If you are rebasing your will inevitably run into scenarios where your git remote rejects your changes because you modified git history. This is okay! All you need to do is append --force to your git push. Just make sure that you only force push to branches that only you use. Do not force push to shared branches.
Example:
# You wont be able to run this in the example repo but it is here for
# your reference
git push origin my-feature-branch --force
Cherry Pick
Cherry picking is the process of copying a commit from one branch to another. Visually this looks like:
Branch 1: a - b - c - d - e
Branch 2 \ f - g
Cherry pick commit e onto branch 2
Branch 1: a - b - c - d - e
Branch 2 \ f - g - e
This is useful if there are multiple forks or branches for a project and they all fix different things or add different features that you want in your version of the repo.
Let’s cherry pick a commit from one feature branch to another.
git checkout my-feature-branch
git log cherry-pick-me
# copy the hash of the commit you want to pick
git cherry-pick <hash>
If you want to cherry pick something form another repo you can add it as a remote, then fetch the remote, then cherry pick just the commit you want. The repos don’t even need to be from the same parent!
Bisecting
Git bisect is used to identify the commit that introduced a regression! It is easiest to explain with an example. In the tutorial repo there is a regression in hello.py. The file is supposed to print “hello world” but right now it is actually printing “goodbye world”.
To work, we need to supply git bisect with a known good commit and a known bad commit. It will then assist you with doing a binary search to identify the commit which introduced the regression.
I have identified 42fece97c4c99739013ec01231487065aa84bed5 as a commit where hello.py was working properly.
Lets start bisecting.
# HEAD means the commit we currently have checked out
git bisect start HEAD 42fece97c4c99739013ec01231487065aa84bed5
Now lets run the program and see if it works properly
# If the program works as expected run this:
git bisect good
# If it does not work run this:
git bisect bad
After you are done it will tell you the commit which introduced the issue. If you don’t need the rest of the commit you can revert it by doing
git bisect reset # exit the bisect
git revert <commit-hash>
If you want to modify the revert before committing use
git revert -n <commit-hash>
Limitations
Git bisect is a powerful bug finding tool but only works under the following conditions
- The bug is a regression
- The bug was introduced as a result of a single commit
- We can identify a somewhat recent working commit
Un-fucking your branch
The overwhelming majority of the time when somebody tells me their “branch is fucked” they could actually fix it with an interactive rebase. However, if you are doing a git operation that rewrites git history and mess it up massively you will likely need to turn to:
git reflog
It works basically the same as git log except it also has a log of history changes and is very verbose. You can checkout each of the various hashes listed as normal.
The website Oh shit git also has a ton of common “fucked branch” cases and how to get out of it.
Useful guides
These were referenced throughout the text but are included here for quick reference
Meta
Feel free to contact me with any questions, clarifications, and improvements at dffdff2423@gmail.com
I will likely be updating this guide in the future as I get asked questions or feel like I need to include another topic since I want to actually publish this at some point.
The contents of this article are released under the CC BY-NC-SA 4.0 License which means you are free to copy and modify the contents of this article so long as you provide credit and do not use it for commercial reasons.
-
Technically what I described are implementations of git. Git is really just a standardized format for a directory called ‘.git’. That is it. You can even code your own git! ↩︎
-
git actually has something called a pull request but it has nothing to do with the github version of it. Most other GitHub style websites use the term merge request for this reason. ↩︎
-
You may need to enable “show hidden files” in your file manager to view this directory. ↩︎
-
Fine. They have their uses (eg. upstream merges) but those are all advanced use cases and out of scope for this guide. If you don’t know why you are using a git merge don’t use a git merge. ↩︎
-
I was not sure where to put this. Some entry level git guides advise using
git commit -m "blah"to write the message for your commits. This is generally considered to be bad practice. If you omit the-m "blah"your text editor will open and allow you to write a more detailed multiline commit message. After you are done save and quit your editor. It is convention to keep the first line 72 characters or less and write more detailed information after a blank line. ↩︎