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 addwould add the changes from working area to staging areagit commitwould add the changes from staging area to local repo
HEAD
HEAD is a pointer to the current commit you’re working on, typically the most recent commit in your local repository.
1 |
|
File Operations
Staging Files
Staging files means adding local changes from the working directory to the staging area.
1 |
|
Committing Files
Committing files means adding the changes from the staging area to the local repository.
1 | ❯ git commit [files] |
Showing Status
1 | ❯ git status |
Discarding Unstaged Changes
Discard unstaged changes in the working directory.
1 | # reset all unstaged files in the current working directory |
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 | # dry run, just list the files that would be removed |
Removing Files from Working Directory and Staging Area
Remove the file from the staging area and working directory
1 | ❯ git 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 | ❯ git branch |
Switching to a Specific Branch
Switch to a specific branch
1 | ❯ git checkout 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 | ❯ git branch feature-v2.0.0 |
Creating and Switching to a New Branch
1 | ❯ git checkout -b feature-v2.0.0 |
Merging Branches
1 | ❯ git checkout master |
Deleting Branches
1 | ❯ git 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 | # rename current branch from master to main |
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 | # git stash can only stash changes in the staging area |
.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 | # setup upstream |
Cloning from Remote Repository
Clone from remote repo to local repo
1 | ❯ git clone https://github.com/qzi/hexo-auto-updated.git |
Pushing to Remote Repository
Push the local repo to remote repo
1 | ❯ git push |
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 | ❯ git checkout -b feature-v2.0.0 |
Deleting a Remote Branch
1 | # force to delete the local branch |
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 | # tag a version |
Viewing Changes and History
Reviewing the Log
1 | ❯ git log -p |
Viewing Diff in a Specific Commit
1 | ❯ git log |
Showing Differences Between Working Area and Staging Area
1 | ❯ git diff |
1 | ❯ git diff |
--- a 和 --- b是进行比较的 文件版本a 和 文件版本b
@@ 是 Chunk,代表这一个变更的集合块
-通常代表那行删除或者进行了更改前的内容
+通常代表那行是增加的或者进行了更改后的内容
如果是全新创建的文件,没有diff
Showing Differences Between Staging Area and HEAD
1 | ❯ git diff --staged |
1 | ❯ git diff --staged |
Git Configuration and Tools Integration
Configuring Diff and Merge Tools
Config vimdiff as the merge tool and diff tool
1 | ❯ git config --global merge.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 …
Reference
- 用 diff 来检查改动: Git Tower 出品的进阶教程,详细图解了 diff 的用法。
- About Mermaid: Mermaid 官方文档,文中流程图语法的详细参考。
- Git reset 命令: 菜鸟教程关于 reset 的中文详解,适合查阅参数。