Quick Handbook for Git

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: '#252424ff'
    primaryTextColor: '#ffffffff'
    primaryBorderColor: '#5b5a5aff'
    lineColor: '#F8B229'
    secondaryColor: '#2c2b2bff'

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

Common Git Commands

Stage file

1
$git add [files]

Commit file

1
$git commit [files]

Show status

1
$git status

Review the log

1
$git log -p

Git diff in the 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

Show differences between working area and the 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

Show differences in 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)的
| 按电梯的按钮,用纸巾按,出门常备纸巾 |
| 家庭成员不要共用個人生活用品 |
| 就餐时,公筷分餐,快进食,少说话 |
+| 外出进门前注意鞋底在外可能踩踏吐痰等等中介传播物的路面的情况 |

HEAD – is a reference to a specific commit (normally to the the last commit in a local repository).

1
2
3
4
5
6
7
8
9
10
11
12
13
HEAD 说明:
HEAD 表示当前版本
HEAD^ 上一个版本
HEAD^^ 上上一个版本
HEAD^^^ 上上上一个版本
以此类推...

可以使用 ~数字表示
HEAD~0 表示当前版本
HEAD~1 上一个版本
HEAD^2 上上一个版本
HEAD^3 上上上一个版本
以此类推...

Discard unstaged changes

1
2
3
4
5
❯ git checkout -- . # reset all unstaged files in the current directory
❯ git restore . # alternative command for git checkout -- .

❯ git checkout -- test.txt # reset the specific unstaged file
❯ git restore test.txt # alternative command for git checkout -- test.txt

Uncommited files

1
$git reset –hard

Untrack files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
❯ git rm -r -n --cached package-lock.json
rm 'package-lock.json'

❯ git rm -r --cached package-lock.json
rm 'package-lock.json'

❯ git status
On branch hexo
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: package-lock.json

❯ git commit
[hexo 2a02d23] git rm --cached package-lock.json
1 file changed, 9318 deletions(-)
delete mode 100644 package-lock.json

-r 表示递归,-n 表示先不删除,只是列出文件。

Basic Branch Mangement

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

List branches

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

Switch to the specific branch

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

Create a new branch

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

Create a new branch and switch to it

1
2
3
4
5
❯ git checkout -b feature-v2.0.0
Switched to a new branch 'feature-v2.0.0'
❯ git branch
* feature-v2.0.0
master

Merge the branch

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

Delete the branch

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

Rename the current branch

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

1
git branch -M main

.gitignore

1
touch .gitignore

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

Basic Operation of Github

Git clone

Clone from remote repo to local repo

1
2
3
4
5
6
❯ git clone https://github.com/theme-next/hexo-next-share.git
Cloning into 'hexo-next-share'...
remote: Enumerating objects: 50, done.
remote: Total 50 (delta 0), reused 0 (delta 0), pack-reused 50
Receiving objects: 100% (50/50), 10.82 KiB | 15.00 KiB/s, done.
Resolving deltas: 100% (23/23), done.

Git push

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

Push a new branch to remote repo

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

1
2
3
❯ 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

Delete the remote branch

1
2
❯ git branch -D feature-v2.0.0 # force to delete the local branch
❯ git push origin --delete feature-v2.0.0 # delete the remote branch

Git diff.tool && merge.tool

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

Git integration with Vscode

Config VSCode as the Editor

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

Install the Gitlens to view the committed message

PressShift + Command + P, then type Install extentionsenter to find the Gitlens

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

Install the Git Graph to view the commit history

PressShift + Command + P, then type Install extentionsenter to find the Git Graph or Git history as a 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

用 diff 来检查改动
https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/diffs

About Mermaid
https://mermaid-js.github.io/mermaid/#/

git reset 命令
https://www.runoob.com/git/git-reset.html