Thinking in Git



Git is the most popular source code management and version control system in the open source community. Its complexity and power make it the best choice for most projects, while simultaneously giving it a daunting learning curve for newcomers. This talk will assume no background knowledge of version control, and will teach the basics of Git and GitHub in order to give you an accurate mental model of what the tool does, and help you fix mistakes then ask the right questions if you run into problems using it later.




You won't memorize all the commands in 2 hours but this will help you ask the right questions.

Thinking about Software Development


Why version control?


How do you track changes?


Goals of Distributed Version Control


Using Git


We're going to talk about a lot of commands now.

Don't be afraid. Don't expect to know everything at first.

These slides are online; the link will show up again at the end.

Setting Up


The name and email you tell Git will be visible to everyone you share your commits with. If you use a public GitHub repo, that's the entire world.

ECDSA -- elliptic-curve digital signature algorithm -- gives much smaller keys with comparable security

GitHub can handle ECDSA, GitLab only does RSA as of 5.1.0


You can time travel through the history of any project!


This assumes that you have some number of projects you work on, each one has a history of changes, and those histories are tracked separately. A repository is the basic unit of a directory whose changes we want to track.

What's a repository?


Database of snapshots of your code

Universe whose history you can travel through

Getting a repo

$ git init # Make a brand new repo

$ git clone <git clone url> # Start with a copy of another


Looking at a repo

$ ls .git/

$ git show
fatal: bad default revision 'HEAD'
# To be expected with nothing in the repo

$ git show
fatal: Not a git repository (or any of the
       parent directories): .git
# not in a repo

$ git log

Undo repository creation


This deletes your history. Only do it if you really want to stop having a Git repo here.

$ rm -rf .git


What if you had to publish every change as soon as you made it?

How Git sees your project

Unstaged | Staged | Committed



It would be simpler to understand the system if we only let you commit one file at a time, but it's more important to have total control of what changes go into what commit.

Git gives you a staging area where you can get a set of changes just right, before setting them in stone.


You decide exactly where time travelers are allowed to land.

What're staged changes?


Staging changes

$ echo "hello Great Wide Open" > foo
$ git add foo

Looking at staged changes

$ touch bar
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..."
   to unstage)
    new file:   foo
    Untracked files:
      (use "git add <file>..." to include
       in what will be committed)
$ git commit --dry-run


$ git rm --cached foo
$ git reset HEAD foo


Time travelers get some signs and instructions when they arrive


Staging changes is all about deciding exactly what state it should be easy to go back to. Creating a commit adds some useful metadata to a snapshot of your repository.

Thinking about snapshots


What's a commit?

snapshot of changes, author, date, committer (can differ from author), parent commit


Making a commit

$ git commit
$ man git-commit
-a, --all
-i, --interactive
--date=<date> (see DATE FORMATS in man page)
-o, --only
-S, --gpg-sign


-o is for only files from command line disregarding the stash Specifying file names disregards staged changes, plus stages all current contents

Looking at commits

# details on latest or specified
$ git show

# Summary of recent, or a range
$ git log

$ man gitrevisions # ranges

What about commits per file?

$ git blame <file>

Commit display options

$ git show

$ git show --oneline

# see PRETTY FORMATS section of
$ man git-show

# Check the GPG signature
$ git show --show-signature

# Want a GUI?
$ gitk


# just one file
$ git checkout <commit> <filename>
$ git add <filename>
$ git commit -m "i put that file back how it was"

Or undo the whole commit

$ git revert <commit to revert to>


next: remotes Reverting makes a revert commit. Reversability > hiding mistakes


Time travelers get a list of especially interesting locations to visit

What's a tag?


Adding a Tag

$ man git-tag
$ git tag -m <msg> <tagname>

Default is lightweight tag -- just a reference for SHA-1 of latest commit

Pass -s or -u <key-id> to GPG-sign

Looking at Tags

# List all available tags
$ git tag

# List tags matching regex
$ git tag -l 'regex'

