Git命令记录

这里没有add/commit/push/pull等命令的介绍

查阅最近的提交修改

git log --stat

查看某个文件在某次提交中的修改

git show <hashcode> <filename>

hashcode就是你想要查看的节点的哈希值

查看仅这个文件的所有历史记录

git log --pretty=oneline <filename>

在某些情况下我们可能希望查看目标文件两个版本之间的差异。 查看这个文件任意两个版本的差异

git diff <hashcode-before-right> <hashcode> <filename>

注意:filename在提交记录中的文件路径可能已经被缩略,我们在写filename一定要写上完整路径,但不该是绝对路径。

查看某个文件的包含提交人员,日期、版本号等记录信息,不包括修改详情

git whatchanged <filename>

恢复已删除的文件

git checkout commit_id file.name
git add file.name
git commit file.name -m ""
git push

查看某次提交修改详情

git show <hashcode>
git log -p <hashcode>

Git log常用参数

-p:按补丁显示每个更新间的差异

--stat:显示每次更新的修改文件的统计信息

--shortstat:只显示--stat中最后的行数添加修改删除统计

--name-only:尽在已修改的提交信息后显示文件清单

--name-status:显示新增、修改和删除的文件清单

--abbrev-commit:仅显示SHA-1的前几个字符,而非所有的40个字符

--relative-date:使用较短的相对时间显示(例如:"two weeks ago")
--graph:显示ASCII图形表示的分支合并历史

--pretty:使用其他格式显示历史提交信息

个性化Git log输出

如果我们只想输出hash.

git log --pretty=format:"%h"
git用各种placeholder来决定各种显示内容:
下面内容来自这里

  • %H: commit hash
  • %h: 缩短的commit hash
  • %T: tree hash
  • %t: 缩短的 tree hash
  • %P: parent hashes
  • %p: 缩短的 parent hashes
  • %an: 作者名字
  • %aN: mailmap的作者名字 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ae: 作者邮箱
  • %aE: 作者邮箱 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ad: 日期 (--date= 制定的格式)
  • %aD: 日期, RFC2822格式
  • %ar: 日期, 相对格式(1 day ago)
  • %at: 日期, UNIX timestamp
  • %ai: 日期, ISO 8601 格式
  • %cn: 提交者名字
  • %cN: 提交者名字 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ce: 提交者 email
  • %cE: 提交者 email (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
  • %cd: 提交日期 (--date= 制定的格式)
  • %cD: 提交日期, RFC2822格式
  • %cr: 提交日期, 相对格式(1 day ago)
  • %ct: 提交日期, UNIX timestamp
  • %ci: 提交日期, ISO 8601 格式
  • %d: ref名称
  • %e: encoding
  • %s: commit信息标题
  • %f: sanitized subject line, suitable for a filename
  • %b: commit信息内容
  • %N: commit notes
  • %gD: reflog selector, e.g., refs/stash@{1}
  • %gd: shortened reflog selector, e.g., stash@{1}
  • %gs: reflog subject
  • %Cred: 切换到红色
  • %Cgreen: 切换到绿色
  • %Cblue: 切换到蓝色
  • %Creset: 重设颜色
  • %C(...): 制定颜色, as described in color.branch.* config option
  • %m: left, right or boundary mark
  • %n: 换行
  • %%: a raw %
  • %x00: print a byte from a hex code
  • %w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1).

除此之外, --graph选项可以显示branch的ascii图例。

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容