3招搞定!保持清潔的Git提交記錄
這篇文章為大家帶來了關於保持清潔的Git提交記錄的相關知識,其中包括了「git commit –amend」、「git rebase -i」和「rebase」的相關問題,希望對大家有幫助。
推薦學習:《Git教學》
大家都有學習如何規範簡潔的 寫程式碼 ,但卻很少學習如何規範簡潔的 提交程式碼 。現在大家基本上都用Git 作為原始碼管理的工具,Git 提供了極大的彈性,我們按照各種workflow 來提交/合併code,這種彈性把控不好,也會帶來很多問題
最常見的問題就是亂成一團的git log history,那真的是老太太的裹腳布, 又臭又長, 個人極其不喜歡這種log
造成這個問題的根本原因就是隨意提交程式碼。
程式碼都提交了,那還有什麼辦法可以拯救嗎?三個錦囊,就可以完美解決了
善用git commit –amend
這個指令的幫助文件是這樣描述的:
--amend amend previous commit
也就是說,它可以幫我們修改 最後一次提交
既可以修改我們提交的message,又可以修改我們提交的文件,最後還會替換最後一個commit-id
我們可能會在某次提交的時候遺漏了某個文件,當我們再次提交就可能會多處一個無用的commit-id,大家都這樣做,git log 慢慢就會亂得無法追踪完整功能了
假設我們有這樣一段log 訊息
* 98a75af (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 * 119f86e feat: [JIRA123] add feature 1.1 * 5dd0ad3 feat: [JIRA123] add feature 1 * c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
假設我們要修改最後一個log message,就可以使用下面命令:
git commit --amend -m "feat: [JIRA123] add feature 1.2 and 1.3"
我們再來看一下log 資訊, 可以發現,我們用新的commit-id 5e354d1 替換了舊的commit-id 98a75af , 修改了message,並沒有增加節點
* 5e354d1 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3 * 119f86e feat: [JIRA123] add feature 1.1 * 5dd0ad3 feat: [JIRA123] add feature 1 * c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
現在我們的repo 中文件是這樣的:
假設我們提交 feature 1.3 的時候,忘記了一個設定檔 config.yaml , 不想修改log,不想新增新的commit-id,那下面的這個指令就非常好用了. ├── README.md └── feat1.txt 0 directories, 2 files
echo "feature 1.3 config info" > config.yaml git add . git commit --amend --no-edit
. ├── README.md ├── config.yaml └── feat1.txt 0 directories, 3 files
* 247572e (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3 * 119f86e feat: [JIRA123] add feature 1.1 * 5dd0ad3 feat: [JIRA123] add feature 1 * c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
git rebase -i HEAD~n
git rebase -i HEAD~3
1 pick 5dd0ad3 feat: [JIRA123] add feature 1 2 pick 119f86e feat: [JIRA123] add feature 1.1 3 pick 247572e feat: [JIRA123] add feature 1.2 and 1.3 4 5 # Rebase c69f53d..247572e onto c69f53d (3 commands) 6 # 7 # Commands: 8 # p, pick <commit> = use commit 9 # r, reword <commit> = use commit, but edit the commit message 10 # e, edit <commit> = use commit, but stop for amending 11 # s, squash <commit> = use commit, but meld into previous commit 12 # f, fixup <commit> = like "squash", but discard this commit's log message 13 # x, exec <command> = run command (the rest of the line) using shell 14 # d, drop <commit> = remove commit 15 # l, label <label> = label current HEAD with a name 16 # t, reset <label> = reset HEAD to a label 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] 18 # . create a merge commit using the original merge commit's 19 # . message (or the oneline, if no original merge commit was 20 # . specified). Use -c <commit> to reword the commit message. 21 # 22 # These lines can be re-ordered; they are executed from top to bottom. 23 # 24 # If you remove a line here THAT COMMIT WILL BE LOST. 25 # 26 # However, if you remove everything, the rebase will be aborted. 27 # 28 # 29 # Note that empty commits are commented out</commit></oneline></label></commit></commit></label></label></commit></command></commit></commit></commit></commit></commit>
1 pick 5dd0ad3 feat: [JIRA123] add feature 1 2 fixup 119f86e feat: [JIRA123] add feature 1.1 3 fixup 247572e feat: [JIRA123] add feature 1.2 and 1.3
* 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1 * c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
git pull origin main --rebase
我在這裡使用 git pull origin main --rebase 省略了切換main 並拉取最新內容再切回來的過程,一步到位,背後的原理都是上圖展示的這樣
使用rebase 是要遵守一個黃金法則的,這個之前有說過,就不再是贅述了
總結
有了這三個錦囊,相信大家的git log 都無比的清晰,如果你還不知道,完全可以用起來,如果你的組內成員不知道,你完全可以推廣起來,這樣的repo 看起來才更健康
#推薦學習:《Git教程》
以上是3招搞定!保持清潔的Git提交記錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

更新 git 代碼的步驟:檢出代碼:git clone https://github.com/username/repo.git獲取最新更改:git fetch合併更改:git merge origin/master推送更改(可選):git push origin master

要通過 Git 下載項目到本地,請按以下步驟操作:安裝 Git。導航到項目目錄。使用以下命令克隆遠程存儲庫:git clone https://github.com/username/repository-name.git

Git 代碼合併過程:拉取最新更改以避免衝突。切換到要合併的分支。發起合併,指定要合併的分支。解決合併衝突(如有)。暫存和提交合併,提供提交消息。

Git Commit 是一種命令,將文件變更記錄到 Git 存儲庫中,以保存項目當前狀態的快照。使用方法如下:添加變更到暫存區域編寫簡潔且信息豐富的提交消息保存並退出提交消息以完成提交可選:為提交添加簽名使用 git log 查看提交內容

解決 Git 下載速度慢時可採取以下步驟:檢查網絡連接,嘗試切換連接方式。優化 Git 配置:增加 POST 緩衝區大小(git config --global http.postBuffer 524288000)、降低低速限制(git config --global http.lowSpeedLimit 1000)。使用 Git 代理(如 git-proxy 或 git-lfs-proxy)。嘗試使用不同的 Git 客戶端(如 Sourcetree 或 Github Desktop)。檢查防火

如何更新本地 Git 代碼?用 git fetch 從遠程倉庫拉取最新更改。用 git merge origin/<遠程分支名稱> 將遠程變更合併到本地分支。解決因合併產生的衝突。用 git commit -m "Merge branch <遠程分支名稱>" 提交合併更改,應用更新。

在開發一個電商網站時,我遇到了一個棘手的問題:如何在大量商品數據中實現高效的搜索功能?傳統的數據庫搜索效率低下,用戶體驗不佳。經過一番研究,我發現了Typesense這個搜索引擎,並通過其官方PHP客戶端typesense/typesense-php解決了這個問題,大大提升了搜索性能。

要刪除 Git 倉庫,請執行以下步驟:確認要刪除的倉庫。本地刪除倉庫:使用 rm -rf 命令刪除其文件夾。遠程刪除倉庫:導航到倉庫設置,找到“刪除倉庫”選項,確認操作。
