Quick Handbook for Git

This quick Git handbook gives you a concise overview of the commands and workflows you’ll use most often. It focuses on practical tips, clear explanations, and the essential concepts you need to work efficiently with Git in real projects.

Core Concepts

When working with a beast like Git, always remember the golden rule: Know your place!

Git Process Flow

The key to mastering Git is understanding where we are in its three main phases:

---
config:
  theme: 'base'
  themeVariables:
    primaryColor: 'transparent'
    primaryTextColor: '#c9d1d9'
    primaryBorderColor: '#767677ff'
    lineColor: '#6a9fb5'
    secondaryColor: 'transparent'
    tertiaryColor: 'transparent'
    mainBkg: 'transparent'
    nodeBorder: 'transparent'
    edgeLabelBackground: 'transparent'
    edgeLabelColor: '#c9d1d9' 
    labelBackground: 'transparent'
    labelBoxBkgColor: 'transparent'
    labelBoxBorderColor: 'transparent'
    clusterBkg: 'transparent'
    fontSize: '14px'
title: Git Flow

---
graph TD;
A[Working Area - UNSTAGED] -->|git add| B[Staging Area - STAGED];
B -->|git commit| C[Local Repo - COMMITTED];
  • git add would add the changes from working area to staging area
  • git commit would add the changes from staging area to local repo

HEAD is a pointer to the current commit you’re working on, typically the most recent commit in your local repository.

1
2
3
4
5
6
7
8
9
10
11
12
13

HEAD represents the current version
HEAD^ the previous version
HEAD^^ two versions back
HEAD^^^ three versions back
and so on...

You can also use ~ with numbers:
HEAD~0 represents the current version
HEAD~1 one version back
HEAD~2 two versions back
HEAD~3 three versions back
and so on...

File Operations

Staging Files

Staging files means adding local changes from the working directory to the staging area.

1
2
3
4
5
6

# add all files in the current directory
❯ git add .
# add the specific file
❯ git add test.txt

Committing Files

Committing files means adding the changes from the staging area to the local repository.

1
2
3
❯ git commit [files]
# example
❯ git commit -m " committed message "

Showing Status

1
❯ git status

Discarding Unstaged Changes

Discard unstaged changes in the working directory.

1
2
3
4
5
6
7
8
# reset all unstaged files in the current working directory
❯ git checkout -- .
# alternative command for git checkout -- .
❯ git restore .
# reset the specific unstaged file
❯ git checkout -- test.txt
# alternative command for git checkout -- test.txt
❯ git restore test.txt

Discarding All Local Changes (Staged & Unstaged)

It’s truely a dangerous operation, use with caution. It would discard all local changes, both staged and unstaged.

1
❯ git reset --hard

Untracking Files

Untracking files means removing files from the staging area & local repo, but not removing them from the working directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# dry run, just list the files that would be removed
❯ git rm -n --cached package-lock.json
rm 'package-lock.json'
ls package-lock.json
package-lock.json

# remove the file from the staging area && local repo, but not remove it from the working directory
❯ git rm --cached package-lock.json
rm 'package-lock.json'

# Next commit would remove the file from the local repo
❯ git status
On branch hexo
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: package-lock.json

# if you push the commit to remote repo, the file would be removed from the remote repo
❯ git commit
[hexo 2a02d23] git rm --cached package-lock.json
1 file changed, 9318 deletions(-)
delete mode 100644 package-lock.json


Removing Files from Working Directory and Staging Area

Remove the file from the staging area and working directory

1
2
❯ git rm package-lock.json 
rm 'package-lock.json'

Basic Branch Management

Git graph based on mermaid

%%{init: { 'logLevel': 'debug', 'theme': 'neutral' } }%%
gitGraph
  commit
  commit

  branch develop
  checkout develop
  commit
  commit

  branch feature-v1.0.0
  commit
  commit
  checkout feature-v1.0.0
  commit
  commit
  checkout develop
  merge feature-v1.0.0
  checkout main
  merge develop
  commit

Listing Branches

1
2
3
4
❯ git branch
feature-v1.1.0
* master

Switching to a Specific Branch

Switch to a specific branch

1
2
❯ git checkout master
Switched to branch 'master'

Notice:
Unstaged changes do not belong to any specific branch. When switching branches, changes in the working area are preserved and carried over to the new branch, rather than being lost. However, if there are conflicts with the destination branch, Git will abort the switch to prevent overwriting your work.

Creating a New Branch

1
2
3
4
5
❯ git branch feature-v2.0.0
❯ git branch
feature-v2.0.0
* master