# I want this version!
$ git checkout <tag name>


$ git tag -d <tagname>

# And remove it from a remote repo
$ git push origin :refs/tags/<tagname>


You can work on separate sets of changes that don't affect each other

What's a branch?


A parallel path of development, starting from a commit that's in the tree


Point out why the arrows are "backwards"

Making a branch

# track remote branch by default if one matches
$ git checkout -b <branchname>

# Shorthand for:
$ git branch <branchname>   # create
$ git checkout <branchname> # check out

# Pushing a branch to a remote
$ git push <remotename> <branchname>

Looking at branches

$ git branch

$ git show <branchname>



GitHub's "network" graph and gitk are good for this


# delete only if fully merged
$ git branch -d

# Delete, I Don't care what I lose
$ git branch -D

# delete remote branch
$ git push <remotename> :<branchname>


Someone else could work on the same repo in a parallel universe


Whenever you get multiple people working on the same project, they'll want to make different changes and then bring them back together. To do this, Git needs to let history continue in two different directions and then bring the changes from each back together.

What's a remote?


Another clone of more or less the same repo

(remember when we cloned to get a copy?)


Adding a Remote

$ man git-remote

$ git remote add <name> <url>


Looking at Remotes

$ git config -e

# OR

$ git remote show <name>

From one of my git configs...

[remote "origin"]
  url =
  fetch = +refs/heads/*:refs/remotes/origin/*
[remote "edunham"]
  url =
  fetch = +refs/heads/*:refs/remotes/edunham/*


Do you prefer text editor...

$ git config -e
# delete or change remote

... or commands?

$ man git-remote
$ git remote rename <old> <new>
$ git remote remove <name>


"Undoing" push to remote is... trickier next: tags

What's a merge?



"a group of developers is called a merge conflict"

Making a Merge

# Branch you're changing
$ git checkout mywork

$ git merge master

# Merge conflicts?
$ git status
    On branch mywork
    You have unmerged paths.
      (fix conflicts and run "git commit")



Merge Conflicts

<<<<<<< HEAD
This content was in mywork but not master
This content was in master but not mywork
>>>>>>> master

Looking at Merges

$ git diff <commit before> <merge commit>

# before merging, see changes
$ git log ..otherbranch
$ git diff ...otherbranch
$ gitk ...otherbranch


$ git merge abort
$ git reset --keep HEAD@{1}

What's a rebase?


Changing history. Means others will have to force pull.


Don't do this unless you know what you're doing... But here's how to know what you're doing.


$ git rebase -i <commit range>
                # last 4 commits

# Oops I forgot to pull
$ git pull --rebase

Looking at the rebase

# Rebase 1a20f51..147c812 onto 1a20f51
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.


Make sure you have your git editor set!


I should never have done that

$ git reset --hard ORIG_HEAD

I'm stuck in a broken rebase, get me out

$ git rebase --abort



Not Exactly Git


Watch Linus's talk for more detail

Getting Started

HTTP vs SSH Clones

Permission denied (publickey).
fatal: Could not read from remote

Please make sure you have the
correct access rights and the
repository exists.

HTTP clone prompts for username and password

SSH clone uses key from your account



Pull Requests


Annoying Tricks


Extra Features

Additional GitHub tricks

Continuous Integration


Playing Well With Others



Other Stuff


$ git checkout branch

point HEAD at the tip of the specified branch

$ git checkout <revision> file


$ man gitrevisions

git bisect

Binary Search:

git bisect start
git bisect bad <commit>
git bisect good <commit>
git bisect next
git bisect reset <commit>

git cherry-pick

$ git checkout <branch that needs special commit>
$ git cherry-pick <special commit from another branch>

git format-patch

$ git format-patch origin/master
# I wonder what this patch does
$ git apply --stat 0001-first-commit.patch

# Let's merge!
$ git apply 0001-first-commit.patch

# Does your project use signed-off-by?
$ git am --signoff < 0001-first-commit.patch