Git基本命令

总述

Git 是一个快速的、可扩展的、分布的版本控制系统,它具有丰富的命令控制指令,并提供高级的操作。

git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
    [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
    [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    [--super-prefix=<path>] [--config-env=<name>=<envvar>]
    <command> [<args>]

参数说明

  • -v alias: [--version, version]

    打印 Git 的版本信息。优先级高于 --help

    version

  • -h alias: [--help, help]

    打印全部的指令参数信息并列举介绍出一些常用的命令。携带 --all-a 参数,将打印所有可用的命令。

  • -C

    Change,官方文档没看明白也没用明白……

    修改当前工作目录。当使用多个时 -C <path> -C <path> ...,后继都是对前驱的解释。当path存在但是为空时,则当前工作目录不变:git -C ""(使用的2.37.3.windows.1版本已经不支持接收空值)。

    This option affects options that expect path name like --git-dir and --work-tree in that their interpretations of the path names would be made relative to the working directory caused by the -C option. For example the following invocations are equivalent:

    git --git-dir=a.git --work-tree=b -C c status
    git --git-dir=c/a.git --work-tree=c/b status
    
  • -c =

    config

    将配置参数传递给命令。给定的值<value>将覆盖配置文件中的值。 <name> 的格式应与 git config 列出的格式相同(子键以点分隔)。

高级命令

部分命令

⭐git 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=<文件> [--pathspec-file-nul]]
          [--] [<指定路径>…​]
    
  • 描述

    该命令在工作树中找到的当前的内容以更新索引,为下一次提交准备暂存的内容。它通常会整体添加现有路径的当前内容,但是通过某些选项,它也可以用于仅添加对工作树文件所做的部分更改,或删除工作树中不存在的路径。

    “索引”保存着工作树内容的快照,该快照作为下一次提交的内容。因此,在对工作树进行任何更改之后,以及在运行commit命令之前,必须使用add命令将所有新文件或修改过的文件添加到索引中。

    提交前可以多次执行此命令。它仅在运行add命令时添加指定文件的内容;如果要在下一次提交中包含后续更改,则必须再次运行git add将新内容添加到索引中。

    git status 命令可用于获取摘要,说明哪些变化的文件已暂存,准备下一次提交。

    git add 命令默认不会添加被忽略的文件。如果在命令行上明确指定了任何被忽略的文件,则git add会失败,并显示被忽略文件的列表。由目录递归或Git执行的文件名通配符(在script之前引用的通配符)所涉及的忽略文件将被静默忽略。 git add 命令可用于通过 -f 选项(force)强制添加忽略的文件。

    请参阅 git-commit[1] 了解将内容添加到提交的其他替代方法。

git am

应用来自邮件的补丁

通过电子邮件收到了一个需要整合进入项目的补丁,需要将其应用到主题分支中进行评估。 有两种应用该种补丁的方法:使用 git apply,或者使用 git am

如果补丁的贡献者也是一个 Git 用户,并且其能熟练使用 format-patch 命令来生成补丁,这样的话你的工作会变得更加轻松,因为这种补丁中包含了作者信息和提交信息供你参考。 如果可能的话,请鼓励贡献者使用 format-patch 而不是 diff 来为你生成补丁。 而只有对老式的补丁,你才必须使用 git apply 命令。

要应用一个由 format-patch 命令生成的补丁,你应该使用 git am 命令 (该命令的名字 am 表示它“应用(Apply)一系列来自邮箱(Mailbox)的补丁”)。 从技术的角度看,git am 是为了读取 mbox 文件而构建的, mbox 是一种用来在单个文本文件中存储一个或多个电子邮件消息的简单纯文本格式。 其大致格式如下所示:

From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith <jessica@example.com>
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function

Limit log functionality to the first 20

这其实就是你前面看到的 git format-patch 命令输出的开始几行, 而同时它也是有效的 mbox 电子邮件格式。 如果有人使用 git send-email 命令将补丁以电子邮件的形式发送给你, 你便可以将它下载为 mbox 格式的文件,之后将 git am 命令指向该文件,它会应用其中包含的所有补丁。 如果你所使用的邮件客户端能够同时将多封邮件保存为 mbox 格式的文件, 你甚至能够将一系列补丁打包为单个 mbox 文件,并利用 git am 命令将它们一次性全部应用。

然而,如果贡献者将 git format-patch 生成的补丁文件上传到工单系统或类似的任务处理系统, 你可以先将其保存到本地,之后通过 git am 来应用补丁:

$ git am 0001-limit-log-function.patch
Applying: add limit to log function

你会看到补丁被顺利地应用,并且为你自动创建了一个新的提交。 其中的作者信息来自于电子邮件头部的 From 和 Date 字段,提交消息则取自 Subject 和邮件正文中补丁之前的内容。 比如,应用上面那个 mbox 示例后生成的提交是这样的:

$ git log --pretty=fuller -1
commit 6c5e70b984a60b3cecd395edd5b48a7575bf58e0
Author:     Jessica Smith <jessica@example.com>
AuthorDate: Sun Apr 6 10:17:23 2008 -0700
Commit:     Scott Chacon <schacon@gmail.com>
CommitDate: Thu Apr 9 09:19:06 2009 -0700

   add limit to log function

   Limit log functionality to the first 20

其中 Commit 信息表示的是应用补丁的人和应用补丁的时间。 Author 信息则表示补丁的原作者和原本的创建时间。

但是,有时候无法顺利地应用补丁。 这也许是因为你的主分支和创建补丁的分支相差较多,也有可能是因为这个补丁依赖于其他你尚未应用的补丁。 这种情况下,git am 进程将会报错并且询问你要做什么:

$ git am 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Patch failed at 0001.
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".

该命令将会在所有出现问题的文件内加入冲突标记,就和发生冲突的合并或变基操作一样。 而你解决问题的手段很大程度上也是一样的——即手动编辑那些文件来解决冲突,暂存新的文件, 之后运行 git am --resolved 继续应用下一个补丁:

$ (fix the file)
$ git add ticgit.gemspec
$ git am --resolved
Applying: seeing if this helps the gem

如果你希望 Git 能够尝试以更加智能的方式解决冲突,你可以对其传递 -3 选项来使 Git 尝试进行三方合并。 该选项默认并没有打开,因为如果用于创建补丁的提交并不在你的版本库内的话,这样做是没有用处的。 而如果你确实有那个提交的话——比如补丁是基于某个公共提交的——那么通常 -3 选项对于应用有冲突的补丁是更加明智的选择。

$ git am -3 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.

比如上面这种情况,如果没有 -3 选项的话,这看起来就像是存在一个冲突。 由于使用了 -3 选项,该补丁就被干净地应用了

如果你正在利用一个 mbox 文件应用多个补丁,也可以在交互模式下运行 am 命令, 这样在每个补丁之前,它会停住询问你是否要应用该补丁:

$ git am -3 -i mbox
Commit Body is:
--------------------------
seeing if this helps the gem
--------------------------
Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all

这在你保存的补丁较多时很好用,因为你可以在应用之前查看忘掉内容的补丁,并且跳过已经应用过的补丁。

当与你的特性相关的所有补丁都被应用并提交到分支中之后,你就可以选择是否以及如何将其整合到更长期的分支中去了。

git archive

归档打包文件树

  • 概述

    git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
              [-o <file> | --output=<file>] [--worktree-attributes]
              [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
              [<path>…​]
    
  • 描述

    将所需文件或文件树打包压缩至压缩包中[tar、tgz、tar.gz、zip]。

git bisect

使用二分搜索发现 commit 中的 Bug 并显示

  • 概述

    git bisect <subcommand> <options>
    
  • 描述

    此命令使用二分搜索算法检索项目的 commit 历史中的错误。bad:存在错误,good:阴性 commit。

⭐git branch

列出、新建、删除分支

  • 概述

    git branch [--color[=<when>] |--no-color] [--show-current]
        [-v [--abbrev=<n> |--no-abbrev] ]
        [--column[=<options>] | --no-column][--sort=<key>]
        [--合并 [<commit>]][--no-merged [<commit>]]。
        [--包含[<commit>]][--no-contains [<commit>]] [--contains [<commit>]]
        [--points-at <object>] [--format=<format>]。
        [(-r | --remotes) | (-a | --all)
        [--列表] [<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>  # Move/rename a branch, together with its config and reflog.
    git branch (-c | -C) [<oldbranch>] <newbranch>  # Copy a branch, together with its config and reflog.
    git branch (-d | -D) [-r] <branchname>…​  # Delete a branch. 
    git branch --edit-debashion [<branchname>])
    
  • 描述

    如果给了--list,或者没有非选项参数,现有的分支将被列出;当前的分支将以绿色突出显示,并标有星号。 在链接的工作树中检查出来的任何分支将以青色突出显示,并标有加号。选项-r导致远程跟踪的分支被列出,选项-a显示本地和远程分支。

    如果给出了<pattern>,它被用作bash通配符,将输出限制在匹配的分支上。如果给出多个模式,如果一个分支与任何模式相匹配,就会显示出来。

    注意,当提供""时,必须使用"--list";否则命令可能被解释为创建分支。

    使用 --contains 时,只显示包含指定提交的分支(换句话说,尖端提交是指定提交的后裔的分支),--no-contains 会反过来。如果使用 --merged,则只列出被合并到指定提交的分支(即其顶端提交可以从指定提交处到达的分支)。 如果使用"--no-merged",则只列出未被合并到指定提交的分支。 如果缺少参数,则默认为HEAD(即当前分支的顶端)。

    该命令的第二种形式是创建一个名为的新分支头,该分支头指向当前的HEAD,如果给定的话,则指向。作为一种特殊情况,对于,你可以使用"A…​B"作为AB的合并基础的快捷方式,如果正好有一个合并基础。你最多可以省略AB中的一个,在这种情况下,它默认为HEAD

    注意,这将创建新的分支,但不会将工作树切换到它;使用 git switch <newbranch>来切换到新的分支。

    当一个本地分支从远程跟踪的分支开始时,Git 会设置该分支(特别是 branch.<name>.remote 和 branch.<name>.merge 配置项),以便 "git pull "能适当地从远程跟踪的分支合并。这种行为可以通过全局的 branch.autoSetupMerge 配置标志来改变。该设置可以通过使用--track--no-track选项来覆盖,并在之后使用git branch --set-upstream-to来改变。

    使用-m-M选项,将被重命名为。 如果 有相应的 reflog,它将被重命名以匹配 ,并创建一个 reflog 条目以记住分支重命名。如果 存在,必须使用 -M 来强制重命名。

    -c-C选项的语义与-m-M完全相同,只不过不是对分支进行重命名,而是将其连同其配置和日志一起复制到一个新的名称。

    使用-d-D选项,<branchname>将被删除。 你可以指定一个以上的分支进行删除。 如果该分支目前有一个reflog,那么reflog也将被删除。

    使用 -r 和 -d 来删除远程跟踪的分支。注意,只有当远程分支不再存在于远程仓库或 "git fetch "被配置为不再获取它们时,删除远程跟踪分支才有意义。参见 git-remote[1] 的 "prune "子命令,它可以清理所有过时的远程跟踪分支。

git bundle

压缩并转移版本内容

  • 概述

    git bundle create [-q | --quiet | --progress | --all-progress] [--all-progress-implied]
                [--version=<version>] <file> <git-rev-list-args>
    git bundle verify [-q | --quiet] <file>
    git bundle list-heads <file> [<refname>…​]
    git bundle unbundle [--progress] <file> [<refname>…​]
    
  • 描述

    git push命令所传输的所有内容打包成一个二进制文件, 你可以将这个文件通过邮件或者闪存传给其他人,然后解包到其他的仓库中。

⭐git 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 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]
          [--filter=<filter>] [--] <repository>
          [<directory>]
    
  • 描述

    将存储库克隆到新创建的目录中,为克隆存储库中的每个分支创建远程跟踪分支(使用git branch --remotes可见),并创建、签出从克隆存储库当前活动的分支派生的初始分支。

    -b <名称>:不将新创建的 HEAD 指向克隆存储库的 HEAD 所指向的分支,而是指向<名称>``分支。在非裸仓库中,这是将被检出的分支。 --branch`` 也可以使用标签并在生成的存储库中的提交时分离 HEAD。

⭐git commit

记录仓库的变更

  • 概述

    git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
           [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<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]]
           [(--trailer <token>[(=|:)<value>])…​] [-S[<keyid>]]
           [--] [<pathspec>…​]
    
  • 描述

    修改仓库中的内容使用此命令,将更改提交,一般附加-m(msg)参数来解释提交。

git describe

查找提交中可到达的最近的标记

  • 概述

    git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>…​]
    git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]
    git describe <blob>
    
  • 描述

    如果标签指向提交,则只显示标签。 否则,它会在标记名称的后缀加上标记对象顶部的附加提交数量和最近提交的缩写对象名称。 结果是一个易读的对象名称,它也可用于识别对其他 git 命令的提交。

