Git 實用技巧
一、基本操作
1. 新建 git 倉庫
| git init -b main
git config --global init.defaultBranch main
git branch -m main
|
2. 克隆遠端倉庫
| git clone http://git.example.com/someone/test.git
git clone http://git.example.com/someone/test.git test
git clone http://git.example.com/someone/test.git --depth=1 -b main
|
3. 提交程式碼
| git add -a
git add -u
git add .
git commit
git commit -m "first commit"
git commit -am "first commit"
|
4. 檢視倉庫狀態
5. 檢視提交歷史
https://git-scm.com/docs/git-log

6. 新建分支
| git branch test
git checkout test
git checkout -b test
|

7. 合併分支
| git checkout main
git merge test
|

8. 刪除分支
| git branch -d test-not-need
|
9. 合併衝突
當兩個分支都對同一行進行了修改,git 便會產生衝突,並標記為未合併
此時將每個檔案進行修改,確認最後的內容,使用 git add 方法標記為衝突已解決
在所有檔案的衝突均已解決後,使用 commit 提交此次修改。

10. 遠端倉庫
預設應該為空
| git remote add origin http://git.example.com/someone/test.git
git push origin main
git fetch --all
git fetch origin
git branch --set-upstream-to=origin/main main
git branch -u origin/main main
|

| git push -u origin main
git pull
git pull origin main
|
二、常見技巧
1. 臨時儲存成果

2. 合併分支靈活選擇 rebase/merge
| git merge test
git rebase test
|

3. cherry-pick
適合 hotfix
| git cherry-pick 12d654f1d701cbf7cd9abb98ce84eeef460a24a7
|

4. 修改上次提交
會同時提交暫存的檔案
5. 取消檔案修改

6. 棄用提交
| 保留檔案
git reset --soft 12d654f1d701cbf7cd9abb98ce84eeef460a24a7
丟棄修改
git reset --hard 12d654f1d701cbf7cd9abb98ce84eeef460a24a7
|
7. 補丁檔案
| git
git diff [file] > a.patch
git apply a.patch
|