這篇文章為大家帶來了關於保持清潔的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 filesgit commit -- amend --no-edit 就是靈魂所在了,來看看目前的repo 檔案:
echo "feature 1.3 config info" > config.yaml git add . git commit --amend --no-edit再來看git log
. ├── 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其中n 代表最後幾個提交,上面我們針對feature 1 有三個提交,所以就可以使用:
git rebase -i HEAD~n運行後,會顯示一個vim 編輯器,內容如下:
git rebase -i HEAD~3合併commit-id 最常用的是 squash 和 fixup , 前者包含commit message,後者不包含,這裡使用fixup, 然後 :wq 退出
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>我們再來看一下log, 這就非常清晰了
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善用rebase上面的feature1 已經完整地開發完了,main 分支也有了其他人的更新,再將feature merge 回main 分支之前,以防程式碼有衝突,需要先將main 分支的內容合併到feature 中,如果用merge 指令,就會多處一個merge 節點,log history 中也會出現拐點,並不是線性的,所以這裡我們可以在feature 分支上使用rebase 指令
* 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我們的feature1 功能 on top of main 的提交節點,還是保持線性,接下來就可以push 程式碼,然後提PR,將你的feature merge 到main 分支了簡單描述merge 和rebase 的區別就是這樣的:
我在這裡使用 git pull origin main --rebase 省略了切換main 並拉取最新內容再切回來的過程,一步到位,背後的原理都是上圖展示的這樣
使用rebase 是要遵守一個黃金法則的,這個之前有說過,就不再是贅述了
總結
有了這三個錦囊,相信大家的git log 都無比的清晰,如果你還不知道,完全可以用起來,如果你的組內成員不知道,你完全可以推廣起來,這樣的repo 看起來才更健康
#推薦學習:《Git教程》
以上是3招搞定!保持清潔的Git提交記錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Git和GitHub在軟件開發中的角色和功能是管理代碼和協作開發。 Git通過commit、branch和merge等功能高效管理代碼版本,而GitHub則提供代碼託管和協作工具,如PullRequest和Issues,提升團隊協作效率。

GitHub是開發者發現、分享和貢獻代碼的首選平台。 1)通過搜索功能查找特定代碼庫,如Python項目。 2)創建倉庫並推送代碼分享給全球開發者。 3)通過Fork和PullRequest參與開源項目並貢獻代碼。

Git是一種版本控制系統,GitHub是基於Git的在線平台。使用Git和GitHub進行代碼管理和團隊協作的步驟包括:1.初始化Git倉庫:gitinit。 2.添加文件到暫存區:gitadd.。 3.提交更改:gitcommit-m"Initialcommit"。 4.關聯GitHub倉庫:gitremoteaddoriginhttps://github.com/username/repository.git。 5.推送代碼到GitHub:gitpush-uoriginmaste

GitHub對軟件開發和協作的影響深遠:1.它基於Git的分佈式版本控制系統,提高了代碼安全性和開發靈活性;2.通過PullRequest等功能,提升了團隊協作效率和知識共享;3.GitHubActions等工具幫助優化開發流程,提高代碼質量。

在GitHub上分享、管理和貢獻代碼的方法包括:1.創建倉庫並推送代碼,編寫README和LICENSE文件;2.使用分支、標籤和合併請求管理代碼;3.Fork倉庫、修改並提交PullRequest貢獻代碼。通過這些步驟,開發者可以有效利用GitHub提升開發效率和協作能力。

Git是一個分佈式版本控制系統,GitHub是一個基於Git的協作平台。 Git用於版本控制和代碼管理,GitHub則提供額外的協作功能,如代碼審查和項目管理。

Git是分佈式版本控制系統,GitHub是基於Git的在線平台。 Git用於版本控制、分支管理和合併,GitHub提供代碼託管、協作工具和社交網絡功能。

Git是後端版本控制系統,GitHub是基於Git的前端協作平台。 Git管理代碼版本,GitHub提供用戶界面和協作工具,兩者協同工作提升開發效率。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

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

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器