Learning Git

git
linux
Published

September 18, 2021

Intro

I have been always felt tormented while using Git when working with teams or even alone. This below comic from xkcd aptly summarizes my experience with Git.

To finally get comfortable, and efficiently use Git, I went through this lecture(link) from Missing semester course. Trying to summarize and demnstrate my learnings. Recommend this video and lecture notes for detailed walkthrough.

Data model

Terminology:

  • blob - a file (which is a bunch of bytes) is called a blob
  • tree - a directory is called a tree, it maps names to blobs or treees
  • commits - snapshots are called commits.

DAG (directed acyclic graph):

  • Each snapshot in git refers to a set of ‘parents’ (snapshots that preceded it).
  • A snapshot might descend from multiple parents (due to merging)
  • The arrow points to the parent of each commit
  • Commits are immutable

Command line

Basics

  • git help <command>: get help for a git command
  • git init: creates a new git repo, with data stored in the .git directory
  • git status: tells you what’s going on
  • git add <filename>: adds files to staging area
  • git commit: creates a new commit
  • git log: shows a flattened log of history
  • git log --all --graph --decorate: visualizes history as a DAG
  • git diff <filename>: show changes you made relative to the staging area
  • git diff <revision> <filename>: shows differences in a file between snapshots
  • git checkout <revision>: updates HEAD and current branch
    • move around in your version history
    • it not just moves you different brach names, but can also move you with hash ids
    • essentially it moves HEAD around
    • Let’s take a look at decorated logs
    • Let’s move our HEAD to commit #76c0a77c
    • Now if we see git log (the decorated version) - HEAD is moved to the desired commit (snapshot) with hash key (that too not complete hash is required)

Branching & Merging

  • git branch: shows branches
  • git branch <name>: creates a branch
  • git checkout -b <name>: creates a branch and switches to it
    • same as git branch <name>; git checkout <name>
  • git merge <revision>: merges into current branch
  • git mergetool: use a fancy tool to help resolve merge conflicts
  • git rebase: rebase set of patches onto a new base

Remotes

  • git remote: list remotes
  • git remote add <name> <url>: add a remote
  • git push <remote> <local branch>:<remote branch>: send objects to remote, and update remote reference
  • git branch --set-upstream-to=<remote>/<remote branch>: set up correspondence between local and remote branch
  • git fetch: retrieve objects/references from a remote
  • git pull: same as git fetch; git merge
  • git clone: download repository from remote

Advanced git

  • git config: Git is highly customizable
  • git clone --depth=1: shallow clone, without entire version history
  • git add -p: interactive staging
  • git rebase -i: interactive rebasing
  • git blame: show who last edited which line
  • git stash: temporarily remove modifications to working directory
  • git bisect: binary search history (e.g. for regressions)
  • .gitignore: specify intentionally untracked files to ignore