本文已参与「新人创作礼」活动,一起开启掘金创作之路。
Git 命令
对 Git使用过程中的一些常用命令做个记录。
文档只列举出部分常用的 git 命令以部分常用的命令选项,完整的 git 命令及选项请查阅 ☞ git 命令参考文档。
注意:git 版本不同,可能部分命令在使用时有所差异。推荐使用最新版 git 。
git config
git config 命令可以用来查询/设置/替换/取消设置选项。
具体有哪些可选项,可以在命令行使用 git config 命令查看。
C:\Users\zhang>git config
usage: git config [<options>]
Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object
Action
--get get value: name [value-regex]
--get-all get all values: key [value-regex]
--get-regexp get values for regexp: name-regex [value-regex]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value_regex]
--add add a new variable: name value
--unset remove a variable: name [value-regex]
--unset-all remove all matches: name [value-regex]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
-e, --edit open an editor
--get-color find the color configured: slot [default]
--get-colorbool find the color setting: slot [stdout-is-tty]
Type
-t, --type <> value is given this type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--path value is a path (file or directory name)
--expiry-date value is an expiry date
Other
-z, --null terminate values with NUL byte
--name-only show variable names only
--includes respect include directives on lookup
--show-origin show origin of config (file, standard input, blob, command line)
--default <value> with --get, use default value when missing entry
配置项说明
-
--global:在命令行中加入此选项,会将配置信息写入全局配置文件 -
--system:在命令行中加入此选项,会将配置项信息写入系统配置文件 -
--local:默认行为,将配置项信息写入 git 仓库的.git/config文件中 -
--file <file>:简写形式为-f <file>,使用给定的配置文件而不是 GIT_CONFIG 指定的配置文件 -
--get:根据key值获取对应的value,如果key不存在则返回错误代码1,如果key对应多个值,则返回最后一个值
示例
使用 git config 查看或配置用户名和邮箱
// 查看用户名
git config user.name
// 查看已配置的邮箱
git config user.email
// 配置或者修改用户名
git config user.name "my-name"
// 配置或者修改邮箱
git config user.email "my-email@qq.com"
如果不想在每个git目录逐个配置用户名和密码等信息,可以在 git config 命令行加上 --global 选项。使用此选项的 git config 命令,使得配置信息在全局范围内生效。
git clone
git clone 命令克隆一个Git仓库到本地目录。
用法
git clone [<options>] [--] <repo> [<dir>]
E:\PersonalProject>git clone
fatal: You must specify a repository to clone.
usage: git clone [<options>] [--] <repo> [<dir>]
-v, --verbose be more verbose
-q, --quiet be more quiet
--progress force progress reporting
-n, --no-checkout don't create a checkout
--bare create a bare repository
--mirror create a mirror repository (implies bare)
-l, --local to clone from a local repository
--no-hardlinks don't use local hardlinks, always copy
-s, --shared setup as shared repository
--recurse-submodules[=<pathspec>]
initialize submodules in the clone
-j, --jobs <n> number of submodules cloned in parallel
--template <template-directory>
directory from which templates will be used
--reference <repo> reference repository
--reference-if-able <repo>
reference repository
--dissociate use --reference only while cloning
-o, --origin <name> use <name> instead of 'origin' to track upstream
-b, --branch <branch>
checkout <branch> instead of the remote's HEAD
-u, --upload-pack <path>
path to git-upload-pack on the remote
--depth <depth> create a shallow clone of that depth
--shallow-since <time>
create a shallow clone since a specific time
--shallow-exclude <revision>
deepen history of shallow clone, excluding rev
--single-branch clone only one branch, HEAD or --branch
--no-tags don't clone any tags, and make later fetches not to follow them
--shallow-submodules any cloned submodules will be shallow
--separate-git-dir <gitdir>
separate git dir from working tree
-c, --config <key=value>
set config inside the new repository
-4, --ipv4 use IPv4 addresses only
-6, --ipv6 use IPv6 addresses only
--filter <args> object filtering
命令选项说明
repo: 仓库地址dir:【可选】要克隆到的本地目录--branch <branch-name>:简写形式为-b <branch-name>,【可选项】,检出仓库的指定分支,如果不指定,则会检出git仓库的默认分支
示例
克隆某个git仓库
git clone repository-url
克隆git仓库的develop分支
git clone -b develop https://gitee.com/someone/project.git
git remote
git remote 命令用来管理一组跟踪的仓库。
用法
git remote [options]
命令选项
-
-v:在仓库名称后面显示仓库的地址 -
add <name> <url>:为项目添加远程仓库地址 -
rename <old> <new>:修改远程仓库名称 -
remove <name>:移除远程库及其相关的配置信息
示例
查看项目远程地址
git remote -v
添加git远程仓库
git remote add <shortname> <url>
git branch
git branch 命令管理(列出/创建/删除)分支
用法
git branch [options]
使用此命令而不加任何选项时,效果等同于使用 --list 选项。
选项说明
--list:简写形式为-l,列出现有的所有分支,分支名称支持模糊匹配,当前分支会以绿色突出显示并用星号标记--all:简写形式为-a,列出远程分支和本地分支,可以与--list组合使用--delete,简写形式为-d,删除分支--remotes:简写形式为-r,表示远程分支,可以与-d组合使用--force:简写形式为-f,表示强制重置分支至某个节点,可以与-d组合使用-D:--delete --force的简写形式,删除分支时可以加上该选项<branch-name>:要创建和删除的分支名称
示例
在本地创建分支
git branch <branch-name>
删除分支
git branch -d <branch-name>
git checkout
git checkout 命令用来切换分支、更新工作树的文件。
用法
git checkout [options] [<branch>]
如果不加任何选项, git checkout 命令会更新 HEAD,并将指定的分支设置为当前分支。
选项说明
<branch-name>:分支名称,表示切换到指定分支(更新索引和工作树中的文件),如果分支不存在则报错-b <branch-name>:创建一个新分支并检出-B <branch-name>:如果分支不存在的话则新建分支,否则重置分支--force:简写形式为-f,强制切换分支,即使索引和工作树与 HEAD 不同也进行切换,本地修改和任何未跟踪的文件将会被丢弃。
示例
切换到指定分支
git checkout <branch-name>
创建新分支并检出
git checkout -b <branch-name>
git add
git add 命令用于将文件/文件夹添加到索引/暂存区。
用法
git add [options] <file or directory>
注意: 默认情况下,该命令不会添加被忽略的文件。如果在命令行明确指令了任何被忽略的文件,git add 将失败,并显示一个被忽略的文件列表。
命令选项
-
file:该选项可以是具体的文件名或文件夹名称,也可以使用模式匹配来匹配满足条件的文件或文件夹。 -
--dry-run:简写形式为-n,Don’t actually add the file(s), just show if they exist and/or will be ignored. -
--force:简写形式为-f,强制添加文件,允许添加被忽略的文件。 -
--refresh:使用此选项,不会添加任何文件,只会刷新暂存区内文件的stat信息 -
--ignore-errors:忽略错误信息,将命令继续执行。如果某些文件因为索引错误导致无法添加时,使用该选项,不会终止命令执行,而是继续添加其它文件。该命令仍以非零状态退出。可以配置add.ignoreErrors为true,使成为默认行为。
示例
// 添加一个或多个文件到暂存区
git add [file1] [file2] [fileN]
// 添加指定文件夹到暂存区
git add [dir]
// 添加当前目录下的所有文件到暂存区
git add .
git rm
git rm 命令用于从工作树和索引中删除文件或文件夹。
用法
git rm [options] <file or directory>
命令选项
-
--force:简写形式为-f,表示强制删除。如果要删除的文件被修改过或者已经存在于暂存区,则需要加上此选项。 -
--dry-run:简写形式为-n,不会真实删除任何文件,相反仅仅展示文件是否存在于索引中,否则会被命令删除。 (Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.) -
--force:简写形式为-r,表示允许递归删除。如果使用该命令删除目录,则会递归删除指定目录下的所有子目录和文件。 -
--cached:仅从索引(暂存区)中移除文件,不管该文件是否被修改,工作树中的文件都会被保留。 -
--ignore-unmatch:即使没有匹配的文件,也以零状态退出。避免命令执行时因找不到匹配的文件而报错,导致命令中止以及后续命令无法执行。 -
--quiet:简写形式为-q,git rm命令执行时会为删除的文件输出一行日志,使用此选项取消日志的输出。 -
file:要删除的文件或文件夹。支持文件名称或文件夹名称支持模式匹配。
示例
// 删除当前目录下utils文件夹中的以index开头的文件,如果未找到匹配的文件,该命令也不会报错
git rm -f --ignore-unmatch ./utils/index.*
// 删除当前目录下utils文件夹所有内容,不输出日志
git rm -r -f --ignore-unmatch -q ./utils/src
未完待补充