转载 git 用法大全

原文地址

Git 是一套内容寻址文件系统

在项目目录创建和配置.gitignore文件,可以忽略指定文件

Git

Git工作流一般约定:编辑、缓存、提交、推送/拉取

  • Workspace:工作区(编辑)
  • Index / Stage:暂存区(缓存)
  • Repository:仓库区(本地仓库Local Repo)(提交)
  • Remote:远程仓库(推送/拉取)

git 操作

常见工作流

  • 中心化的工作流
  • 基于功能分支的工作流
  • Gitflow工作流
  • Fork工作流

详情

基于Gitflow工作流的分支管理策略

正式发布: 主分支master

负责记录上线版本的迭代,该分支代码与线上代码是完全一致的。

日常开发: 开发分支develop

该分支记录相对稳定的版本,所有的feature分支和bugfix分支都从该分支创建。

正式对外发布,在master分支上对develop分支进行merge

特定目的: 临时性分支

  • 功能feature分支
  • 预发布release分支
  • 不紧急bug修复bugfix分支
  • 紧急bug修复hotfix分支

临时性分支在开发完成后都要删除

功能feature分支

develop分支创建,开发某种特定功能,开发完成后并入develop

feature-分支名称形式命名。

预发布release分支

develop分支创建,用于代码上线准备,进行整理提交、充分测试、更新文档,发现bug在该release分支上进行bug修复,上线前合并到master分支和develop分支,为提交打上标签。

release-分支名称形式命名。

修补bugbugfix分支

master分支创建,用于修复不紧急普通bug,开发完成自测后合并到develop分支,下一个发布周期修复。

bugfix-分支名称形式命名。

紧急bug修复hotfix分支

master分支创建,紧急情况下使用,用于紧急修复线上bug,开发和测试完成后合并到master分支和develop分支。

hotfix-分支名称形式命名。

GitFlow 工作流

Git操作

初始项目,init

# 将一个目录初始化为Git代码库,加`--bare`将创建裸仓库无工作目录
$ git init <project-name.git> [--bare]

# 下载远程仓库代码到本地仓库,加`-o`指定仓库别名
$ git clone <repository> <local>?

# `clone`之后会默认添加与远程仓库的`remote`追踪关系

远程仓库,remote

便于管理,每个远程仓库都有一个对应的别名

# 查看远程库
$ git remote
$ git remote -v
$ git remote show <remote-name>

# 添加新的远程仓库连接,指定`shortname`,如`upstream`,`origin`
$ git remote add <remote-name> <repository url>

# 移除远程仓库连接
$ git remote rm <remote-name>

# 重命名远程仓库连接
$ git remote rename <old-name> <new-name>

# 如果是在Github上`Fork`的工作流,有官方中央仓库`upstream`、开发者个人服务端仓库`origin`两个远程连接,
$ git remote add upstream <repository>
# 同步官方仓库
$ git pull upstream <repository>

# 如果是要推到多个仓库上
git remote set-url --add <remote-name> <repository url>

拉取,fetch,pull

# `git pull`,从最初`克隆的服务器`上抓取数据并自动尝试合并到当前所在的分支,等同于先fetch再merge操作
# `--reabse`线性获取代码,如预期冲突比较多,直接`pull`
$ git pull

# 允许合并不相关历史的内容,如果有冲突会自动合并,合并失败,会提示解决冲突,然后`git add`,交互式合并界面esc,`:wq!`,然后`git commit`
$ git pull origin master --allow-unrelated-histories

# `git fetch` 命令会将数据拉取到你的`本地仓库`,不会自动合并或修改当前的`工作目录`,须手动将其合并入`工作目录`
# `--prune`远程分支删除,则本地分支亦删除,`--dry-run`预览要删除的已经删除的分支
$ git fetch --prune --dry-run <remote-name> <branch-name>?
$ git merge <remote-name> <branch-name>

# 更新时,远程分支删除,则本地分支亦删除
$ git pull -p
# 等同于
$ git fetch --prune <remote-name>

