
圖片來自官網(wǎng)
由來: 公司最近要進(jìn)行招人,所以整理了份 Git 的常用命令,以做參考。同時(shí)讓自己也進(jìn)行學(xué)習(xí)一波。
<center> Git 管理
一、Git 常用操作
-
提交修改到本地分支
1.git add . 2.git commit -m "Add:1.登錄模塊已完成" -
查看修改
1.git diff // 提交至?xí)捍鎺旌蟮男薷挠涗?2.git diff dc519c7 // 當(dāng)前與某次提交暫存庫后的修改記錄 3.git diff dev // 當(dāng)前與 dev 分支比較的修改記錄 -
新建并切換分支
1.git checkout -b debug -
合并代碼
1.git merge xxbranch -
關(guān)聯(lián)遠(yuǎn)程分支
1.git branch --set-upstream-to=origin/xxbranch xxbranch -
拉取 / 推送代碼到遠(yuǎn)端
1.git fetch // 拉取遠(yuǎn)程對(duì)應(yīng)分支代碼 2.git diff // 查看修改 3.git merge // 合并 4.git push origin xxbranch1.git pull // 拉取遠(yuǎn)程對(duì)應(yīng)分支更新并合并 2.git push origin xxbranch -
修改上次 commit 信息
1.git commit --amend -
刪除本地分支
1.git branch -d xxbranch -
刪除遠(yuǎn)程分支
1.git push origin --delete xxbranch -
標(biāo)記 tag
1.git tag v0.0.1 / git tag -a v0.0.1 -m "v0.0.1版本" 2.git push origin v0.0.1 // 將本地 v0.0.1 的 tag 推送到遠(yuǎn)端服務(wù)器 3.git push --tags / git push origin --tags // 推送所有 tag 到遠(yuǎn)端 4.git tag // 查看 tag 列表 5.git show v0.0.1 // 查看 tag 修改信息 -
將當(dāng)前修改存儲(chǔ)至緩存區(qū)
1.git stash // 將當(dāng)前的修改放到緩存區(qū) 2.git stash list // 查看 stash 列表 3.git stash pop // 恢復(fù)至最近一次 stash, 同時(shí)刪除該條記錄 4.git stash apply // 恢復(fù)至最近一次 stash, 記錄還存在 list 中 5.git stash show stash@{1} // 查看指定 stash 修改 6.git stash apply stash@{1} // 恢復(fù)至指定 stash 7.git stash drop stash@{1} // 刪除指定 stash 8.git stash clear // 刪除所有 stash -
版本回退 / 回滾
1.git reset --hard 123456 2.git revert 123456針對(duì)遠(yuǎn)程
1.自己的分支回滾可直接用 reset 2.公共分支回滾要用 revert
二、線上 bug fix 流程
-
假定 master 分支為當(dāng)前線上版本分支, 此時(shí)線上分支出現(xiàn)了一個(gè) bug 需要緊急修復(fù), 而你此時(shí)正在 dev 下的 xxx 分支開發(fā), 該如何操作?
- 1.如果當(dāng)前開發(fā)已告一段落:
1.git add . 2.git commit -m "當(dāng)前功能已完成" 3.git checkout master 4.git checkout -b debug // 在 master 分支上新建 debug 分支用作修改 5.git add . 6.git commit -m "bug 已修改完成" 7.git checkout master 8.git merge debug / git merge --no-ff -m "bug 已修改完成" debug // 合并 debug 分支 9.git add . 10.git commit -m "merge debug branch and fixed bug" 11.git pull 12.git push origin master 13.git branch -d debug // 刪除 debug 分支- 2.如果當(dāng)前分支功能開發(fā)尚未完成:
git stash // 保存現(xiàn)場(chǎng) 順次執(zhí)行上述 1 ~ 13 步 git checkout xxbranch // 切回修復(fù) bug 之前分支 git stash pop // 恢復(fù)現(xiàn)場(chǎng)繼續(xù)未完成開發(fā)
三、自己的遠(yuǎn)程分支版本回退
1.git reflog
2.git reset --hard 123456
3.git push -f origin xxbranch // 本地分支回滾后版本將落后遠(yuǎn)程分支, 必須使用強(qiáng)制推送覆蓋遠(yuǎn)程分支
附錄
一、命令
-
git add .將代碼提交到暫存區(qū)
-
-
git commit -m "balabala"將代碼提交到工作區(qū)(本地庫)
-
-
git diff查看提交至?xí)捍鎺旌蟮男薷挠涗?/li>
-
-
git checkout xxbranch切換到某個(gè)分支
-
-
git checkout -b xxbranch創(chuàng)建并切換到某個(gè)分支
-
-
git merge xxbranch合并某個(gè)分支
-
-
git merge --no-ff -m "balabala"合并某個(gè)分支(臨時(shí)分支用完后一般會(huì)刪除, 則無法通過分支查詢歷史記錄, 所以使用臨時(shí)分支時(shí)需要使用 --no-ff 的方式,同時(shí)寫上 -m 備注信息)
-
-
git fetch拉取遠(yuǎn)程對(duì)應(yīng)分支
-
-
git pull拉取遠(yuǎn)程對(duì)應(yīng)分支更新并合并
-
- 10.
git push origin xxbranch推送當(dāng)前分支到遠(yuǎn)程 - 11.
git push -f origin xxbranch強(qiáng)制推送當(dāng)前分支到遠(yuǎn)程 - 12.
git branch --set-upstream-to=origin/xxbranch xxbranch本地分支與遠(yuǎn)程分支建立關(guān)聯(lián) - 13.
git stash將當(dāng)前的修改放到緩存區(qū) - 14.
git stash list查看 stash 列表 - 15.
git stash pop恢復(fù)至最近一次 stash, 同時(shí)刪除該記錄 - 16.
git stash apply恢復(fù)至最近一次 stash, 記錄還存在 list 中 - 17.
git stash show stash@{1}查看指定 stash 修改 - 18.
git stash apply stash@{1}恢復(fù)至指定 stash - 19.
git stash drop stash@{1}刪除指定 stash - 20.
git stash clear刪除所有 stash - 21.
git branch查看本地分支列表 - 22.
git branch -a查看遠(yuǎn)程分支列表 - 23.
git tag v0.0.1標(biāo)記 tag - 24.
git tag -a v0.0.1 -m "v0.0.1版本"標(biāo)記 tag 并附帶信息 - 25.
git push origin v0.0.1將本地 v0.0.1 的 tag 推送到遠(yuǎn)端服務(wù)器 - 26.
git push --tags / git push origin --tags推送所有 tag 到遠(yuǎn)端 - 27.
git tag查看 tag 列表 - 28.
git show v0.0.1查看 tag 修改信息 - 29.
git reset --hard 123456版本回退 - 30.
git revert 123456版本回滾, 會(huì)在當(dāng)前基礎(chǔ)上新增 commit 記錄 - 31.
git log顯示所有提交過的版本信息 - 32.
git reflog顯示所有提交過的版本信息, 包括已經(jīng)被刪除的 commit 記錄
二、git commit -m "xxx"
- 提交 log: Action + Message
- Action: Add / Mod(ified) / Del(ete) / Rem(ove) / Fix / Ref(actor) / Rea(dability)
-
Add修改 -
Modified修改 -
DeleteRemove刪除 -
Fix修復(fù) bug -
Refactor重構(gòu) -
Readability增加可讀性
-
- Message:對(duì)應(yīng)的描述信息
- Action: Add / Mod(ified) / Del(ete) / Rem(ove) / Fix / Ref(actor) / Rea(dability)
- 示例:git commit -m "Add:發(fā)言模塊完成."