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 ];
git addwould add the changes from working area to staging areagit commitwould 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 | ❯ git log |
Show differences between working area and the staging area
1 | $git diff |
1 | ❯ git diff |
--- a 和 --- b是进行比较的 文件版本a 和 文件版本b
@@ 是 Chunk,代表这一个变更的集合块
-通常代表那行删除或者进行了更改前的内容
+通常代表那行是增加的或者进行了更改后的内容
如果是全新创建的文件,没有diff
Show differences in staging area and HEAD
1 | $git diff --staged |
1 | ❯ git diff --staged |
HEAD
HEAD – is a reference to a specific commit (normally to the the last commit in a local repository).
1 | HEAD 说明: |
Discard unstaged changes
1 | ❯ git checkout -- . # reset all unstaged files in the current directory |
Uncommited files
1 | $git reset –hard |
Untrack files
1 | ❯ git rm -r -n --cached 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 | ❯ git branch |
Switch to the specific branch
1 | ❯ git checkout master |
Create a new branch
1 | ❯ git branch feature-v2.0.0 |
Create a new branch and switch to it
1 | ❯ git checkout -b feature-v2.0.0 |
Merge the branch
1 | ❯ git checkout master |
Delete the branch
1 | ❯ git 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 | ❯ git clone https://github.com/theme-next/hexo-next-share.git |
Git push
Push the local repo to remote repo
1 | ❯ git push |
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 | ❯ git checkout -b feature-v2.0.0 |
Delete the remote branch
1 | ❯ git branch -D feature-v2.0.0 # force to delete the local branch |
Git diff.tool && merge.tool
1 | ❯ git config --global merge.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 …
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