Creating and Switching to a New Branch

1
2
3
4
5
6
7
8
9
❯ git checkout -b feature-v2.0.0
Switched to a new branch 'feature-v2.0.0'
# be equal to
❯ git branch feature-v2.0.0
❯ git checkout feature-v2.0.0
# skip two steps above
❯ git branch
* feature-v2.0.0
master

Merging Branches

1
2
3
4
5
6
7
8
9
10
11
12
❯ git checkout master
Switched to branch 'master'
❯ git branch # list all existing branches
feature-v2.0.0
* master
❯ git merge feature-v2.0.0 # merge feature-v2.0.0 into master
Updating 9d87f34..7e66e0c
Fast-forward
.gitignore | 1 +
test_branch | 3 +++
2 files changed, 4 insertions(+)
create mode 100644 .gitignore

Deleting Branches

1
2
3
4
5
6
7
8
9
10
11
12
13
❯ git branch
* master
❯ git branch feature-v3.0.0 # create a new branch, feature-v3.0.0 and don't switch to it.
❯ git branch
feature-v3.0.0
* master
❯ git branch -d feature-v3.0.0
Deleted branch feature-v3.0.0 (was fc84191).
❯ git branch
* master
❯ git branch -D feature-v3.0.0 # force delete, if not merged
❯ git push origin --delete feature-v3.0.0 # delete remote branch

Renaming the Current Branch

Rename the current branch from master to main, it would force to cover to existing main branch if any.

1
2
3
4
5
6
# rename current branch from master to main
❯ git branch -M main
# push the new branch to remote
❯ git push origin -u main
# delete the old branch from remote
❯ git push origin --delete master

Stashing Changes Temporarily

When you’re in the middle of developing a feature and need to temporarily set aside your current work to focus on a different task, that’s when git stash can help you. Stash the changes in the staging area, then you can switch to another branch and work on it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# git stash can only stash changes in the staging area
❯ git stash
No local changes to save
❯ git status
On branch hexo
Your branch is up to date with 'origin/hexo'.

Untracked files:
(use "git add <file>..." to include in what will be committed)
scripts/
source/_drafts/hexo-plugin.md

nothing added to commit but untracked files present (use "git add" to track)
❯ git add .
# after we've staged the changes in the staging area, git stash works properly
❯ git stash
Saved working directory and index state WIP on hexo: 879878 change since 2000
# the staging area will be empty after we temporarily stash the changes
❯ git status
On branch hexo
Your branch is up to date with 'origin/hexo'.

nothing to commit, working tree clean

.gitignore

1
touch .gitignore

Add .idea/ into .gitignore, then it can filter the files and folders under .idea/

Basic GitHub Operations

---
config:
  theme: 'base'
  themeVariables:
    primaryColor: 'transparent'
    primaryTextColor: '#c9d1d9'
    primaryBorderColor: '#767677ff'
    lineColor: '#6a9fb5'
    secondaryColor: 'transparent'
    tertiaryColor: 'transparent'
    mainBkg: 'transparent'
    nodeBorder: 'transparent'
    edgeLabelBackground: 'transparent'
    edgeLabelColor: '#c9d1d9' 
    labelBackground: 'transparent'
    labelBoxBkgColor: 'transparent'
    labelBoxBorderColor: 'transparent'
    clusterBkg: 'transparent'
    fontSize: '14px'
title: Git to Github Flow

---
graph TD;
A[Working Area - UNSTAGED] -->|git add| B[Staging Area - STAGED];
B -->|git commit| C[Local Repo - COMMITTED];
C -->|git push| D[Remote Repo - GITHUB];

Setting up upstream

1
2
3
4
5
6
7
8
9
# setup upstream
❯ git remote add origin https://github.com/qzi/hexo-auto-updated.git
# list remote repo
❯ git remote -v
origin https://github.com/qzi/hexo-auto-updated.git (fetch)
origin https://github.com/qzi/hexo-auto-updated.git (push)
❯ git push -u origin main # -u means upstream
❯ git push # then you can just use git push to push to remote repo

Cloning from Remote Repository

Clone from remote repo to local repo

1
2
❯ git clone https://github.com/qzi/hexo-auto-updated.git

Pushing to Remote Repository

Push the local repo to remote repo

1
2
3
4
5
6
7
8
9
10
❯ git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 27.55 KiB | 3.94 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/abc/abc.github.io.git
c936aa8..fe09403 hexo -> hexo

Pulling from Remote Repository

Pull the remote repo to local repo

1
❯ git pull

Mostly it’s equal to git fetch + git merge if no conflict.

