41個(gè)Git 命令備忘清單

英文 | https://tapajyoti-bose.medium.com/git-cheat-sheet-with-40-commands-concepts-ab1b9d973e96
翻譯 | 楊小愛
git init <directory><directory> 是可選的,如果不指定,將使用當(dāng)前目錄。
2.克隆一個(gè)遠(yuǎn)程倉庫
git clone <url>3.添加文件到暫存區(qū)
git add <file>要添加當(dāng)前目錄中的所有文件,請(qǐng)使用 . 代替 <file>,代碼如下:
git add .4. 提交更改
git commit -m "<message>"如果要添加對(duì)跟蹤文件所做的所有更改并提交。
git commit -a -m "<message>"# orgit commit -am "<message>"
5.從暫存區(qū)刪除一個(gè)文件
git reset <file>6.移動(dòng)或重命名文件
git mv <current path> <new path>7. 從存儲(chǔ)庫中刪除文件
git rm <file>您也可以僅使用 --cached 標(biāo)志將其從暫存區(qū)中刪除
git rm --cached <file>基本 Git 概念
8.默認(rèn)分支名稱:main
9.默認(rèn)遠(yuǎn)程名稱:origin
10.當(dāng)前分支參考:HEAD
11. HEAD 的父級(jí):HEAD^ 或 HEAD~1
12. HEAD 的祖父母:HEAD^^ 或 HEAD~2
13. 顯示分支
git branch有用的標(biāo)志:
-a:顯示所有分支(本地和遠(yuǎn)程)
-r:顯示遠(yuǎn)程分支
-v:顯示最后一次提交的分支
14.創(chuàng)建一個(gè)分支
git branch <branch>你可以創(chuàng)建一個(gè)分支并使用 checkout 命令切換到它。
git checkout -b <branch>15.切換到一個(gè)分支
git checkout <branch>16.刪除一個(gè)分支
git branch -d <branch>您還可以使用 -D 標(biāo)志強(qiáng)制刪除分支。
git branch -D <branch>17.合并分支
git merge <branch to merge into HEAD>有用的標(biāo)志:
--no-ff:即使合并解析為快進(jìn),也創(chuàng)建合并提交
--squash:將指定分支中的所有提交壓縮為單個(gè)提交
快進(jìn)合并

非快進(jìn)合并

建議不要使用 --squash 標(biāo)志,因?yàn)樗鼤?huì)將所有提交壓縮為單個(gè)提交,從而導(dǎo)致提交歷史混亂。
18. 變基分支
變基是將一系列提交移動(dòng)或組合到新的基本提交的過程。
git rebase <branch to rebase from>19. 查看之前的提交
git checkout <commit id>20. 恢復(fù)提交
git revert <commit id>21. 重置提交
git reset <commit id>您還可以添加 --hard 標(biāo)志來刪除所有更改,但請(qǐng)謹(jǐn)慎使用。
git reset --hard <commit id>22.查看存儲(chǔ)庫的狀態(tài)
git status23.顯示提交歷史
git log24.顯示對(duì)未暫存文件的更改
git diff您還可以使用 --staged 標(biāo)志來顯示對(duì)暫存文件的更改。
git diff --staged25.顯示兩次提交之間的變化
git diff <commit id 01> <commit id 02>26. 存儲(chǔ)更改
stash 允許您在不提交更改的情況下臨時(shí)存儲(chǔ)更改。
git stash您還可以將消息添加到存儲(chǔ)中。
git stash save "<message>"27. 列出存儲(chǔ)
git stash list28.申請(qǐng)一個(gè)藏匿處
應(yīng)用存儲(chǔ)不會(huì)將其從存儲(chǔ)列表中刪除。
git stash apply <stash id>如果不指定 <stash id>,將應(yīng)用最新的 stash(適用于所有類似的 stash 命令)
您還可以使用格式 stash@{<index>} 應(yīng)用存儲(chǔ)(適用于所有類似的存儲(chǔ)命令)
git stash apply stash@{0}29.刪除一個(gè)藏匿處
git stash drop <stash id>30.刪除所有藏匿處
git stash clear31. 應(yīng)用和刪除存儲(chǔ)
git stash pop <stash id>32.顯示存儲(chǔ)中的更改
git stash show <stash id>33.添加遠(yuǎn)程倉庫
git remote add <remote name> <url>34. 顯示遠(yuǎn)程倉庫
git remote添加 -v 標(biāo)志以顯示遠(yuǎn)程存儲(chǔ)庫的 URL。
git remote -v35.刪除遠(yuǎn)程倉庫
git remote remove <remote name>36.重命名遠(yuǎn)程存儲(chǔ)庫
git remote rename <old name> <new name>37. 從遠(yuǎn)程存儲(chǔ)庫中獲取更改
git fetch <remote name>38. 從特定分支獲取更改
git fetch <remote name> <branch>39. 從遠(yuǎn)程存儲(chǔ)庫中拉取更改
git pull <remote name> <branch>40.將更改推送到遠(yuǎn)程存儲(chǔ)庫
git push <remote name>41.將更改推送到特定分支
git push <remote name> <branch>寫在最后的想法
