Git


















WHOAMI

The name is ThePrimeagen

Twitch
ThePrimeTimeagen - Engineering News
ThePrimeagen - Hand Crafted
















The Goal

To teach your git well. Not all the commands, but enough that you git it


















Before we git we man

man has all the info



Lets execute the following command. This will open up the man page for man. If you don't know, man stands for manual.

man man


Or to see all the git commands

➜  fem-git git:(main) man git- # press tab for auto complete with zsh
zsh: do you wish to see all 147 possibilities (49 lines)?

















There is a surprisingly long manual for how to read the friendly manual (RTFM), so to keep things short i'll give you some short cuts you need to know and that is it.

  • j goes one line down
  • k goes one line up
  • d goes one half page down
  • u goes one half page up
  • /<term> will search for term
  • n goes to next search term
  • N goes to prev search term

















EXERCISE

In man pages you see bold terms. Search for the term bold and find out what it means. Here is an example, from man man, of bolded terms

SYNOPSIS
       man [man options] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [man options] [section] term ...
       man -f [whatis options] page ...
       man -l [man options] file ...
       man -w|-W [man options] page ...

















Solution

       bold text          type exactly as shown.
       italic text        replace with appropriate argument.
       [-abc]             any or all arguments within [ ] are optional.
       -a|-b              options delimited by | cannot be used together.
       argument ...       argument is repeatable.
       [expression] ...   entire expression within [ ] is repeatable.

















What is git?

Git is a distributed version control system, VCS. Instead of the traditional centralized control system where even checking out a file required admin priviledges, git allows any work to be locally and may diverge as much as you want.


















Git

In Git, commands are divided into high level ("porcelain") commands and low level ("plumbing") commands.



We will primarily use the high level commands, but will dip down into the low level commands to really understand how git works.
















Key Terms

  • repo: a git tracked project



  • commit: A point in time representing the project in its entirety.

    • The sha that represents a commit, 40 a-f 0-9 characters, is calculated from contents of change, author, time, and more


  • index: I will use this term or staging area interchangeably. From the github blog

The Git index is a critical data structure in Git. It serves as the “staging area” between the files you have on your filesystem and your commit history. When you run git add , the files from your working directory are hashed and stored as objects in the index, leading them to be “staged changes”.



  • squash: to take several commits and turn it into one commit
    • technically a squash would be taking N commits and turning it into N - 1 to 1 commit. but typically its N commits to 1 commit


  • work tree, working tree, main working tree: This is your git repo. This is the set of files that represent your project. Your working tree is setup by git init or git clone.


  • untracked, staged, and tracked: (use excalidraw)
  1. untracked files. this means files that are not staged for the first time (indexed) or already committed / tracked by the repo. These files are the easiest to accidentally lose work on since git does not have any information about these files.



  2. indexed / staged: this is where the changes are to be committed. You must stage before you commit and you stage changes by using the git add command. see man git-add for more information



  3. tracked. These are files that are already tracked by git. Now a file could be tracked AND have staged changes (changes stored in the index) ready to be committed.



  • remote: The same git repo on another computer or directory. You can accept changes from or potentially push changes too. Think github

















Key Facts About Git



  • git is an acyclic graph, meaning the following cannot exist


  • in git, each commit is a node in the graph, and each pointer is the child to parent relationship


  • if you delete untracked files they are lost forever. commit early, commit often, you can always change history to make it one commit (squashing)


  • man git-<op> for the friendly manual

















A Warning

A lot of peoples experience with git can be summed up in these five commands

  • push
  • pull
  • add
  • commit
  • status


Everything else is ... mysterious and painful (if this is you, fantastic, this course is MOSTLY designed for you)



This course can take someone with zero knowledge and get them up to speed on git. That means I will be interweaving simple items with complex items throughout this course. If you try to skip a head you may miss out on a key conflict, or a way to use git log or reflog.



The reality is if you are comfortable with

  • rebase
  • rerere
  • reflog
  • log
  • cat-file
  • config
  • reset

this course is probably not for you. You already know the 97% of git.


















Git State

We will be doing some changes to the default settings throughout this course.

git config --get --global init.defaultBranch

master # or any name to show up here

## PLEASE DO THIS
git config --global --unset init.defaultBranch

Ensure that rerere isn't true

git config --global rerere.enabled
true
git config --global --unset rerere.enabled
git config --global rerere.enabled


(you should do that now)


















How things will be structured

Every section is going to come with a set of instructions and time for you to do this by yourself then i'll walk through doing it myself. With git, the best way to learn is to do it yourself.


















We will move fast

If you have questions i'll ask throughout and will answer. I have been using git for 10 years so there is blind spots. Let me know