1 目的
本系列文章主要记录一些常用git命令的详细用法,使其作为一个工具书的形式查阅使用,比如在需要使用git完成某些操作时查阅本系列文章找到对应的用法,又比如想要拓展一下git的使用,找到一些自己没有用过的git功能或者一些命令的高级使用技巧。
2 适用人群
本系列文章并不作为一个git教程使用,不会从零开始讲解git的历史,安装下载,工作原理,基础命令使用,而是以介绍命令的形式详细介绍每个命令的详细使用方式。所以文章默认你是已经使用过git并对git的工作原理有一定理解,能够使用git的一些基本命令完成简单的版本管理的基础上,想要使用一些更高级或者更复杂的功能,并想拓展一些使用技巧而阅读本系列文章的读者。
如果你是一个还没有接触过git或者只接触过其他版本管理工具的新手,可以阅读Progit Book这本GIT官方资料书,里面详细介绍了git的历史,工作原理,基本使用方式,及一些高级用法,足够让你掌握git并在实际工作中运用,网页中还包含了中文版PDF的下载链接。
这里也给出了网页版的git命令官方使用手册。
3 命令行查看git帮助
首先在你的命令行执行git help -a
命令查看现有的git命令,可以查看当前git涵盖的所有命令,包括一些常用命令,辅助命令和底层命令:
$ git help -a
See 'git help <command>' to read about a specific subcommand
Main Porcelain Commands
add Add file contents to the index
am Apply a series of patches from a mailbox
archive Create an archive of files from a named tree
bisect Use binary search to find the commit that introduced a bug
branch List, create, or delete branches
...
Ancillary Commands / Manipulators
config Get and set repository or global options
fast-export Git data exporter
...
Ancillary Commands / Interrogators
annotate Annotate file lines with commit information
blame Show what revision and author last modified each line of a file
...
Interacting with Others
archimport Import a GNU Arch repository into Git
cvsexportcommit Export a single commit to a CVS checkout
...
Low-level Commands / Manipulators
apply Apply a patch to files and/or to the index
checkout-index Copy files from the index to the working tree
commit-graph Write and verify Git commit-graph files
...
Low-level Commands / Interrogators
cat-file Provide content or type and size information for repository objects
cherry Find commits yet to be applied to upstream
diff-files Compares files in the working tree and the index
...
Low-level Commands / Syncing Repositories
daemon A really simple server for Git repositories
...
Low-level Commands / Internal Helpers
check-attr Display gitattributes information
...
使用git <command> --help
命令可以查看每条命令的具体用法,比如git branch:
$ git branch --help
GIT-BRANCH(1) Git Manual
NAME
git-branch - List, create, or delete branches
SYNOPSIS
git branch [--color[=<when>] | --no-color] [--show-current]
[-v [--abbrev=<length> | --no-abbrev]]
[--column[=<options>] | --no-column] [--sort=<key>]
[(--merged | --no-merged) [<commit>]]
[--contains [<commit]] [--no-contains [<commit>]]
[--points-at <object>] [--format=<format>]
[(-r | --remotes) | (-a | --all)]
[--list] [<pattern>...]
git branch [--track | --no-track] [-f] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-c | -C) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>...
git branch --edit-description [<branchname>]
DESCRIPTION
If --list is given, or if there are no non-option arguments, existing branches are listed; the current branch will be highlighted in green and marked with an asterisk. Any branches
checked out in linked worktrees will be highlighted in cyan and marked with a plus sign. Option -r causes the remote-tracking branches to be listed, and option -a shows both local and
remote branches.
...
4 git命令使用导航链接
git init
和git clone
获取仓库 (一)
git config
配置Git (二)
git add
跟踪新文件和加入索引 (三)
git commit
提交更新到Git存储库 (四)
git status
检查当前工作树文件状态 (五)
git diff
查看文件在工作树和历史提交中的修改 (六)
git rm
从Git存储库移除对文件的跟踪 (七)
git mv
对跟踪文件进行移动重命名 (八)
git log
查看提交历史 (九)
git reset
撤销索引操作和git restore
恢复工作树文件 (十)
git checkout
检出操作的各种功能 (十一)
git remote
对远程仓库的操作和使用 (十二)
git fetch
和git pull
抓取远端Git存储库更新到本地Git存储库 (十三)
git push
推送本地Git存储库更新到远端Git存储数据库 (十四)
git tag
标签操作 (十五)
git show
显示各种各样的Git存储库中的提交对象信息 (十六)
git branch
分支管理和git switch
分支切换 (十七)
git worktree
实用的工作树管理 (十八)
git merge
第一种合并分支操作 (十九)
git rebase
第二种合并分支操作和变基整理分支操作 (二十)
git cherry-pick
拣选提交工作流 (二十一)
git revert
还原提交操作 (二十二)
git format-patch
生成用于邮件发送的补丁 (二十三)
git apply
第一种应用补丁到工作树的操作 (二十四)
git am
第二种应用补丁到工作树的操作 (二十五)
git bundle
打包提交到二进制文件 (二十六)
git describe
为发布版本生成一个构建号 (二十七)
git archive
归档一个版本并用于发布 (二十八)
git shortlog
制作提交简报对提交信息进行总结 (二十九)
git reflog
查看引用日志 (三十)
git stash
储藏工作区未保存更改操作 (三十一)
git clean
清理工作目录 (三十二)
git grep
Git存储数据库搜索操作 (三十三)
git bisect
使用二分搜索来查找引入bug的提交 (三十四)
git filer-branch
底层重写历史操作 (三十五)
git submodule
子模块相关操作 (三十六)
5 VS Code中使用git
在使用VS Code中,VS Code已经集成了一些不错的git,比如大家熟知的GitLens
它提供了不错的基础功能,包括显示文件状态,提交信息,文件历史,分支信息,远程仓库信息,暂存内容,标签管理,贡献者统计,搜索和比较功能。使用GitLens
已经足够完成一些日常的版本管理工作:
- 工作树文件状态显示:
不仅可以查看还可以添加暂存,取消暂存,还原修改等。 - 当前分支提交信息显示:
其他一些功能都非常实用,比如分支比较,历史信息查询等。
另一个比较有意思的插件是Git Graph
,它可以详细显示整个Git存储库的分支状态,并以提交层次显示提交信息:
当然还有一些Git GUI,比如GitHub Desktop
,SourceTree
,GitKraken
都是不错的gui桌面。
6 注意
- 在新版本的git中新增的命令,如switch,restore等可能并不在本系列文章中出现,原因是这些新增的命令使用原有命令也可达到同样的效果。
- 在后续命令介绍中
[]
中的选项表示为可选,<>
中的选项表示必填,以此区分哪些选项是必须写入的哪些选项是可选的。 - 当然文中很多用法可能也是借鉴其他博客或者文章,这些引用的文章也会在文章最后附上原文链接。