Git基本功能教學
主要內容節錄至 YouTube頻道-李祐棠:
Git中文教學
1. 安裝與設定
- Linux : gitk(built-in)
- MacOS : gitx
Start a Project
- Simple
git init
git add <file>
git commit
- Setup user name and email
git config user.name
git config user.email
- Setup editer
git config core.editor vim
- Setup merge tool
git config merge.tool vimdiff
- Setup alias (快捷名稱設定)
- Setup color
補充:git config --global
可以一次性更改所有 git 設定。
2. Add and Commit
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
而 git commit
是把 staged 的東西移到 commit 資料庫內。
git commit -a
將 git commit
+ git add
兩個動作一次完成。
- See your repository status by
git status
- git add
git add -p
分別檢視與是否加入每一個改變,這就是 patch add 。
- git commit
- 50/72 rule
- As detailed as possible
常用指令:
-
查看狀態
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
加入想要 Commit 的修改項目
-
log 查看版本紀錄
-
ignore (不要出現在 status 的方法)
- 產生一個
gitignore
,裡面
在欲加入 gitignore
的檔案,加上 /
後為現在這個資料夾下的那個檔案
- 將新建的
gitignore
檔案加入 Commit 中
如果要取消加入
該 ignore 什麼的參考資料
Git:–local-branching-on-the-cheap
重要守則
- 不要留下個人機密資料(因為幾乎刪不掉)
- 不要放上非必要的大檔案
- Generated files(產生的檔案,例如執行檔)
50/72法則
在 Commit 中,遵從標題,內文72字的規則,標題清楚標,細節在內文。會有字數限制是因為,在終端機用 $ git log
閱讀 Commit 時,這樣可以一行看完,而不會有些 Commit 標題一行,有些兩行。
>標題在超過字數(50字)後,文字會變成白色提醒使用者。
撰寫 Commit 建議須包含以下:
- 議題編號:
Relate Issue: Issue #159
- 問題敘述:
The Problem:
- 原因解釋:
The Cause:
- 解決方式:
The fix:
建議範本
參考資料:fix wrong arrow angle on branching #171
偉大 Git commit message 的七條規則
- 用一行空白行分隔標題與內容
- 限制標題最多只有 50 字元
- 標題開頭要大寫
- 標題不以句點結尾
- 以祈使句撰寫標題
- 內文每行最多 72 字
- 用內文解釋 what 以及 why vs. how
3. Indicate a Commit
Hash Value: 指定是哪個Commit。
-
^
看目前 Commit 的舊一個。
-
--oneline
只顯示一行。
$ git log --oneline load.cpp
: 只顯示與 load.cpp
有關的 Commit。
-
Git log <path>|<commit>
- -n : limit number
- __oneline : view hash and commit summary
- –stat : view files change
- –patch : view lines change
- -S or –grep : find modification
diff
:顯示改了什麼( Commit 與未加入 Commit 的不同)。
參考資料:
Git 基礎 - 檢視提交的歷史記錄
4. Patch Add and Amend
- Git add -p
- My experience: always use this
- this hunk from worktree [y,n,q,a,d,/,j,J,g,s,e,?]?
- s: seperate all change parts
- e: edit
Modify Commit
- Use git commit – amend
a. Check before doing this
- Use reset HEAD^ then re-commit

5. Branch and Merge
Move and Create Branch
- Checkout: move HEAD
git checkout <commit>
Move to HEAD
git checkout <path>
WARNING: discard change
- Branch:
git branch
List branch
git branch <name>
Create branch
- 綜合開新的Branch和移動HEAD到新增的Branch
View Branch
git branch
git log ru8 加下面
- –decorate (看 reference 用的,若無效輸入下面那行)
- –graph
Delete Branch
git branch -d <name>
git branch -D <name>
for no-merge commit
Merge
git merge <branch>
- Usually mainline merge other
git checkout master
git merge <branch>
Resolve Conflict
- Manually resole
- Check evert codes between
<<<<<<
, >>>>>>
- Edit code to what it should be
- Use
git mergetool
(圖形化介面,展示哪裡不同) like vimdiff
- It shows: local, base, remote, file to be edited
- Edit "file to be edited" to what is should be
- Add and commit

上圖的意思:
<<<<<<
未修改的版本
======
新改的地方
>>>>>>
去對照後,手動改成最新版的,再 commit 。
Reference
6. Rebase
Rebase: Why and What
- Move your branch to another place
- Clean up your DAG (Directed Acyclic Graph) / Edit commits
- Main branch has updated, rebase feature branch to new master

What Rebase Do
- Checkout the new-base
- Re-apply commits

Rebase: How
- Rebase <new_base>
- Rebase current branch to <new_base>
- Start from
git merge-base HEAD <new-base>
rebase --onto <new base><ref_commit><branch>
- Rebase from common ancestor of
<ref_commit>
and <branch>
Example:
git rebase --onto master A dev
- Notice the Hash Value

Rebase: Conflict
- Rebase stop if conflict
- A little different to merge case: just add the final result
git rebase --continue
until end
git rebase --abort
if out of control
Rebase -i
- Change content (edit)
- Change message (reword): 修改 Commit message
- Change order: 直接在介面裡面換順序就好
- Remove commit (drop)
- Combine commit (squash, fixup)
- Split (edit->reset)
- Run command (exec)
Some Tips
- Rebase can do fast-forward if new base is child of current branch
Thing to Notice
- Rebase is re-commit, it changes histoy
- Do not rebase on published commits
- Prevent rebase across branch base
- Suggest making a backup before rebase
Reference
7. Remote and Github
Clone and Remote
git clone
- Default remote name is origin
- Remote:
git remote show <origin>
: list detail
git remote add <name>
URL: add remote
git remote rename <a><b>
: rename
Remote is Reference
- Remote branch is named by:
<remote>/<branch>
- Show hidden branch by
branch -a
Update Status
- fetch remote
- Rebase local branch to remote state
Or
- Just
pull
(=fetch + rebase)
Push
git push <remote><branch>
: <branch>
git push <remote>
: <branch>
delete remote branch
basic Workflow
- Fork on Github, clone your repository as
<origin>
- Add
<upstream>
- Develop on mew branch
- Push branch to
<origin>
, open Pull Request
- After merge, fetch
<upstream>
and rebase master
Reference
8. Stash
Why Stash
- Temporarily store your work not enough for commit
Usage
git stash
: store stash content
git stash list
: show stored content
- Use
stash@{n}
to indicate stash
git stash pop
: pop top
git stash drop
: drop stash WARNING
Reference
9. Patch and Cherry-Pick
Generate Patch
git format-patch <base_commit>
git format-patch -n3 <top_commit>
Or
git diff > name.patch
Apply Patch (貼上去)
git apply <patch>
: apply patch
git apply --check
: check no conflict
git am
: apply and commit
git am *.patch
is very useful
Patch Conflict
git apply --reject <patch>
- Patched failed stored in file.rej
- Manual resolve the conflict, then
git am --continue
cherry-pick
git cherry-pick <commit>
- Resolve conflict like rebase, just ass then
git cherry-pick -- continue
Reference
10. Bisect and Blame
Serch for Modification
git blame -L <start>,<end> <file>
git blame -L
: <function> <file>
Bisect
Find where program failed
git bisect start
git bisect good <commit>
git bisect bad <commit>
… Do binary search, mark good, bad…
Or
- Just
git bisect run <shell>
Refernece
11. Ending
Not Mention
- Open your own derver
- Manage branch on local or server
- Use git with SVN server
- Submodule
- Tag
- Save deleted data
- Modify history