Git配置,config

# 显示当前的Git配置
$ git config --list

# 编辑Git配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "<name>"
$ git config [--global] user.email "<email>"

增加/删除文件,add,rm,clean

# 添加指定文件到暂存区
$ git add <file1> [<file2>] ...

# 添加指定目录到暂存区,包括子目录
$ git add <dir>

# 提交新文件(new)和被修改(modified)文件,Git Version 1.x以下不包括被删除(deleted)文件
$ git add .

# 仅监控`add`过的`tracked file`,不含`untracked file`,即:提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
$ git add -u

# 粗暴添加到暂存区
$ git add -A/--all

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 本要添加提交两个文件,提交了A文件,忘记了添加了B文件
$ git add <file-b>
$ git commit --amend --no-edit

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm <file1> <file2> ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached <file>

# 改名文件,并且将这个改名放入暂存区
$ git mv <file-from> <file-to>

# 将未跟踪的文件从工作目录中移除,操作无法撤消,
# `-d`未追踪的文件以及空的子目录, `-f`移除强制操作
$ git clean

# 演习,将要移除什么
$ git clean -d -n

提交代码,commit

# 提交暂存区到仓库区
$ git commit -m <"commit-message">

# 提交暂存区的指定文件到仓库区
$ git commit <file1> <file2> ... -m <"commit-message">

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m <"commit-message">

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend <file1> <file2> ...

推送到远程仓库,push

# 加`--force`强制推送(不建议)
$ git push <remote-name> <branch-name>

# 初次 push 加`-u`指定默认推送到远程主机的分支,以后可不加 `<remote-name>` 参数使用`push`
$ git push -u <remote-name> <branch-name>

# 没指定推送分支:
# `simple` 模式(默认),仅推送当前分支
# `matching`模式,推送所有本地分支
$ git config --global push.default [simple]|[matching]

# 将本地所有分支都推送到远程主机
$ git push --all <remote-name>

分支与合并,branch,merge

默认情况下,Git执行"快进式合并"(fast-farward merge),会直接将master分支指向develop分支。

使用--no-ff参数后,会执行正常合并,在master分支上生成一个新节点。保证版本演进的清晰。

在切换分支之前,保持好一个干净的状态,可以保存进度(stashing) 和 修补提交(commit amending)。

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建一个分支,但依然停留在当前分支
$ git branch <branch-name>

# 新建一个分支,并切换到该分支
$ git checkout -b <branch-name>

# 新建一个分支,指向指定commit
$ git branch <branch-name> <commit-id>

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track <branch-name> <remote-branch-name>

# 切换到指定分支,并更新工作区
$ git checkout <branch-name>

# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream-to=<remote-name>/<branch-name>

# 合并指定分支到当前分支
$ git merge <branch-name>

# 选择一个commit,合并进当前分支
$ git cherry-pick <commit-id>

# 删除分支
$ git branch -d <branch-name>

# 删除远程分支
$ git push <remote-name> :<branch-name>
$ git push <remote-name> --delete <branch-name>
$ git branch -dr <remote-name/branch-name>
# 开发分支
$ git checkout -b develop master
$ git checkout master
$ git merge --no-ff develop

# 功能分支
$ git checkout -b <feature-name> develop
$ git checkout develop
$ git merge --no-ff <feature-name>
$ git branch -d <feature-name>

# 预发布分支
$ git checkout -b <release-version> develop
$ git checkout master
$ git merge --no-ff <release-version>
$ git tag -a <version>
$ git checkout develop
$ git merge --no-ff <release-version>
$ git branch -d <release-version>

# 修复不紧急bug
$ git checkout -b <bugfix-version> master
$ git checkout develop
$ git merge --no-ff <bugfix-version>
$ git branch -d <bugfix-version>

# 修复紧急bug
$ git checkout -b <hotfix-version> master
$ git checkout master
$ git merge --no-ff <hotfix-version>
$ git tag -a <new version>
$ git checkout develop
$ git merge --no-ff <hotfix-version>
$ git branch -d <hotfix-version>

