Local Git Commands

Git
Author

Shane Oh

Published

April 14, 2023

Thanks Cottle for creating this educational tool. Ever thought of Git graphs as linked lists? Here is a great explanation.

First things first,

git commit

This creates a new commit C1, which references where it was based off of — in this case, a C0 which is a initial commit becomes the parent.

git commit

flowchart TB
  main:::pointer -.-> C0:::commit
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  main:::pointer -.-> C1
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git branch

Branches is Git are simply pointers to a specific commit – nothing more. This is why many Git enthusiasts chant the mantra

branch early, and branch often.

When we start mixing branches (pointers) and commits, we will see how these two features combine. For now, just remember that a branch essentially says

I want to include the work of this commit and all parent commits.

git checkout main
git branch dev

flowchart TB
  C1:::commit --> C0:::commit
  main:::pointer -.-> C1
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  main:::pointer -.-> C1
  dev:::pointer -.-> C1
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git checkout main
git checkout -b dev
git checkout main
git switch -c dev

flowchart TB
  C1:::commit --> C0:::commit
  main:::pointer -.-> C1
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  main:::pointer -.-> C1
  dev:::pointer -.-> C1
  HEAD:::pointer -.-> dev
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git merge

merge command eventually creates a special commit which has two unique parents. A commit with two parents essentially means

I want to include all the work from both parents, and the set of all their parents.

git checkout main
1git merge dev
git checkout dev
2git merge main
1
The command merges dev branch into the current main branch. This leaves the dev branch (pointer) behind.
2
We don’t need to derive the work from both the main and dev parents again because the dev graph is a subgraph of the main graph. Instead, we can simply move the dev pointer to match the position of the main pointer. This is also called fast forward.

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C3
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C2:::commit
  C4:::commit --> C3:::commit

  main:::pointer -.-> C4
  dev:::pointer -.-> C3
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C2:::commit
  C4:::commit --> C3:::commit

  main:::pointer -.-> C4
  dev:::pointer -.-> C3
  HEAD:::pointer -.-> dev
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C2:::commit
  C4:::commit --> C3:::commit

  main:::pointer -.-> C4
  dev:::pointer -.-> C4
  HEAD:::pointer -.-> dev
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git rebase

rebase copies the commits and stack them on somewhere else.

Note

The position of the HEAD pointer is different when merging and rebasing.

git checkout main # <- HEAD
git merge dev
git checkout dev # <- HEAD
git rebase main

When rebasing, we are willing to rebase with copied commits onto main.

git checkout dev
git rebase main
git checkout main
git merge dev

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C3:::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C4
  HEAD:::pointer -.-> dev
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3':::commit --> C2:::commit
  C4':::commit --> C3':::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C4'
  HEAD:::pointer -.-> dev
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3':::commit --> C2:::commit
  C4':::commit --> C3':::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C4'
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3':::commit --> C2:::commit
  C4':::commit --> C3':::commit
  main:::pointer -.-> C4'
  dev:::pointer -.-> C4'
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git checkout

Didn’t we use the git checkout command without any discomfort so far? What checkout does is point HEAD to the desired object such as a branch or a commit.

git checkout 1b7979e16daafabf7c052411b083ea9e2e8a13d5

Relative reference (^ and ~)

git checkout C1
git checkout C2^
git checkout main^^
git checkout main~2
git checkout HEAD^; git checkout HEAD^

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C2:::commit
  main:::pointer -.-> C3
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C2:::commit
  main:::pointer -.-> C3
  HEAD:::pointer -.-> C1
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

Branch forcing (git branch -f)

This is called branch forcing.

git branch -f feature dev^

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C3:::commit
  C5:::commit --> C4:::commit
  C6:::commit --> C5:::commit
  C7:::commit --> C4:::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C6
  feature:::pointer -.-> C7
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

flowchart TB
  C1:::commit --> C0:::commit
  C2:::commit --> C1:::commit
  C3:::commit --> C1:::commit
  C4:::commit --> C3:::commit
  C5:::commit --> C4:::commit
  C6:::commit --> C5:::commit
  main:::pointer -.-> C2
  dev:::pointer -.-> C6
  feature:::pointer -.-> C5
  HEAD:::pointer -.-> main
  classDef commit fill: #abc, color: #000
  classDef pointer fill: #fff, color #000, font: #000

git reset

The reset command is often used to undo changes that have been staged or committed. This sets the HEAD to the desired commit object. The most common command will be as follows.

Undoing the add command.

git reset
git reset HEAD

This command moves HEAD to the parent of the current commit (HEAD^), effectively undoing the most recent commit.

git reset HEAD^

Undo a commit and make a topic branch out of it.

git branch topic/foo
git reset --hard HEAD~3
git checkout topic/foo

git revert

revert is a command that creates a new commit that undoes the changes made by a previous commit. This means that instead of deleting or altering past commits, git revert adds a new commit on top of the branch.

When HEAD is on the merge commit,

git revert HEAD -m 1
git revert HEAD -m 2 

The number (index) of a parent can be checked with the command git cat-file -p HEAD.