# git ## command note ### Commit 管理 `git revert xxxx` 用新的 commit 來退版 `git checkout -- <file\>` 將檔案內容向前還原 `git reset HEAD <file\>` 變更檔案狀態 `git reset --soft HEAD^` 撤銷最近的 commit `git commit --amend` 修改 commit 訊息 - 修改舊 commit 訊息 - https://soarlin.github.io/2021/08/31/Git-%E6%9B%B4%E6%94%B9%E8%88%8A%E7%9A%84-commit-%E8%A8%8A%E6%81%AF/ `git log --pretty=oneline` ### Remote ``` git fetch git merge origin/<remote-branch> ``` ## .gitignore `*` matches anything except a slash. `?` matches any one character except `/`. `**` can match directories anywhere. A convenient `.gitignore` in Linux : ``` * !/**/ !*.* !/**/makefile !makefile ``` ## 開發相關 ### jserv's opinion - 程式註解:幫助理解程式碼行為 - git commit message:幫助追溯程式、專案開發軌跡 - HackMD documentation:開發流水帳及問題記載、如何克服 ## Way to Write Good git Commit source: >[How to Write a Git Commit Message](https://cbea.ms/git-commit/) >[如何寫好 git message](https://blog.louie.lu/2017/03/21/%E5%A6%82%E4%BD%95%E5%AF%AB%E4%B8%80%E5%80%8B-git-commit-message/) ### The seven rules 1. 用一行空白分隔標題和 body 2. 標題保持在 50 個字元以內 3. 標題開頭大寫 4. 標題不加句號 5. 以祈使句撰寫標題 6. message body 每 72 字元就要換行 7. 使用 body 去解釋為何要這麼做 commit message 是為了幫助開發者掌握程式碼變更的脈絡(原因)而非變更內容 ### 標題部份 - 如果無法保持在 50 個字元以內,那麼或許這個 commit 的變更太多了 - 一個良好的 commit 應該適用於以下模板 > If applied, this commit will <標題>. ### body部份 - 解釋 commit 解決了什麼問題,集中描述為何要這麼做而非如何達成,描寫這個變更帶來了什麼不直覺的結果 - 如果有 issue tracker 可以放在最後