Pushing a New Branch to Remote Repository

Create and switch to a new branch feature-v2.0.0, then push it to remote repo

1
2
3
4
5
6
❯ git checkout -b feature-v2.0.0
Switched to a new branch 'feature-v2.0.0'
❯ git push --set-upstream origin feature-v2.0.0
# the same as
❯ git push -u origin feature-v2.0.0

Deleting a Remote Branch

1
2
3
4
5
# force to delete the local branch
❯ git branch -D feature-v2.0.0

# delete the remote branch
❯ git push origin --delete feature-v2.0.0

Tagging and Releasing Versions

If you only push committed code to the remote repository, the tags will not be pushed automatically. Therefore, you need to push tags to the remote repository separately.

1
2
3
4
5
6
7
8
9
10
11
12
# tag a version
❯ git tag -a v1.0.0 -m "version 1.0.0"

# push the tag to remote repo
❯ git push origin v1.0.0

# or all push all tags
❯ git push origin --tags

# delete the tag
❯ git tag -d v1.0.0
❯ git push origin --delete v1.0.0 # delete the tag from remote repo

Viewing Changes and History

Reviewing the Log

1
❯ git log -p

Viewing Diff in a Specific Commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
❯ git log
commit 222a62a03a46a676019ef58fdacbaf6ab5677c20
Author: Leon Qiu <i@leonvision.online>
Date: Wed Jul 13 21:04:48 2022 +0800

test diff

❯ git diff 222a62a03a46a676019ef58fdacbaf6ab5677c20
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9f11b75
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.idea/
diff --git a/testGit b/testGit
index c67ed73..6c4b8c7 100644
--- a/testGit
+++ b/testGit
@@ -1 +1,5 @@
test git diff
+test git diff 3
+test git diff 2
+
+test branch

Showing Differences Between Working Area and Staging Area

1
❯ git diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
❯ git diff
diff --git a/source/_posts/finance/the-threshold-of-sp500.md b/source/_posts/finance/the-threshold-of-sp500.md
index dd45124..822c9cd 100644
--- a/source/_posts/finance/the-threshold-of-sp500.md
+++ b/source/_posts/finance/the-threshold-of-sp500.md
@@ -11,7 +11,7 @@ excerpt: ' 从 Tesla 的暴涨看 S&P 500 指数的长盛不衰,重点聊指

![image-20200211015932474](the-threshold-of-sp500/image-20200211015932474.png)

-
+test

S&P 500 为啥长生不衰

--- a--- b是进行比较的 文件版本a 和 文件版本b

@@ 是 Chunk,代表这一个变更的集合块

-通常代表那行删除或者进行了更改前的内容

+通常代表那行是增加的或者进行了更改后的内容

如果是全新创建的文件,没有diff

Showing Differences Between Staging Area and HEAD

1
❯ git diff --staged
1
2
3
4
5
6
7
8
9
10
❯ git diff --staged
diff --git a/source/_posts/miscellaneous/2019-ncov-abc.md b/source/_posts/miscellaneous/2019-ncov-abc.md
index 6302830..61c0c54 100644
--- a/source/_posts/miscellaneous/2019-ncov-abc.md
+++ b/source/_posts/miscellaneous/2019-ncov-abc.md
@@ -31,6 +31,7 @@ excerpt: ' 本文尝试普及 2019新型冠状病毒(2019-nCoV/SARS-COV2)的
| 按电梯的按钮,用纸巾按,出门常备纸巾 |
| 家庭成员不要共用個人生活用品 |
| 就餐时,公筷分餐,快进食,少说话 |
+| 外出进门前注意鞋底在外可能踩踏吐痰等等中介传播物的路面的情况 |

Git Configuration and Tools Integration

Configuring Diff and Merge Tools

Config vimdiff as the merge tool and diff tool

1
2
❯ git config --global merge.tool vimdiff
❯ git config --global diff.tool vimdiff

VS Code Integration

Setting VSCode as the Default Editor

1
❯ git config --global core.editor "code --wait"

Installing GitLens Extension

Press Shift + Command + P, then type Install extentions enter to find the GitLens

GitLens: https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens

Installing Git Graph Extension

Press Shift + Command + P, then type Install extentions enter to find the Git Graph or Git History as an alternative

Git Graph: https://marketplace.visualstudio.com/items?itemName=mhutchie.git-graph
Git History: https://marketplace.visualstudio.com/items?itemName=donjayamanne.githistory

其实我只是想实验 Mermaid 的 flowchart 才写的这篇 つ﹏⊂

To Be Continue …

My Github Sponsors Profile

Reference