合并 commit

# 1.交互式(interactive)合并
$ git checkout <topic-branch-name>
$ git rebase -i <base-branch-name>

# Commands:
#  p, pick = use commit(正常选中)
#  r, reword = use commit, but edit the commit message(选中,并且修改提交信息)
#  e, edit = use commit, but stop for amending(选中,rebase时会暂停,允许你修改这个commit)
#  s, squash = use commit, but meld into previous commit(选中,会将当前commit与上一个commit合并)
#  f, fixup = like "squash", but discard this commit's log message(与squash相同,但不会保存当前commit的提交信息)
#  x, exec = run command (the rest of the line) using shell(执行其他shell命令)

# 2.Pony Foo提出另外一种合并commit的简便方法:
# 先撤销过去5个commit,然后再建一个新commit。
$ git reset HEAD~5
$ git add .
$ git commit -a -m "Here's the bug fix that closes #28"
$ git push --force

撤销操作,reset,checkout,revert

git resetgit checkout的参数决定了它们的作用域,没有包含文件路径对commit操作,包含文件路径仅对指定文件操作。

git revert 没有文件层面的操作。

  • 提交层面的撤销操作

    • git reset
      git reset 将一个分支的HEAD指针指向另一个提交,移除当前分支的一些提交
      git reset 操作的作用域:
      --soft – 缓存区和工作目录都不会被改变
      --mixed – 默认选项。缓存区和你指定的提交同步,但工作目录不受影响
      --hard – 缓存区和工作目录都同步到你指定的提交

      ```
      # 按指令撤销提交至指定 commit
      $ git reset --hard HEAD~n/<commit-id>
      ```
      
    • git checkout
      会造成HEAD指针脱离原分支,跳转到当前checkout的 commit 位置的匿名分支!,可以基于此创建一个分支。

      git checkout HEAD~n/<commit-id>
      
    • git revert
      撤销一个提交的同时会创建一个新的提交,不会重写提交历史。

  • 文件层面的操作

    • git reset

      # 从`本地仓库`更改`暂存区`文件
      $ git reset HEAD~n/<commit-id> <file-name>
      
    • git checkout

      # 从`本地仓库`更改`工作目录`文件
      $ git checkout HEAD~n/<commit-id> <file-name>
      
      # 从`暂存区`更改`工作目录`文件
      $ git checkout -- <file-name>
      

查看信息,status,log,show

# 显示有变更的文件
$ git status

# 显示当前分支的版本历史,不能察看已经删除了的`commit`记录
# 用`空格键`来滚动,按`q`退出
$ git log --oneline
$ git log --graph --pretty=oneline
$ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date="short"
$ git log --pretty=format:"%h -%an,%ar : %s" -3


# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat

# 搜索提交历史,根据关键词
$ git log -S <keyword>

# 显示某个commit之后的所有变动,每个commit占据一行
$ git log <tag> HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
$ git log <tag> HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
$ git log --follow <file>
$ git whatchanged <file>

# 显示指定文件相关的每一次diff
$ git log -p <file>

# 显示过去5次提交
$ git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 搜索特定作者的提交
$ git log --author="<pattern>"

# 显示指定文件是什么人在什么时间修改过
$ git blame <file>

# 显示某次提交的元数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only <commit-id>

# 显示某次提交时,某个文件的内容
$ git show <commit-id>:<file>

# 查看所有分支的所有操作记录,包括被删除的`commit`记录
$ git reflog

# 查看文件列表
$ git ls-files

差异,diff

# 显示`工作区`与`暂存区`的差异
$ git diff

# 显示`暂存区`与`本地仓`的差异
$ git diff --cached <file>

# 显示`工作区`与`本地仓`的差异
$ git diff HEAD

# 显示当前分支和指定分支的差异
$ git diff <branch-name>

# 显示两次提交之间的差异
$ git diff <first-commit> <second-commit>

# 显示两个分支之间的差异
$ git diff <first-branch> <second-branch>

# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"

工作现场,stash

