Git 是開發人員的必備工具,可實現高效的版本控制、協作和專案管理。但掌握 Git 需要了解其強大的命令和工作流程。本備忘單總結了最重要的 Git 指令和技巧,讓您能夠像專業人士一樣管理程式碼庫。
要開始使用 Git 追蹤專案,請初始化一個新儲存庫:
git init
將現有儲存庫複製到本機:
git clone <repository-url>
查看儲存庫的目前狀態:
git status
Working with Changes
下次提交的暫存文件:
git add <file> # Add specific file
git add . # Add all changes
使用描述性訊息儲存您的變更:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git commit -m <span class="hljs-string">"Your commit message here"</span>
### 6. 查看提交歷史記錄
檢查儲存庫中過去的提交:
<
pre class="flex flex-col justify-center border mt-7 dark:border-gray-700"> `git <span class="hljs-built_in">log</span> <span class="hljs-comment"># Detailed log</span>`
git log --oneline \# 緊湊的一行摘要
### 分行管理
分支允許您獨立處理功能或修復。
### 7. 建立一個新分支
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git branch <branch-name>
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git checkout <branch-name>
### 9. 用一個指令建立和切換
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git checkout -b <branch-name>
將一個分支的變更整合到另一個分支:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git merge <branch-name>
### 11. 刪除分支
<
pre class="flex flex-col justify-center border mt-7 dark:border-gray-700"> `git branch -d <branch-name> <span class="hljs-comment"># Delete after merge</span>`
gitbranch -D <branch-name> \# 強制刪除
### 與遠端儲存庫協作
### 12. 新增遠端儲存庫
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git remote add origin <repository-url>
將提交發送到遠端存儲庫:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git push origin <branch-name>
### 14. 拉動變更
從遠端存儲庫檢索更改:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git pull origin <branch-name>
查看遠端中的變更而不合併:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git fetch
### 撤銷更改
### 16. 放棄未暫存的更改
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git checkout -- <file>
從暫存區移除檔案而不刪除它們:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git reset <file>
### 18. 重置提交
撤銷提交並保留更改:
<
pre class="flex flex-col justify-center border mt-7 dark:border-gray-700"> `git reset --soft HEAD~1 <span class="hljs-comment"># Undo last commit but keep changes staged</span>`
git reset --mixed HEAD~1 \# 撤銷上次提交並取消暫存更改
git reset --hard HEAD~1 \# 撤銷上次提交並放棄更改
### 隱藏工作
### 19. 隱藏變化
暫時保存未提交的工作:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git stash
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git stash apply
### 21.查看隱藏列表
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git stash list
比較工作目錄和暫存區之間的變化:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git diff
將分階段變更與上次提交進行比較:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git diff --staged
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git diff <branch1>..<branch2>
### 重寫歷史(謹慎使用!)
### 24. 修改最後一次提交
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git commit --amend -m <span class="hljs-string">"Updated commit message"</span>
互動式變基允許您編輯多個提交:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git rebase -i HEAD~n
### 處理衝突
### 26. 解決合併衝突
當衝突發生時:
1. 編輯衝突的檔案。
2. 將衝突標記為已解決:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git add <file>
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git commit
### Git 專業人員的高階技巧
### 27. 使用別名
加快常用指令的速度:
<
pre class="flex flex-col justify-center border mt-7 dark:border-gray-700"> `git config --global alias.st status`
git config --global alias.co 結帳
git config --global alias.br 分支
### 28. 大型儲存庫的淺克隆
僅克隆最新的提交:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git <span class="hljs-built_in">clone</span> --depth=1 <repository-url>
應用來自另一個分支的特定提交:
<button class="hljs-copy p-1 bg-gray-300 dark:bg-zinc-800"></button> ```bash
git cherry-pick <commit-hash>
### 30.平分查找錯誤
找出引入錯誤的提交:
<
pre class="flex flex-col justify-center border mt-7 dark:border-gray-700"> `git bisect start`
git bisect bad \# 將目前提交標記為壞
git bisect good <commit-hash> \# 標記已知的良好提交
### 使用 Git 的最佳實踐
1. **經常提交:**較小的、集中的提交更容易審查和恢復。
2. **編寫描述性提交訊息:**清楚描述您進行更改的內容和原因。
3. **明智地使用分支:**為功能、修復和實驗建立分支。
4. **先拉後推:**在推前與遙控器同步來避免衝突。
5. **不要提交秘密:**使用`.gitignore`排除 API 金鑰等敏感資訊。
### 結論
掌握 Git 不僅僅是記住命令;還需要學習。它涉及了解工作流程、有效解決衝突以及無縫協作。有了這份備忘單,您就可以像專業人士一樣處理 Git。無論您是管理單獨的專案還是為大型儲存庫做出貢獻,這些命令和技巧都將使您的版本控制體驗更加流暢和強大。
</div>
---
原文出處:https://dev.to/kafeel_ahmad/git-like-a-pro-the-ultimate-cheat-sheet-for-developers-3a87