首頁·終端機命令

git commit

git

用法

git commit [-a] -m "<message>"

把暫存區裡的內容記成一個提交;-a 只會帶上已追蹤檔案的改動,新建的檔案不算。

常用選項

選項含義
-m "<msg>"give the message on the command line
-astage modified tracked files first; new files are skipped
--amendreplace the previous commit instead of adding one
--no-editkeep the old message, used with --amend
--fixup=<commit>mark it to be folded into <commit> by rebase --autosquash
-Ssign the commit with GPG
--allow-emptycommit with nothing staged

範例

git commit -m "fix login redirect"

records what is staged

git commit -a -m "typo"

stages tracked edits and commits in one step

git commit --amend --no-edit

folds the staged change into the last commit, keeping its message

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git commit 是做什麼的?

把暫存區裡的內容記成一個提交;-a 只會帶上已追蹤檔案的改動,新建的檔案不算。

Q. 怎麼寫?

git commit [-a] -m "<message>" —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-commit