# 手头工作没有完成,但是有bug要修复,先保存一个工作现场;`--include-untracked`/`-u`含未跟踪文件
$ git stash [[--include-untracked]|[-u]]

# bug分支创建、修复完成、`--no-ff`模式合并、并删除,
# 回到开发分支,查看工作现场列表,选择进行恢复。
# `apply`只恢复,`pop`恢复并删除,`drop`删除指定`stash`
$ git stash list
$ git stash apply/pop


标签,tag

# 列出所有tag
$ git tag

# 新建tag在当前commit
$ git tag <tag-name>

# 新建tag
$ git tag -a <tag-name> -m <tag-message> <commit-id>|<remote-name>

# 新建GPG签名tag/验证tag
$ git tag -s <tag-name> -m <tag-message> <commit-id>/git tag -v <tag-name>

# 删除本地tag
$ git tag -d <tag-name>

# 删除远程tag
$ git push <remote-name> :refs/tags/<tag-name>

# 查看tag信息
$ git show <tag-name>

# 提交指定tag
$ git push <remote-name> <tag-name>

# 提交所有tag,`git push`不会推送标签(tag)
$ git push [<remote-name>] --tags

# 新建一个分支,指向某个tag
$ git checkout -b <branch-name> <tag-name>

多人协作

  • 试图用git push <remote-name> <branch-name>推送自己的修改
  • 如果推送失败,远程分支比本地更新,需要git pull试图合并
  • 如果合并有冲突,则解决冲突,并再本地提交
  • 没有冲突或者解决掉冲突后,再用git push <remote-name> <branch-name>推送

变基,rebase

整合来自不同分支的修改主要有两种方法:合并merge 以及 变基rebase

merge进行三方合并,生成一个新的快照并提交,分支不会被更改,整个提交历史是分叉并行的;

rebase将提交到当前分支上的所有修改都移至另一分支上。首先找到这两个分支最近共同祖先,然后对比当前分支相对于该祖先的历次提交,提取相应的修改并存为临时文件,然后将当前分支指向目标分支基底(最后一次提交),最后以此将之前另存为临时文件的修改依序应用,然后checkout到目标分支再merge该变基分支。变基使得提交历史更加整洁,整个提交历史是线性串行的。

变基是将一系列提交按照原有次序依次应用到另一分支上,而合并是把最终结果合在一起。

应用场景,先将fork的代码变基到 [remote-name]/[branch-name] 上,然后再向主项目提交修改,该项目的维护者就不再需要进行整合工作,只需要快进合并便可。

不要在公共的分支上使用rebase,别人正在这个分支上工作。

图解 Git 命令

详情

基本用法直接从 commit历史取出文件、直接提交代码约定: 橙色分支、绿色提交、HEAD当前分支


git diffgit commit分支上的git commit更改一次提交


git checkout filegit checkout branchgit checkout commitHEAD 标识处于匿名分支分离状态


git reset HEADgit reset file


git merge fast-forward模式快速向前合并,仅移动HEAD指针并生成一个新的提交git merge 三路合并(两个分支顶端和它们共同的祖先),会出现提交冲突,复杂功能和多个开发者同时工作时常见的情形git cherry-pick,复制提交节点做一次新提交git rebase master,以master为基变动git rebase --onto 限制回滚范围


git cli

git remote

usage: git remote [-v | --verbose]
   or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
   or: git remote rename <old> <new>
   or: git remote remove <name>
   or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
   or: git remote [-v | --verbose] show [-n] <name>
   or: git remote prune [-n | --dry-run] <name>
   or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
   or: git remote set-branches [--add] <name> <branch>...
   or: git remote get-url [--push] [--all] <name>
   or: git remote set-url [--push] <name> <newurl> [<oldurl>]
   or: git remote set-url --add <name> <newurl>
   or: git remote set-url --delete <name> <url>

    -v, --verbose         be verbose; must be placed before a subcommand

git init

