How not to be afraid of Git anymore

Understanding the machinery to whittle away the uncertainty

Neil Kakkar
Nov 13, 2018 · 10 min read
Been here before? (web comic by XKCD)

What is Git anyway?

Why do I need it?

How to Git?

Where to Git?

$ tree .git/
.git/
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
8 directories, 14 files

The Object Store

$ tree .git/
.git/
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── objects
│ ├── a0
│ │ └── 423896973644771497bdc03eb99d5281615b51
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 16 files
$ git hash-object helloworlda0423896973644771497bdc03eb99d5281615b51
$ git cat-file a0423896973644771497bdc03eb99d5281615b51 -p
hello world!

The working directory

$ git status
On branch master
No commits yetChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: helloworldUntracked files:
(use "git add <file>..." to include in what will be committed)
byeworld
$ git status
On branch master
nothing to commit, working tree clean

The Index

$ git commit -m "Add helloworld"
[master (root-commit) a39b9fd] Add helloworld
1 file changed, 1 insertion(+)
create mode 100644 helloworld
$ tree .git/
.git/
├── COMMIT_EDITMSG
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ └── master
├── objects
│ ├── a0
│ │ └── 423896973644771497bdc03eb99d5281615b51
│ ├── a3
│ │ └── 9b9fdd624c35eee08a36077f411e009da68c2f
│ ├── fb
│ │ └── 26ca0289762a454db2ef783c322fedfc566d38
│ ├── info
│ └── pack
└── refs
├── heads
│ └── master
└── tags
14 directories, 22 files
$ git cat-file a39b9fdd624c35eee08a36077f411e009da68c2f -p
tree fb26ca0289762a454db2ef783c322fedfc566d38
author = <=> 1537700068 +0100
committer = <=> 1537700068 +0100
Add helloworld$ git cat-file fb26ca0289762a454db2ef783c322fedfc566d38 -p
100644 blob a0423896973644771497bdc03eb99d5281615b51 helloworld
$ cat .git/refs/heads/master 
a39b9fdd624c35eee08a36077f411e009da68c2f

A branch in Git is a lightweight movable pointer to one of these commits. The default branch name in Git is master.

$ git branch the-ending$ git branch
* master
the-ending
$ cat .git/refs/heads/the-ending 
a39b9fdd624c35eee08a36077f411e009da68c2f
$ git checkout the-ending
Switched to branch 'the-ending'
$ git branch
master
* the-ending
$ tree .git/
.git/
├── COMMIT_EDITMSG
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ ├── master
│ └── the-ending
├── objects
│ ├── 0b
│ │ └── 17be9dbc34c5a5fbb0b94d57680968efd035ca
│ ├── a0
│ │ └── 423896973644771497bdc03eb99d5281615b51
│ ├── a3
│ │ └── 9b9fdd624c35eee08a36077f411e009da68c2f
│ ├── b3
│ │ └── 00387d818adbbd6e7cc14945fdf4c895de6376
│ ├── d1
│ │ └── 8affe001488123b496ceb34d8b13b120ab4cb6
│ ├── fb
│ │ └── 26ca0289762a454db2ef783c322fedfc566d38
│ ├── info
│ └── pack
└── refs
├── heads
│ ├── master
│ └── the-ending
└── tags
17 directories, 27 files
$ git cat-file 0b17be9dbc34c5a5fbb0b94d57680968efd035ca -p
100644 blob d18affe001488123b496ceb34d8b13b120ab4cb6 byeworld
100644 blob a0423896973644771497bdc03eb99d5281615b51 helloworld
$ git cat-file b300387d818adbbd6e7cc14945fdf4c895de6376 -p
tree 0b17be9dbc34c5a5fbb0b94d57680968efd035ca
parent a39b9fdd624c35eee08a36077f411e009da68c2f
author = <=> 1537770989 +0100
committer = <=> 1537770989 +0100
add byeworld$ git cat-file d18affe001488123b496ceb34d8b13b120ab4cb6 -p
Bye world!
$ cat .git/refs/heads/the-ending
b300387d818adbbd6e7cc14945fdf4c895de6376
$ cat .git/refs/heads/master 
a39b9fdd624c35eee08a36077f411e009da68c2f

TL;DR

We’ve moved to freeCodeCamp.org/news

We’ve moved to https://freecodecamp.org/news and publish tons of tutorials each week. See you there.