git常用命令

Git是一个免费的、开源的分布式版本控制系统,以速度和效率处理从小型到大型的所有项目。

总结些git的常用命令

[version]查看版本

git version

git version 2.25.1

[help]获取帮助

查看帮助

git help -a
执行 'git help <command>' 来查看特定子命令

主要的上层命令
   add                  添加文件内容至索引
   am                   应用邮箱格式的系列补丁
   archive              基于一个指定的树创建文件存档
   bisect               通过二分查找定位引入 bug 的提交
   branch               列出、创建或删除分支
   bundle               通过归档移动对象和引用
   checkout             切换分支或恢复工作区文件
   cherry-pick          应用一些现存提交引入的修改
   citool               git-commit 的图形替代界面
   clean                从工作区中删除未跟踪文件
   clone                克隆仓库到一个新目录
   commit               记录变更到仓库
   describe             基于一个现存的引用为一个对象起一个可读的名称
   diff                 显示提交之间、提交和工作区之间等的差异
   fetch                从另外一个仓库下载对象和引用
   format-patch         准备电子邮件提交的补丁
   gc                   清除不必要的文件和优化本地仓库
   gitk                 Git 仓库浏览器
   grep                 输出和模式匹配的行
   gui                  一个便携的 Git 图形客户端
   init                 创建一个空的 Git 仓库或重新初始化一个已存在的仓库
   log                  显示提交日志
   merge                合并两个或更多开发历史
   mv                   移动或重命名一个文件、目录或符号链接
   notes                添加或检查对象注释
   pull                 获取并整合另外的仓库或一个本地分支
   push                 更新远程引用和相关的对象
   range-diff           比较两个提交范围(如一个分支的两个版本)
   rebase               在另一个分支上重新应用提交
   reset                重置当前 HEAD 到指定状态
   restore              恢复工作区文件
   revert               回退一些现存提交
   rm                   从工作区和索引中删除文件
   shortlog             'git log' 输出摘要
   show                 显示各种类型的对象
   sparse-checkout      初始化及修改稀疏检出
   stash                贮藏脏工作区中的修改
   status               显示工作区状态
   submodule            初始化、更新或检查子模组
   switch               切换分支
   tag                  创建、列出、删除或校验一个 GPG 签名的标签对象
   worktree             管理多个工作区

辅助命令/操作者
   config               获取和设置仓库或者全局选项
   fast-export          Git 数据导出器
   fast-import          Git 快速数据导入器后端
   filter-branch        重写分支
   mergetool            运行合并冲突解决工具以解决合并冲突
   pack-refs            打包头和标签以实现高效的仓库访问
   prune                删除对象库中所有不可达对象
   reflog               管理 reflog 信息
   remote               管理已跟踪仓库
   repack               打包仓库中未打包对象
   replace              创建、列出、删除对象替换引用

辅助命令/询问者
   annotate             使用提交信息注释文件行
   blame                显示文件每一行最后修改的版本和修改者
   count-objects        计算未打包对象的数量和磁盘空间占用
   difftool             使用常见的差异工具显示更改
   fsck                 验证仓库中对象的连通性和有效性
   gitweb               Git web 界面(Git 仓库的 web 前端)
   help                 显示 Git 的帮助信息
   instaweb             在 gitweb 中即时浏览您的工作仓库
   merge-tree           显示三路合并而不动索引
   rerere               重用冲突合并的解决方案记录
   show-branch          显示分支和提交
   verify-commit        检查 GPG 提交签名
   verify-tag           检查标签的 GPG 签名
   whatchanged          显示每一个提交引入的差异日志

与其它系统交互
   archimport           将一个 GNU Arch 仓库导入到 Git
   cvsexportcommit      将一个提交导出到 CVS 检出中
   cvsimport            从另外一个人们爱恨的配置管理系统中拯救你的数据
   cvsserver            Git 的一个 CVS 服务模拟器
   imap-send            从标准输入将一组补丁发送到IMAP文件夹
   p4                   导入和提交到 Perforce 仓库中
   quiltimport          将一个 quilt 补丁集应用到当前分支。
   request-pull         生成待定更改的摘要
   send-email           通过电子邮件发送一组补丁
   svn                  Subersion 仓库和 Git 之间的双向操作

低级命令/操作者
   apply                应用一个补丁到文件和/或索引区
   checkout-index       从索引拷贝文件到工作区
   commit-graph         写入和校验 Git 提交图文件
   commit-tree          创建一个新的提交对象
   hash-object          从一个文件计算对象 ID,并可以创建 blob 数据对象
   index-pack           从一个现存的包存档文件创建包索引
   merge-file           运行一个三路文件合并
   merge-index          对于需要合并的文件执行合并
   mktag                创建一个标签对象
   mktree               基于 ls-tree 的格式化文本创建一个树对象
   multi-pack-index     写入和校验多包索引
   pack-objects         创建对象的存档包
   prune-packed         删除已经在包文件中的多余对象
   read-tree            将树信息读取到索引
   symbolic-ref         读取、修改和删除符号引用
   unpack-objects       从打包文件中解压缩对象
   update-index         将工作区的文件内容注册到索引
   update-ref           安全地更新存储于引用中的对象名称
   write-tree           从当前索引创建一个树对象