usage: git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]

    --template <template-directory>
                          directory from which templates will be used
    --bare                create a bare repository
    --shared[=<permissions>]
                          specify that the git repository is to be shared amongst several users
    -q, --quiet           be quiet
    --separate-git-dir <gitdir>
                          separate git dir from working tree

git clone

$ git 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

git pull

$ git pull [<远程主机名> | <远程分支名>:<本地分支名>]

等同于先git fetch,再git merge

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

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --progress            force progress reporting
    --recurse-submodules[=<on-demand>]
                          control for recursive fetching of submodules

Options related to merging
    -r, --rebase[=<false|true|preserve|interactive>]
                          incorporate changes by rebasing rather than merging
    -n                    do not show a diffstat at the end of the merge
    --stat                show a diffstat at the end of the merge
    --log[=<n>]           add (at most <n>) entries from shortlog to merge commit message
    --squash              create a single commit instead of doing a merge
    --commit              perform a commit if the merge succeeds (default)
    --edit                edit message before committing
    --ff                  allow fast-forward
    --ff-only             abort if fast-forward is not possible
    --verify-signatures   verify that the named commit has a valid GPG signature
    --autostash           automatically stash/stash pop before and after rebase
    -s, --strategy <strategy>
                          merge strategy to use
    -X, --strategy-option <option=value>
                          option for selected merge strategy
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit
    --allow-unrelated-histories
                          allow merging unrelated histories

Options related to fetching
    --all                 fetch from all remotes
    -a, --append          append to .git/FETCH_HEAD instead of overwriting
    --upload-pack <path>  path to upload pack on remote end
    -f, --force           force overwrite of local branch
    -t, --tags            fetch all tags and associated objects
    -p, --prune           prune remote-tracking branches no longer on remote
    -j, --jobs[=<n>]      number of submodules pulled in parallel
    --dry-run             dry run
    -k, --keep            keep downloaded pack
    --depth <depth>       deepen history of shallow clone
    --unshallow           convert to a complete repository
    --update-shallow      accept refs that update .git/shallow
    --refmap <refmap>     specify fetch refmap

git fetch

$ git fetch [<远程主机名> | <分支名>]

  • 创建并更新所有远程仓库分支到本地仓库分支,对本地开发代码无影响
  • 设定当前分支的FETCH_HEAD为远程服务器的master分支
  • fetch自动获取remote 新加入的分支
usage: git fetch [<options>] [<repository> [<refspec>...]]
   or: git fetch [<options>] <group>
   or: git fetch --multiple [<options>] [(<repository> | <group>)...]
   or: git fetch --all [<options>]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --all                 fetch from all remotes
    -a, --append          append to .git/FETCH_HEAD instead of overwriting
    --upload-pack <path>  path to upload pack on remote end
    -f, --force           force overwrite of local branch
    -m, --multiple        fetch from multiple remotes
    -t, --tags            fetch all tags and associated objects
    -n                    do not fetch all tags (--no-tags)
    -j, --jobs <n>        number of submodules fetched in parallel
    -p, --prune           prune remote-tracking branches no longer on remote
    --recurse-submodules[=<on-demand>]
                          control recursive fetching of submodules
    --dry-run             dry run
    -k, --keep            keep downloaded pack
    -u, --update-head-ok  allow updating of HEAD ref
    --progress            force progress reporting
    --depth <depth>       deepen history of shallow clone
    --shallow-since <time>
                          deepen history of shallow repository based on time
    --shallow-exclude <revision>
                          deepen history of shallow clone, excluding rev
    --deepen <n>          deepen history of shallow clone
    --unshallow           convert to a complete repository
    --update-shallow      accept refs that update .git/shallow
    --refmap <refmap>     specify fetch refmap
    -4, --ipv4            use IPv4 addresses only
    -6, --ipv6            use IPv6 addresses only

git push

$ git push <远程主机名> <本地分支名>:<远程分支名>

