Pro Git Notes

Engineers use version control. I did not use different versions of my work much in college. Therefore, I was not an engineer in college. But now, I do use VCS (git) extensively and enjoy the advantages of being an adequately skilled user of it. Still, sometimes I wish to do some more complex commands. Sometimes, I want to know the details of a particular git-call. These reasons motivated me to read Pro Git about 2 months ago. I read it, learned a lot of cool stuff, and then took a hiatus from my computer so all those cool features I learned have drifted off into some malfunctioning neurons in my brain. Therefore, I am going to reread this book and this time highlight things for future reference as I embark on my journey to becoming an advanced VCS (git) user.

1. Getting Started

  • Types of Version Control
    • local: copy files from one directory to another
    • centralized: one server contains all versioned files (CVS, Subversion, and Perforce)
      • problem is SPOF (single point of failure)
      • good thing is everyone is on the same page most of the time
    • distributed: everyone has their own copies, central server(s) has its copy. People push and pull occasionally to be in sync (Git, Mercurial, Bazaar, or Darcs)
  • Git came out of Linux kernel when the VCS linus torvalds & team used started charging (bitkeeper)
  • Git unique because it is not a delta-based VCS. 
Every commit, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. Link to previous snapshot of file if not changed in next commit. Git thinks about its data more like a stream of snapshots.
  • Git operations are mostly local so network latency unlike other vcs
  • git computes checksums with SHA-1 of your commits which means files can't change or get corrupted without git knowing about it
  • 3 main stages:
    • modified: changed file but not committed to your database yet
    • staged: marked modified file to go into next snapshot
    • committed: data stored in database
  • Working directory --> staging area --> .git repository
  • .git repo --> checkout to working directory. make changes and stage them on a file which is stored in .git repo
  • git config
    • .git/config (local, trumps all other configs "--local")
    • ~/.gitconfig or ~/.config/git/config (global, "--global")
    • /etc/gitconfig:  (system, "--system")
    • view all settings and where they are coming
      • git config --list --show-origin
    • list all settings vanilla
      • git config --list
    • check what git thinks a config value is:
      • git config user.name
    • find origin of a config value:
      • git config --show-origin user.name

2. Git Basics

  • git clone <url> [target directory name] 
  • Git can use several transfer protocol (for url). It can be through https:// or git:// or user@server:path/to/repo/.git (SSH)
  • Short status
    • git status -s
  • Delete file from filesystem and git
    • git rm <filename>
  • Delete file from git but not filesystem
    • git rm --cached <filename>
  • .gitignore file syntax
    • start pattern with '/hello' to avoid recursivity
    • end pattern with 'hello/' to specify directory
    • negate pattern with '!hello'
    • '*' matches 0 or more characters
    • '?ello' matches single character
    • 'a/**/d' to match multiple directories (i.e. would match 'a/b/c/d' and 'a/c/b/d')
    • .gitignore files apply recursively from their root directory. So you can have multiple overlaying each other. Rules apply top to bottom.
  • git diff [filename...] does staged files versus changed but not staged
  • git diff --staged (or --cached) does staged versus file in previous commit
  • git commit -am "My commit" would stage all tracked files and commit automatically
  • git mv <original filename> <new filename> simplifies 3 git commands in 1
  • git log has many options. this is probably the best
    • git log --pretty=format:"%h %s" --graph [-x]where x is number of commits back
  • Undoing things
    • un-stage something you accidentally staged: git reset HEAD <filename>
    • (Danger) undoing a modification to a file: git checkout <filename>
  • working with remotes
    • get data from your remote projects but do not merge: git fetch <remote>
    • git pull origin master automatically fetches and then merges from origin/master branch into your local master branch
    • git remote show origin helps inspect a remote and give tracking information about all branches on a remote
    • git push origin <tagname> or git push origin master --tags
    • git tag -d <tagname> to remove local tag
    • git push origin --delete <tagname> to remove tag from remote

3. Git branching

Commit object, tree object, 3 blob objects
  • branching is the killer feature of git because it is lightweight and instantaneous, branching is recommended in git rather than other VCS
  • Every commit looks like above, taking SHA-1 of subdirectories. Each new commit has pointer to previous commit through a parent key in the commit object (not shown above)
  • branch is a moveable pointer to one of these commits
  • HEAD is the pointer to which branch/tag you are currently checked out on


Comments

Popular Posts