Git 命令

总结一些常用的 Git 命令。

git-checklist

config

  1. git config [–global] user.name “hello”
    设置作者。
  2. git config [–global] user.email “hello@gmail.com
    设置邮箱。
  3. git config [–global] -l
    显示所有别名。
  4. git config –global alias.mm ‘commit -m’
    添加别名。
  5. git config –global –unset alias.mm
    删除别名。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[alias]
st = status
sts = status --short
ci = commit -m
cino = commit --amend --no-edit
br = branch
co = checkout
df = diff
l1 = log -1
l3 = log -3
lp = log --pretty=oneline
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
psm = push origin master
plm = pull origin master
ss = stash save -a
sl = stash list
sa = stash apply
sd = stash drop
rank = shortlog -sne --no-merges
# 查看所有本地分支的最后修改时间
sbtime = !"for k in `git branch|perl -pe s/^..//`;do echo `git show --pretty=format:\"%Cgreen%ci %Cblue%cr%Creset\" $k|head -n 1`\\\t$k;done|sort"
# 查看所有远程分支的最后修改时间
sbrtime = !"for k in `git branch -r|perl -pe s/^..//`;do echo `git show --pretty=format:\"%Cgreen%ci %Cblue%cr%Creset\" $k|head -n 1`\\\t$k;done|sort"

log

1
git log [<options>] [<revision-range>] [[--] <path>...]

options:

  • –oneline: 一行显示
  • -p|–patch: 显示每次提交所引入的差异(按 补丁 的格式输出)
  • -<n>: 仅显示最近的 n 条提交
  • –stat: 显示每次提交的简略统计信息
  • –name-status: 显示新增、修改、删除的文件清单。
  • –name-only: 仅在提交信息后显示已修改的文件清单。
  • –shortstat: 只显示 –stat 中最后的行数修改添加移除统计
  • –relative-date: 同 –date=relative,使用较短的相对时间而不是完整格式显示日期(比如“2 weeks ago”)
  • –pretty: 内建的子选项有 oneline、short、full、fuller,也可以通过 format 指定,比如 –pretty=format:”%d %h - %an, %ar : %s”
  1. git log [filename]
  2. git log -p -3
    显示每次提交所引入的差异,同时可以指定最近的 3 次提交。
  3. git log –graph –relative-date
    时间显示“n 周前“而不是具体日期。
  4. git log –author= –since=”2022-01-01” –until=”2022-12-31” –no-merges | grep -e ‘commit [a-zA-Z0-9]*‘ | wc -l
    统计 git 提交次数。
  5. git log –graph –pretty=’format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%an%C(reset) %s’
  6. git log –stat –since=’7 Day Ago’ –graph –pretty=oneline –abbrev-commit –relative-date

format 常用的选项:

选项 说明
%H 提交的完整哈希值
%h 提交的简写哈希值
%d 分支
%T 树的完整哈希值
%t 树的简写哈希值
%P 父提交的完整哈希值
%p 父提交的简写哈希值
%an 作者名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 –date=选项 来定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期(距今多长时间)
%s 提交说明

Git-基础-查看提交历史

shortlog

Summarizes git log output in a format suitable for inclusion in release announcements. Each commit will be grouped by author and title.

  1. git shortlog
    根据作者,分组显示提交。

  2. git shortlog -sne –no-merges
    列出所有的提交者。

    1
    2
    3
    223  Jose Alcerreca <jalc@google.com>
    110 Jose Alcérreca <JoseAlcerreca@users.noreply.github.com>
    47 Stephan Linzner <slinzner@google.com>

whatchanged

Shows commit logs and diff output each commit introduces.

  1. git whatchanged –oneline
    查看修改的文件列表。
  2. git whatchanged –stat
    查看修改的文件列表及文件修改的统计。

for-each-ref

  1. git for-each-ref –format=”%(committerdate) %09 %(refname) %09 %(authorname)”
    显示各个分支最后一次提交信息。
  2. git for-each-ref --sort=committerdate --format='%(refname:short) * %(authorname) * %(committerdate:relative)' refs/remotes/ | column -t -s '*'
    列出所有遠端的分支,以及每個分支的最後一個 commit。

show

  1. git show

commit

  1. git commit -am “message”
    git add 和 git commit 的合并命令,但是对有新增的文件无效。
  2. git commit –amend
    提交文件并重写信息。
  3. git commit –amend –no-edit
    使用上次的提交信息。

diff

可以用来比较工作区、暂存区、工作目录以及两个分支之间的差异。

  1. git diff
    比较工作区文件与暂存区的差异。

  2. git diff –cached
    比较暂存区和上次提交差异。

  3. git diff –word-diff=plain master

  4. git diff –shortstat master

    1
    1 file changed, 1 insertion(+), 1 deletion(-)
  5. git diff –numstat master

    1
    1       1       ccc.txt
  6. git diff –dirstat master

    1
    2
    95.2% app/src/main/java/com/android/app/activity/
    4.7% app/src/main/java/com/android/app/fragment/
  7. git diff –stat master

    1
    2
    ccc.txt | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
  8. git diff SHA1 SHA2

reset

branch

  1. git branch
    显示本地分支。
  2. git branch -r
    显示远程分支。
  3. git branch -a
    显示所有分支。
  4. git branch -d
    删除本地分支。
  5. git branch -r -d
    删除远程分支。
  6. git branch –contains
    显示当前提交在哪个分支上。
  7. git branch –set-upstream origin/
    关联本地分支到远程分支上,否则 pull 时提示 “no tracking information”。指定以后在 pull、push 的时候不需要指定远程分支了。
  8. git branch -rv –sort=committerdate
    显示各分支下的最后一次提交,降序排序。

checkout

  1. git checkout -b dev origin/dev
    创建本地 dev 分支并关联远程 dev 分支。

add

This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit.

  1. git add -p
    交互式添加文件。

apply

  1. git apply chages.diff

merge

rebase

tag

  1. git tag -l
    显示所有 tag。
  2. git tag
    打标签。
  3. git tag -d
    删除 tag。

pull

push

  1. git push origin :local (:前有空格)
    删除远程分支。

remote

Manage the set of repositories (“remotes”) whose branches you track.

ls-remote

Displays references available in a remote repository along with the associated commit IDs.

  1. git ls-remote –heads origin

    1
    335ec14ca6c31b47abd96f3582144dfe0fcb5ba6  refs/heads/main

rm

  1. git rm –cached
    忽略已经被追踪的文件。

revert

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

用于恢复特定提交的内容,然后生成一个提交,不会影响其他的提交内容。适用于要恢复的提交修改的文件数量少,而且修改点单一,比较实用,不用处理冲突,风险小。

  1. git revert -n
    恢复指定的提交。-n 是 –no-commit 的意思,指不自动生成提交信息。
  2. git revert –abort
    如果产出了冲突,放弃合并。因为要恢复的文件可能别的提交已经更改,所以需要处理冲突。
  3. git revert –quit
    退出合并,但是保留文件修改。

Git - git-revert Documentation (git-scm.com)