低级命令/询问者
   cat-file             提供仓库对象的内容、类型或大小
   cherry               查找尚未应用到上游的提交
   diff-files           比较工作区和索引区中的文件
   diff-index           将一个树和工作区或索引做比较
   diff-tree            比较两个树对象的文件内容和模式
   for-each-ref         对每一个引用输出信息
   get-tar-commit-id    从 git-archive 创建的归档文件中提取提交 ID
   ls-files             显示索引和工作区中文件的信息
   ls-remote            显示一个远程仓库的引用
   ls-tree              显示一个树对象的内容
   merge-base           为了合并查找尽可能好的公共祖先提交
   name-rev             查找给定版本的符号名称
   pack-redundant       查找冗余的包文件
   rev-list             按时间顺序列出提交对象
   rev-parse            选出并处理参数
   show-index           显示打包归档索引
   show-ref             显示本地仓库中的引用
   unpack-file          用 blob 数据对象的内容创建一个临时文件
   var                  显示一个Git逻辑变量
   verify-pack          校验打包的Git存仓文件

低级命令/同步仓库
   daemon               一个非常简单的 Git 仓库服务器
   fetch-pack           从另一个仓库获取缺失的对象
   http-backend         Git HTTP 协议的服务端实现
   send-pack            使用 Git 协议推送对象到另一个仓库
   update-server-info   更新辅助信息文件以帮助哑协议服务

低级命令/内部助手
   check-attr           显示 gitattributes 信息
   check-ignore         调试 gitignore / exclude 文件
   check-mailmap        显示联系人的规范名称和电子邮件
   check-ref-format     确保引用名称格式正确
   column               以列的方式显示数据
   credential           检索和存储用户密码
   credential-cache     在内存中临时存储密码的助手
   credential-store     在磁盘存储密码的助手
   fmt-merge-msg        生成一个合并提交信息
   interpret-trailers   添加或解析提交说明中的结构化信息
   mailinfo             从单个电子邮件中提取补丁和作者身份
   mailsplit            简单的 UNIX mbox 邮箱切分程序
   merge-one-file       与 git-merge-index 一起使用的标准向导程序
   patch-id             计算一个补丁的唯一 ID
   sh-i18n              为 shell 脚本准备的 Git 国际化设置代码
   sh-setup             常用的 Git shell 脚本设置代码
   stripspace           删除不必要的空白字符

使用方法

git help add

查看指导手册

git help -g
最常用的 Git 向导有:
   attributes          定义路径的属性
   cli                 Git 命令行界面和约定
   core-tutorial       面向开发人员的 Git 核心教程
   cvs-migration       适合 CVS 用户的 Git 帮助
   diffcore            调整差异输出
   everyday            每一天 Git 的一组有用的最小命令集合
   glossary            Git 词汇表
   hooks               Git 使用的钩子
   ignore              忽略指定的未跟踪文件
   modules             定义子模组属性
   namespaces          Git 名字空间
   repository-layout   Git 仓库布局
   revisions           指定 Git 的版本和版本范围
   submodules          将一个仓库安装到另外一个仓库中
   tutorial            一个 Git 教程
   tutorial-2          一个 Git 教程:第二部分
   workflows           Git 推荐的工作流概览

使用方法

git help attributes

常用指令

