Git 命令
总结一些常用的 Git 命令。
config
- git config [–global] user.name “hello”
设置作者。 - git config [–global] user.email “hello@gmail.com“
设置邮箱。 - git config [–global] -l
显示所有别名。 - git config –global alias.mm ‘commit -m’
添加别名。 - git config –global –unset alias.mm
删除别名。
1 | [alias] |
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”
- git log [filename]
- git log -p -3
显示每次提交所引入的差异,同时可以指定最近的 3 次提交。 - git log –graph –relative-date
时间显示“n 周前“而不是具体日期。 - git log –author=
–since=”2022-01-01” –until=”2022-12-31” –no-merges | grep -e ‘commit [a-zA-Z0-9]*‘ | wc -l
统计 git 提交次数。 - git log –graph –pretty=’format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%an%C(reset) %s’
- 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 | 提交说明 |
shortlog
Summarizes git log output in a format suitable for inclusion in release announcements. Each commit will be grouped by author and title.
git shortlog
根据作者,分组显示提交。git shortlog -sne –no-merges
列出所有的提交者。1
2
3223 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.
- git whatchanged –oneline
查看修改的文件列表。 - git whatchanged –stat
查看修改的文件列表及文件修改的统计。
for-each-ref
- git for-each-ref –format=”%(committerdate) %09 %(refname) %09 %(authorname)”
显示各个分支最后一次提交信息。 git for-each-ref --sort=committerdate --format='%(refname:short) * %(authorname) * %(committerdate:relative)' refs/remotes/ | column -t -s '*'
列出所有遠端的分支,以及每個分支的最後一個 commit。
show
- git show
commit
- git commit -am “message”
git add 和 git commit 的合并命令,但是对有新增的文件无效。 - git commit –amend
提交文件并重写信息。 - git commit –amend –no-edit
使用上次的提交信息。
diff
可以用来比较工作区、暂存区、工作目录以及两个分支之间的差异。
git diff
比较工作区文件与暂存区的差异。git diff –cached
比较暂存区和上次提交差异。git diff –word-diff=plain master
git diff –shortstat master
1
1 file changed, 1 insertion(+), 1 deletion(-)
git diff –numstat master
1
1 1 ccc.txt
git diff –dirstat master
1
295.2% app/src/main/java/com/android/app/activity/
4.7% app/src/main/java/com/android/app/fragment/git diff –stat master
1
2ccc.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)git diff SHA1 SHA2
reset
branch
- git branch
显示本地分支。 - git branch -r
显示远程分支。 - git branch -a
显示所有分支。 - git branch -d
删除本地分支。 - git branch -r -d
删除远程分支。 - git branch –contains
显示当前提交在哪个分支上。 - git branch –set-upstream
origin/
关联本地分支到远程分支上,否则 pull 时提示 “no tracking information”。指定以后在 pull、push 的时候不需要指定远程分支了。 - git branch -rv –sort=committerdate
显示各分支下的最后一次提交,降序排序。
checkout
- 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.
- git add -p
交互式添加文件。
apply
- git apply chages.diff
merge
rebase
tag
- git tag -l
显示所有 tag。 - git tag
打标签。 - git tag -d
删除 tag。
pull
push
- 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.
git ls-remote –heads origin
1
335ec14ca6c31b47abd96f3582144dfe0fcb5ba6 refs/heads/main
rm
- 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).
用于恢复特定提交的内容,然后生成一个提交,不会影响其他的提交内容。适用于要恢复的提交修改的文件数量少,而且修改点单一,比较实用,不用处理冲突,风险小。
- git revert -n
恢复指定的提交。-n 是 –no-commit 的意思,指不自动生成提交信息。 - git revert –abort
如果产出了冲突,放弃合并。因为要恢复的文件可能别的提交已经更改,所以需要处理冲突。 - git revert –quit
退出合并,但是保留文件修改。