git diff

differences。显示提交之间或提交与当前的更改。

  • 概述

    git diff [<options>] [<commit>] [--] [<path>…​]
    git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>…​]
    git diff [<options>] [--merge-base] <commit> [<commit>…​] <commit> [--] [<path>…​]
    git diff [<options>] <commit>…​<commit> [--] [<path>…​]
    git diff [<options>] <blob> <blob>
    git diff [<options>] --no-index [--] <path> <path>
    
  • 描述

    显示提交之间或提交与当前的更改。当前工作内容与任一提交、索引与任一提交、提交之间的、合并结果、分支之间、文件系统下的不同文件。

⭐git fetch

从仓库获取内容

  • 概述

    git fetch [<options>] [<repository> [<refspec>…​]]
    git fetch [<options>] <group>
    git fetch --multiple [<options>] [(<repository> | <group>)…​]
    git fetch --all [<options>]
    
  • 描述

    从一个或者多个仓库获取分支或者tags标签中的内容。

    git fetch 可以从单个命名存储库或 URL 中获取,或者如果给出 并且配置文件中有 remotes. 条目,则可以一次从多个存储库中获取。

⭐git init

初始化 Git 仓库

  • 概述

    git init [-q | --quiet] [--bare] [--template=<template_directory>]
          [--separate-git-dir <git dir>] [--object-format=<format>]
          [-b <branch-name> | --initial-branch=<branch-name>]
          [--shared[=<permissions>]] [directory]
    
  • 描述

    该命令创建一个空的 Git 存储库 - 本质上是一个 .git 目录,其中包含 objectsrefs/headsrefs/tags和模板文件的子目录。还将创建一个引用master分支 HEAD 的初始 HEAD 文件。

    If the $GIT_DIR environment variable is set then it specifies a path to use instead of ./.git for the base of the repository.

    如果通过 $GIT_OBJECT_DIRECTORY 环境变量指定了对象存储目录,那么将在该目录下创建 sha1 目录,否则将使用默认的 $GIT_DIR/objects 目录。