usage: git push [<options>] [<repository> [<refspec>...]]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --repo <repository>   repository
    --all                 push all refs
    --mirror              mirror all refs
    -d, --delete          delete refs
    --tags                push tags (can't be used with --all or --mirror)
    -n, --dry-run         dry run
    --porcelain           machine-readable output
    -f, --force           force updates
    --force-with-lease[=<refname>:<expect>]
                          require old value of ref to be at this value
    --recurse-submodules[=<check|on-demand|no>]
                          control recursive pushing of submodules
    --thin                use thin pack
    --receive-pack <receive-pack>
                          receive pack program
    --exec <receive-pack>
                          receive pack program
    -u, --set-upstream    set upstream for git pull/status
    --progress            force progress reporting
    --prune               prune locally removed refs
    --no-verify           bypass pre-push hook
    --follow-tags         push missing but relevant tags
    --signed[=<yes|no|if-asked>]
                          GPG sign the push
    --atomic              request atomic transaction on remote side
    -o, --push-option <server-specific>
                          option to transmit
    -4, --ipv4            use IPv4 addresses only
    -6, --ipv6            use IPv6 addresses only

git log

usage: git log [<options>] [<revision-range>] [[--] <path>...]
   or: git show [<options>] <object>...

    -q, --quiet           suppress diff output
    --source              show source
    --use-mailmap         Use mail map file
    --decorate[=...]      decorate options
    -L <n,m:file>         Process line range n,m in file, counting from 1

git add

usage: git add [<options>] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
    --chmod <(+/-)x>      override the executable bit of the listed files

git commit

usage: git commit [<options>] [--] <pathspec>...

    -q, --quiet           suppress summary after successful commit
    -v, --verbose         show diff in commit message template

Commit message options
    -F, --file <file>     read message from file
    --author <author>     override author for commit
    --date <date>         override date for commit
    -m, --message <message>
                          commit message
    -c, --reedit-message <commit>
                          reuse and edit message from specified commit
    -C, --reuse-message <commit>
                          reuse message from specified commit
    --fixup <commit>      use autosquash formatted message to fixup specified commit
    --squash <commit>     use autosquash formatted message to squash specified commit
    --reset-author        the commit is authored by me now (used with -C/-c/--amend)
    -s, --signoff         add Signed-off-by:
    -t, --template <file>
                          use specified template file
    -e, --edit            force edit of commit
    --cleanup <default>   how to strip spaces and #comments from message
    --status              include status in commit message template
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit

Commit contents options
    -a, --all             commit all changed files
    -i, --include         add specified files to index for commit
    --interactive         interactively add files
    -p, --patch           interactively add changes
    -o, --only            commit only specified files
    -n, --no-verify       bypass pre-commit and commit-msg hooks
    --dry-run             show what would be committed
    --short               show status concisely
    --branch              show branch information
    --porcelain           machine-readable output
    --long                show status in long format (default)
    -z, --null            terminate entries with NUL
    --amend               amend previous commit
    --no-post-rewrite     bypass post-rewrite hook
    -u, --untracked-files[=<mode>]
                          show untracked files, optional modes: all, normal, no. (Default: all)

git cherry-pick

usage: git cherry-pick [<options>] <commit-ish>...
   or: git cherry-pick <subcommand>

    --quit                end revert or cherry-pick sequence
    --continue            resume revert or cherry-pick sequence
    --abort               cancel revert or cherry-pick sequence
    -n, --no-commit       don't automatically commit
    -e, --edit            edit the commit message
    -s, --signoff         add Signed-off-by:
    -m, --mainline <parent-number>
                          select mainline parent
    --rerere-autoupdate   update the index with reused conflict resolution if possible
    --strategy <strategy>
                          merge strategy
    -X, --strategy-option <option>
                          option for merge strategy
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit
    -x                    append commit name
    --ff                  allow fast-forward
    --allow-empty         preserve initially empty commits
    --allow-empty-message
                          allow commits with empty messages
    --keep-redundant-commits
                          keep redundant, empty commits

git merge

usage: git merge [<options>] [<commit>...]
   or: git merge --abort
   or: git merge --continue

    -n                    do not show a diffstat at the end of the merge
    --stat                show a diffstat at the end of the merge
    --summary             (synonym to --stat)
    --log[=<n>]           add (at most <n>) entries from shortlog to merge commit message
    --squash              create a single commit instead of doing a merge
    --commit              perform a commit if the merge succeeds (default)
    -e, --edit            edit message before committing
    --ff                  allow fast-forward (default)
    --ff-only             abort if fast-forward is not possible
    --rerere-autoupdate   update the index with reused conflict resolution if possible
    --verify-signatures   verify that the named commit has a valid GPG signature
    -s, --strategy <strategy>
                          merge strategy to use
    -X, --strategy-option <option=value>
                          option for selected merge strategy
    -m, --message <message>
                          merge commit message (for a non-fast-forward merge)
    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --abort               abort the current in-progress merge
    --continue            continue the current in-progress merge
    --allow-unrelated-histories
                          allow merging unrelated histories
    --progress            force progress reporting
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit
    --overwrite-ignore    update ignored files (default)

git rebase

usage: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
   or: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
   or: git-rebase --continue | --abort | --skip | --edit-todo

Available options are
    -v, --verbose         display a diffstat of what changed upstream
    -q, --quiet           be quiet. implies --no-stat
    --autostash           automatically stash/stash pop before and after
    --fork-point          use 'merge-base --fork-point' to refine upstream
    --onto ...            rebase onto given branch instead of upstream
    -p, --preserve-merges
                          try to recreate merges instead of ignoring them
    -s, --strategy ...    use the given merge strategy
    --no-ff               cherry-pick all commits, even if unchanged
    -m, --merge           use merging strategies to rebase
    -i, --interactive     let the user edit the list of commits to rebase
    -x, --exec ...        add exec lines after each commit of the editable list
    -k, --keep-empty         preserve empty commits during rebase
    -f, --force-rebase    force rebase even if branch is up to date
    -X, --strategy-option ...
                          pass the argument through to the merge strategy
    --stat                display a diffstat of what changed upstream
    -n, --no-stat         do not show diffstat of what changed upstream
    --verify              allow pre-rebase hook to run
    --rerere-autoupdate   allow rerere to update index with resolved conflicts
    --root                rebase all reachable commits up to the root(s)
    --autosquash         move commits that begin with squash
                          move commits that begin with squash!/fixup! under -i
    --committer-date-is-author-date
                          passed to 'git am'
    --ignore-date         passed to 'git am'
    --signoff             passed to 'git am'
    --whitespace ...      passed to 'git apply'
    --ignore-whitespace   passed to 'git apply'
    -C ...                passed to 'git apply'
    -S, --gpg-sign[=...]  GPG-sign commits

Actions:
    --continue            continue
    --abort               abort and check out the original branch
    --skip                skip current patch and continue
    --edit-todo           edit the todo list during an interactive rebase
    --quit                abort but keep HEAD where it is

git reset

usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] [<tree-ish>] [--] <paths>...
   or: EXPERIMENTAL: git reset [-q] [--stdin [-z]] [<tree-ish>]
   or: git reset --patch [<tree-ish>] [--] [<paths>...]

    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    --recurse-submodules[=<reset>]
                          control recursive updating of submodules
    -p, --patch           select hunks interactively
    -N, --intent-to-add   record only the fact that removed paths will be added later
    -z                    EXPERIMENTAL: paths are separated with NUL character
    --stdin               EXPERIMENTAL: read paths from <stdin>

