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命令使用导航链接
5 注意
在新版本的git中新增的命令,如switch,restore等可能并不在本系列文章中出现,原因是这些新增的命令使用原有命令也可达到同样的效果。
当然文中很多用法可能也是借鉴其他博客或者文章,这些引用的文章也会在文章最后附上原文链接。