Local Git Commands
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 commitflowchart 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 devflowchart 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 devgit checkout main
git switch -c devflowchart 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.
- 1
-
The command merges
devbranch into the currentmainbranch. This leaves thedevbranch (pointer) behind. - 2
-
We don’t need to derive the work from both the
mainanddevparents again because thedevgraph is a subgraph of themaingraph. Instead, we can simply move thedevpointer to match the position of themainpointer. 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.
The position of the HEAD pointer is different when merging and rebasing.
git checkout main # <- HEAD
git merge devgit checkout dev # <- HEAD
git rebase mainWhen rebasing, we are willing to rebase with copied commits onto main.
git checkout dev
git rebase main
git checkout main
git merge devflowchart 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 1b7979e16daafabf7c052411b083ea9e2e8a13d5Relative 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 HEADThis 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/foogit 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.