git checkout

usage: git checkout [<options>] <branch>
   or: git checkout [<options>] [<branch>] -- <file>...

    -q, --quiet           suppress progress reporting
    -b <branch>           create and checkout a new branch
    -B <branch>           create/reset and checkout a branch
    -l                    create reflog for new branch
    --detach              detach HEAD at named commit
    -t, --track           set upstream info for new branch
    --orphan <new-branch>
                          new unparented branch
    -2, --ours            checkout our version for unmerged files
    -3, --theirs          checkout their version for unmerged files
    -f, --force           force checkout (throw away local modifications)
    -m, --merge           perform a 3-way merge with the new branch
    --overwrite-ignore    update ignored files (default)
    --conflict <style>    conflict style (merge or diff3)
    -p, --patch           select hunks interactively
    --ignore-skip-worktree-bits
                          do not limit pathspecs to sparse entries only
    --ignore-other-worktrees
                          do not check if another worktree is holding the given ref
    --recurse-submodules[=<checkout>]
                          control recursive updating of submodules
    --progress            force progress reporting

git revert

usage: git revert [<options>] <commit-ish>...
   or: git revert <subcommand>

    --quit                end revert or cherry-pick sequence
    --continue            resume revert or cherry-pick sequence
    --abort               cancel revert or cherry-pick sequence
    -n, --no-commit       don't automatically commit
    -e, --edit            edit the commit message
    -s, --signoff         add Signed-off-by:
    -m, --mainline <parent-number>
                          select mainline parent
    --rerere-autoupdate   update the index with reused conflict resolution if possible
    --strategy <strategy>
                          merge strategy
    -X, --strategy-option <option>
                          option for merge strategy
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit

