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