⭐git 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>]
        [--into-name <branch>] [<commit>…​]
    git merge (--continue | --abort | --quit)
    
  • 描述

    合并提交中的更改,执行git pull时会被使用。

⭐git pull

更新内容

  • 概述

    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>]] [--force-if-includes]]
           [--no-verify] [<repository> [<refspec>…​]]
    
  • 描述

    拉取仓库中的更新,并合并本地。

git rebase

变基。实际开发还是倾向于使用git merge

  • 概述

    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)
    
  • 描述

    合并当前分支与所选分支,并将此时提交作为新的基点。

⭐git 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>]
    
  • 描述

    重置HEAD,达到跳转版本的目的。

    git reset --hard <commitId>
    

⭐git status

显示工作树状态

  • 概述

    git status [<options>…​] [--] [<pathspec>…​]
    
  • 描述

    显示索引文件和当前HEAD提交有差异的路径,工作树和索引文件有差异的路径,以及工作树中不被Git追踪的路径(也不被gitignore[5]忽略)。前者是你通过运行 "git commit "会提交的东西;第二和第三者是你在运行 "git commit "之前通过运行 "git add "可以提交的东西。

git switch

切换分支

  • 概述

    git switch [<options>] [--no-guess] <branch>
    git switch [<options>] --detach [<start-point>]
    git switch [<options>] (-c|-C) <new-branch> [<start-point>]
    git switch [<options>] --orphan <new-branch>
    
  • 描述

    切换到指定分支。

总结

由于英语水平有限,官方的文档有些地方即使凭借翻译,理解起来也较为困难,部分内容来源于实践+猜测,不确保是否正确(欢迎指正,会跟进文章内容)。阅读了官方的文档,才知道Git如此强大,日常用到的只不过冰山一角。

posted @ 2022-09-15 09:33  Flipped1121  阅读(22)  评论(0编辑  收藏  举报