git stash

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

git branch

usage: git branch [<options>] [-r | -a] [--merged | --no-merged]
   or: git branch [<options>] [-l] [-f] <branch-name> [<start-point>]
   or: git branch [<options>] [-r] (-d | -D) <branch-name>...
   or: git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
   or: git branch [<options>] [-r | -a] [--points-at]
   or: git branch [<options>] [-r | -a] [--format]

Generic options
    -v, --verbose         show hash and subject, give twice for upstream branch
    -q, --quiet           suppress informational messages
    -t, --track           set up tracking mode (see git-pull(1))
    --set-upstream        change upstream info
    -u, --set-upstream-to <upstream>
                          change the upstream info
    --unset-upstream      Unset the upstream info
    --color[=<when>]      use colored output
    -r, --remotes         act on remote-tracking branches
    --contains <commit>   print only branches that contain the commit
    --no-contains <commit>
                          print only branches that don't contain the commit
    --abbrev[=<n>]        use <n> digits to display SHA-1s

Specific git-branch actions:
    -a, --all             list both remote-tracking and local branches
    -d, --delete          delete fully merged branch
    -D                    delete branch (even if not merged)
    -m, --move            move/rename a branch and its reflog
    -M                    move/rename a branch, even if target exists
    --list                list branch names
    -l, --create-reflog   create the branch's reflog
    --edit-description    edit the description for the branch
    -f, --force           force creation, move/rename, deletion
    --merged <commit>     print only branches that are merged
    --no-merged <commit>  print only branches that are not merged
    --column[=<style>]    list branches in columns
    --sort <key>          field name to sort on
    --points-at <object>  print only branches of the object
    -i, --ignore-case     sorting and filtering are case insensitive
    --format <format>     format to use for the output

git stage

usage: git add [<options>] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
    --chmod <(+/-)x>      override the executable bit of the listed files

git mv

usage: git mv [<options>] <source>... <destination>

    -v, --verbose         be verbose
    -n, --dry-run         dry run
    -f, --force           force move/rename even if target exists
    -k                    skip move/rename errors

git rm

usage: git rm [<options>] [--] <file>...

    -n, --dry-run         dry run
    -q, --quiet           do not list removed files
    --cached              only remove from the index
    -f, --force           override the up-to-date check
    -r                    allow recursive removal
    --ignore-unmatch      exit with a zero status even if nothing matched

git clean

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

    -q, --quiet           do not print names of files removed
    -n, --dry-run         dry run
    -f, --force           force
    -i, --interactive     interactive cleaning
    -d                    remove whole directories
    -e, --exclude <pattern>
                          add <pattern> to ignore rules
    -x                    remove ignored files, too
    -X                    remove only ignored files

扩展阅读

附: github 操作,注意git和github的区别,不能把私有代码存放在公共仓库


猜你喜欢

转载自www.cnblogs.com/Frank99/p/9070718.html