[add]将文件内容添加到索引

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
                 [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
                 [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
                 [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
                 [--] [<pathspec>...]

添加一个文件

git add <file>

添加目录

git add <path/*>

[branch]列出、创建或删除分支

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>]

查看本地分支

git branch

查看远程分支

git branch -r

查看所有分支

git branch -a

创建分支

git branch -f <branchName>

删除本地分支

git branch -d <branchName>

删除远程分支

git branch -d -r <branchname>

重命名分支

git branch -m <branchNameOld> <branchNameNew>

[checkout]切换分支或恢复工作区文件

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]

切换到新分支

git checkout <branch>

取回误删的文件

git checkout <commitId> <file>

[clean]从工作区中删除未跟踪文件

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...

删除未跟踪文件

git clean -df <path>

[clone]克隆仓库到一个新目录

git clone [--template=<template_directory>]
                 [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
                 [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
                 [--dissociate] [--separate-git-dir <git dir>]
                 [--depth <depth>] [--[no-]single-branch] [--no-tags]
                 [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
                 [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--] <repository>
                 [<directory>]

克隆仓库

git clone <repository> -b <branch>

[commit]记录变更到仓库

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
                  [--dry-run] [(-c | -C | --fixup | --squash) <commit>]
                  [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
                  [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
                  [--date=<date>] [--cleanup=<mode>] [--[no-]status]
                  [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
                  [-S[<keyid>]] [--] [<pathspec>...]

记录变更

git commit

追加变更到当前记录

git commit --amend

[diff]显示提交之间、提交和工作区之间等的差异

git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --cached [<commit>] [--] [<path>...]
git diff [<options>] <commit> <commit> [--] [<path>...]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>

查看当前的修改

git diff

查看两个分支的修改

git diff <branch1> <branch2>

查看某个文件的修改

git diff <file1> <file2>

显示两分支指定路径下文件的详细diff信息

git diff <branch1> <branch2> <path>

[fetch]从另外一个仓库下载对象和引用

git fetch [<options>] [<repository> [<refspec>...]]
git fetch [<options>] <group>
git fetch --multiple [<options>] [(<repository> | <group>)...]
git fetch --all [<options>]

从另外一个仓库下载代码

git fetch <repository> 

[gitk]Git 仓库浏览器

gitk [<options>] [<revision range>] [--] [<path>...]

sudo apt-get install gitk

创建一个空的 Git 仓库或重新初始化一个已存在的仓库

git init [-q | --quiet] [--bare] [--template=<template_directory>]
                 [--separate-git-dir <git dir>]
                 [--shared[=<permissions>]] [directory]

[log]显示提交日志

git log [<options>] [<revision range>] [[--] <path>...]

[merge]合并两个或更多开发历史

git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
       [--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
       [--[no-]allow-unrelated-histories]
       [--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...]
git merge (--continue | --abort | --quit)

[pull]获取并整合另外的仓库或一个本地分支

git pull [<options>] [<repository> [<refspec>...]]

[push]更新远程引用和相关的对象

git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
          [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
          [-u | --set-upstream] [-o <string> | --push-option=<string>]
          [--[no-]signed|--signed=(true|false|if-asked)]
          [--force-with-lease[=<refname>[:<expect>]]]
          [--no-verify] [<repository> [<refspec>...]]

[rebase]在另一个分支上重新应用提交

git rebase [-i | --interactive] [<options>] [--exec <cmd>]
       [--onto <newbase> | --keep-base] [<upstream> [<branch>]]
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
       --root [<branch>]
git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)

[reset]重置当前 HEAD 到指定状态

git reset [-q] [<tree-ish>] [--] <pathspec>...
git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

回退全部提交

git reset --hard

回退到末次提交

git reset --hard <commitId>

[restore]恢复工作区文件

git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...
git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>...]

恢复已add的文件

git restore --staged <file>

[revert]回退一些现存提交

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>...
git revert (--continue | --skip | --abort | --quit)

回退指定提交

git revert <commitId>

[stash]贮藏脏工作区中的修改

git stash list [<options>]
git stash show [<options>] [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
            [-u|--include-untracked] [-a|--all] [-m|--message <message>]
            [--] [<pathspec>...]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>

暂存本地修改

git stash

恢复暂存的修改

git stash apply
git stash pop

[status]显示工作区状态

git status [<options>...] [--] [<pathspec>...]

查看工作区状态

git status

[blame]显示文件每一行最后修改的版本和修改者

git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental]
           [-L <range>] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>]
           [--ignore-rev <rev>] [--ignore-revs-file <file>]
           [--progress] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>..<rev>]
           [--] <file>

显示指定代码行的最后修改

git blame -L <num_10> <file>

[apply]应用一个补丁到文件和/或索引区

git apply [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
         [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
         [--allow-binary-replacement | --binary] [--reject] [-z]
         [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
         [--ignore-space-change | --ignore-whitespace]
         [--whitespace=(nowarn|warn|fix|error|error-all)]
         [--exclude=<path>] [--include=<path>] [--directory=<root>]
         [--verbose] [--unsafe-paths] [<patch>...]

应用一个补丁

git apply <patch>

[reflog]管理 reflog 信息

git reflog <subcommand> <options>
    git reflog [show] [log-options] [<ref>]
    git reflog expire [--expire=<time>] [--expire-unreachable=<time>]
           [--rewrite] [--updateref] [--stale-fix]
           [--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>...]
    git reflog delete [--rewrite] [--updateref]
           [--dry-run | -n] [--verbose] ref@{specifier}...
    git reflog exists <ref>

[tag]创建、列出、删除或校验一个 GPG 签名的标签对象

git tag [-a | -s | -u <keyid>] [-f] [-m <msg> | -F <file>] [-e]
       <tagname> [<commit> | <object>]
git tag -d <tagname>...
git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]
       [--points-at <object>] [--column[=<options>] | --no-column]
       [--create-reflog] [--sort=<key>] [--format=<format>]
       [--[no-]merged [<commit>]] [<pattern>...]
git tag -v [--format=<format>] <tagname>...

扩展功能

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340