2.Git常用命令一览表

1.git

命令描述
git --help查看git帮助
git help <command>
git <command> --help
查看git指定命令帮助
git --version查看git版本
git -C <path> <command>在指定路径下运行git指定命令

2.git config

以下仅以 --global 选项为示例。

命令描述
git config --global --list查看用户级的所有配置
git config --global <key>查看用户级的指定配置
git config --global <key> <value>新增或更改用户级的指定配置
git config --global user.name "<name>"新增或更改用户级的用户名配置
git config --global user.email "<email>"新增或更改用户级的邮箱地址配置
git config --global init.defaultBranch "main"新增或更改用户级的 git init 命令的默认分支名称配置
git config --global --unset <key>删除用户级的指定配置
选项配置文件作用域
--system/etc/gitconfig 文件所有用户
--global~/.gitconfig 或 ~/.config/git/config 文件。当前用户
--local当前仓库目录/.git/config文件当前仓库(需要在当前仓库目录下运行 --local 选项才有用)

3.git init

命令描述
git init将当前目录初始化为一个本地仓库,并在当前目录下创建一个名称为 .git 的子目录。
git init -b "main"执行 git init 命令,并初始化分支名称为 "main"
git init <directory>将指定目录初始化为一个本地仓库,并在指定目录下创建一个名称为 .git 的子目录。

4.git clone

命令描述
git clone <remote-url>克隆远程仓库当前分支的最新提交到当前目录
git clone <remote-url> <directory>克隆远程仓库当前分支的最新提交到指定目录
git clone <remote-url> <directory> --branch <tag-name>克隆远程仓库指定标签到指定目录

5.git add

命令描述
git add .跟踪所有文件,相当于添加所有文件到工作区 或
添加所有文件到暂存区
git add <file>跟踪指定文件,相当于添加指定文件到工作区 或
添加指定文件到暂存区
git add <directory>跟踪指定目录(包括子目录),相当于添加指定目录(包括子目录)到工作区 或
添加指定目录(包括子目录)到暂存区

6.git commit

命令描述
git commit -m '<msg>'提交暂存区到本地仓库,并输入信息。
git commit -a提交工作区直接到本地仓库,跳过了暂存区。

7.git status

命令描述
git status查看状态

8.git remote

命令描述
git remote查看所有远程仓库(仅显示<remote-name>)
git remote -v查看所有远程仓库(显示<remote-name>和<remote-url>)
git remote get-url <remote-name>查看指定远程仓库的<remote-url>
git remote add <remote-name> <remote-url>添加远程仓库,并给远程仓库命名
git remote rename <old-remote-name> <new-remote-name>更改远程仓库的<remote-name>
git remote set-url <remote-name> <new-remote-url> [<old-remote-url>]更改远程仓库的<remote-url>
git remote remove <remote-name>删除远程仓库

9.git push

命令描述
git push -u <remote-name> <branch-name>将本地仓库指定分支与远程仓库对应分支关联,并推送本地仓库指定分支到远程仓库对应分支。等同于 git branch -u <remote-name>/<branch-name> <branch-name> + git push <remote-name> <branch-name>
git push <remote-name> <branch-name>推送本地仓库指定分支到远程仓库对应分支。

10.git fetch

命令描述
git fetch <remote-name> <branch-name>获取远程仓库指定分支相对于本地仓库对应分支没有的所有变化,但不合并到本地仓库对应分支。

11.git merge

命令描述
git merge <branch-name>合并指定分支到当前分支

12.git pull

命令描述
git pull <remote-name> <branch-name>等同于 git fetch + git merge

13.git tag

命令描述
git tag [--list]查看所有标签
git tag --list 'v1.8.5*'查看匹配指定模式的标签
git tag -a <annotated-tag-name> -m 'my version 1.8'给最新提交新增一个附注标签并输入信息
git tag -a <annotated-tag-name> <commit>给指定提交新增一个附注标签
git tag <lightweight-tag-name>给最新提交新增一个轻量标签
git tag <lightweight-tag-name> <commit>给指定提交新增一个轻量标签
git tag -d <tag-name>删除指定标签

14.git branch

命令描述
git branch -a查看本地仓库和远程仓库的所有分支,并用星号 * 标示当前所在分支。
git branch查看本地仓库的所有分支,并用星号 * 标示当前所在分支。
git branch -r查看远程仓库的所有分支
git branch <branch-name>在本地仓库新增一个分支,但依然停留在当前分支。
git branch -m [<old-branch-name>] <new-branch-name>重命名本地仓库指定分支
git branch -d <branch-name>删除本地仓库指定分支
git branch -u <remote-name>/<branch-name> <branch-name>将本地仓库指定分支与远程仓库对应分支关联。
git branch --unset-upstream <branch-name>取消本地仓库指定分支与远程仓库对应分支的关联。

15.git switch

命令描述
git switch <branch-name>切换到本地仓库的指定分支

16.git checkout

命令描述
git checkout <branch-name>git switch <branch-name>
git checkout -b <branch-name>在本地仓库新增一个分支,并切换到此新分支。等同于 git branch <branch-name> + git checkout <branch-name>

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/linux/git/31575.html

(0)
huoxiaoqiang的头像huoxiaoqiang
上一篇 2023年3月1日
下一篇 2023年3月2日

相关推荐

发表回复

登录后才能评论