一、新建代码库
1.git clone用法:git clone [<选项>] [--] <仓库> [<路径>]
-v, --verbose
-q, --quiet
--progress
-n, --no-checkout
--bare
--mirror
-l, --local
--no-hardlinks
-s, --shared
--recurse-submodules[=<路径规格>]
--recursive ... --recurse-submodules
-j, --jobs <n>
--template <模板目录>
--reference <仓库>
--reference-if-able <仓库>
--dissociate
-o, --origin <名称>
-b, --branch <分支>
-u, --upload-pack <路径>
--depth <深度>
--shallow-since <时间>
--shallow-exclude <版本>
--single-branch
--no-tags
--shallow-submodules
--separate-git-dir <git目录>
-c, --config <key=value>
--server-option <server-specific>
-4 , --ipv4
-6 , --ipv6
--filter <参数>
--remote-submodules
--sparse
$ git clone [url]
$ git clone [url] newname
2.git init 用法:git init [-q | --quiet] [--bare] [--template=] [--shared[=]][]
git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]][<directory>]
# 将使用模板的目录/ directory from which templates will be used
--template <template-directory>
# 创建一个空仓库/create a bare repository
--bare
# 指定 git 存储库将在多个用户之间共享/specify that the git repository is to be shared amongst several users
--shared[=<permissions>]
# 安静/be quiet
-q, --quiet
# 将 git dir 与工作树分开/separate git dir from working tree
--separate-git-dir <gitdir>
# 覆盖初始分支的名称/override the name of the initial branch
-b, --initial-branch <name>
# 指定要使用的哈希算法/specify the hash algorithm to use
--object-format <hash>
# git-init - 创建一个空的 Git 存储库或重新初始化一个现有的存储库
$ git init
# 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹.
$ git init [project-name]
$ git init repo
二、配置
1.git config 用法:git config [<选项>]
#用法:git config [<选项>]
#配置文件位置
--global #使用全局配置文件
--system #使用系统级配置文件
--local #使用仓库级配置文件
--worktree #使用工作区级别的配置文件
-f, --file <文件> #使用指定的配置文件
--blob <数据对象 ID> #从给定的数据对象读取配置
#操作
--get #获取值:name [value-regex]
--get -all #获得所有的值:key [value-regex]
--get -regexp #根据正则表达式获得值:name-regex [value-regex]
--get -urlmatch #获得 URL 取值:section[.var] URL
--replace-all #替换所有匹配的变量:name value [value_regex]
--add #添加一个新的变量:name value
--unset #删除一个变量:name [value-regex]
--unset-all #删除所有匹配项:name [value-regex]
--rename-section #重命名小节:old-name new-name
--remove -section #删除一个小节:name
-l, --list #列出所有
-e, --edit #打开一个编辑器
--get -color #获得配置的颜色:配置 [默认]
--get -colorbool #获得颜色设置:配置 [stdout-is-tty]
#类型
-t, --type <> #取值为该类型
--bool #值是 "true" 或 "false"
--int #值是十进制数
--bool -or -int #值是 --bool or --int
--path #值是一个路径(文件或目录名)
--expiry-date #值是一个到期日期
#其它
-z, --null #终止值是 NUL 字节
--name-only #只显示变量名
--includes #查询时参照 include 指令递归查找
--show-origin #显示配置的来源(文件、标准输入、数据对象,或命令行)
--show-scope #显示配置的作用域(工作区、本地、全局、系统、命令)
--default <取值> #使用 --get 参数,当缺少设置时使用默认值
#Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
# 显示当前的Git配置
$ git config --list
# 编辑Git配置文件
$ git config -e --global
# 设置提交代码时的用户信息
$ git config --global user.name "[name]"
$ git config --global user.email "[email address]"
三、增加/删除文件
1.git add 用法:git add [<选项>] [--] <路径规格>...
-n, --dry-run
-v, --verbose
-i, --interactive
-p, --patch
-e, --edit
-f, --force
-u, --update
--renormalize
-N, --intent-to-add
-A, --all
--ignore-removal
--refresh
--ignore-errors
--ignore-missing
--chmod (+|-)x
--pathspec-from -file <文件>
--pathspec-file-nul
$ git add [file1] [file2] ...
$ git add [dir ]
$ git add .
$ git add -p
2.git rm 用法:git rm [<选项>] [--] <文件>...
-n , --dry-run
-q , --quiet
--cached
-f , --force
-r
--ignore-unmatch
--pathspec-from-file <文件>
--pathspec-file-nul
$ git rm [file1 ] [file2 ] ...
$ git rm --cached [file ]
3.git mv 用法:git mv [<选项>] <源>... <目标>
-v , --verbose
-n , --dry-run
-f , --force
-k
$ git mv [file -original ] [file -renamed ="token punctuation" ]
4.git clean 用法:git clean [-d] [-f] [-i] [-n] [-q] [-e <模式>] [-x | -X] [--] <路径>...
-q , --quiet
-n , --dry-run
-f , --force
-i , --interactive
-d
-e , --exclude <模式>
-x
-X
$ git clean -df
-d 表示同时移除目录,-f 表示force,因为在git的配置文件中,clean.requireForce=true,如果不加-f ,clean将会拒绝执行.
四、代码提交
1.git commit 用法:git commit [<选项>] [--] <路径规格>...
-q, --quiet
-v, --verbose
-F, --file <文件>
--author <作者>
--date <日期>
-m, --message <说明>
-c, --reedit-message <提交>
-C, --reuse-message <提交>
--fixup <提交>
--squash <提交>
--reset-author
-s, --signoff
-t, --template <文件>
-e, --edit
--cleanup <模式>
--status
-S, --gpg-sign[=<key-id>]
-a, --all
-i, --include
--interactive
-p, --patch
-o, --only
-n, --no-verify
--dry-run
--short
--branch
--ahead-behind
--porcelain
--long
-z, --null
--amend
--no-post-rewrite
-u, --untracked-files[=<模式>]
--pathspec-from-file <文件>
--pathspec-file -nul
$ git commit -m [message ]
$ git commit [file1] [file2] ... -m [message ]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message ]
$ git commit --amend [file1] [file2] ...
五、分支
1.git branch 用法:git branch [通用选项] [具体的 git-branch 动作] [--merged | --no-merged]
或:git branch [<选项>] [-l ] [-f ] <分支名> [<起始点>]
或:git branch [<选项>] [-r ] (-d | -D ) <分支名>...
或:git branch [<选项>] (-m | -M ) [<旧分支>] <新分支>
或:git branch [<选项>] (-c | -C ) [<老分支>] <新分支>
或:git branch [<选项>] [-r | -a ] [--points -at ]
或:git branch [<选项>] [-r | -a ] [--format ]
-v , --verbose
-q , --quiet
-t , --track
-u , --set-upstream-to <上游>
--unset-upstream
--color [=<何时>]
-r , --remotes
--contains <提交>
--no-contains <提交>
--abbrev [=<n >]
-a , --all
-d , --delete
-D
-m , --move
-M
-c , --copy
-C
-l , --list
--show-current
--create-reflog
--edit-description
-f , --force
--merged <提交>
--no-merged <提交>
--column [=<风格>]
--sort <key>
--points-at <对象>
-i , --ignore-case
--format <格式>
$ git branch
$ git branch -r
$ git branch -a
$ git branch [branch -name ]
$ git branch [branch ] [commit ]
$ git branch --track [branch ] [remote -branch ]
$ git branch --set-upstream [branch ] [remote -branch ]
git branch --set-upstream-to =origin/develop develop
$ git branch -d [branch -name ]
$ git branch -dr [remote /branch ]
2. git checkout用法:git checkout [<选项>] [<分支>] -- <文件>...
或:git checkout [<选项>] [<分支>] -- <文件>...
-b <分支>
-B <分支>
-l
--guess
--overlay
-q, --quiet
--recurse-submodules[=<checkout>]
control recursive updating of submodules
--progress
-m, --merge
--conflict <风格>
-d, --detach
-t, --track
-f, --force
--orphan <新分支>
--overwrite-ignore
--ignore-other-worktrees
-2 , --ours
-3 , --theirs
-p, --patch
--ignore-skip-worktree-bits
--pathspec-from-file <文件>
--pathspec-file-nul
$ git checkout -b [branch]
$ git checkout [branch-name]
$ git checkout -
$ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
3.git merge 用法: git merge [<选项>] [<提交>...]
或:git merge --abort
或:git merge --continue
-n
--stat
--summary (和 --stat 同义)
--log [=<n>]
--squash
--commit
-e, --edit
--cleanup <模式>
--ff
--ff-only
--rerere-autoupdate
--verify-signatures
-s, --strategy <策略>
-X, --strategy-option <option=value>
-m, --message <说明>
-F, --file <路径>
-v, --verbose
-q , --quiet
--abort
--quit --abort,
--continue
--allow-unrelated-histories
--progress
-S, --gpg-sign[=<key-id>]
GPG
--autostash
--overwrite-ignore
--signoff
--no -verify
$ git merge [branch]
$ git merge [commit-id]
4.git cherry-pick用法:git cherry-pick [<选项>] <提交号>...
或:git cherry-pick <子命令>
--quit
--continue
--abort
--skip
--cleanup <模式>
-n, --no-commit
-e, --edit
-s, --signoff
-m, --mainline <父编号>
--rerere-autoupdate
--strategy <策略>
-X, --strategy-option <选项>
-S, --gpg-sign[=<key-id>]
-x
--ff
--allow-empty
--allow-empty -message
--keep-redundant-commits
$ git cherry-pick [commit]
5.用法:git push [<选项>] [<仓库> [<引用规格>...]]
-v, --verbose
-q , --quiet
--repo <仓库>
--all
--mirror
-d, --delete
--tags
-n, --dry-run
--porcelain
-f, --force
--force-with-lease[=<引用名>:<期望值>]
--recurse-submodules (check|on-demand|no )
--thin
--receive-pack <receive-pack >
--exec <receive-pack >
-u, --set-upstream
--progress
--prune
--no -verify
--follow-tags
--signed[=(yes|no |if -asked)]
--atomic
-o, --push -option <server-specific>
-4 , --ipv4
-6 , --ipv6
六、标签
1.git tag用法
或:git tag -d <标签名>...
或:git tag -l [-n[<数字>]] [--contains <提交>] [--no-contains <提交>] [--points-at <对象>]
[--format=<格式>] [--[no-]merged [<提交>]] [<模式>...]
或:git tag -v [--format=<格式>] <标签名>...
-l, --list
-n[<n>]
-d, --delete
-v, --verify
-a, --annotate
-m, --message <说明>
-F, --file <文件>
-e, --edit
-s, --sign
--cleanup <模式>
-u, --local-user <key-id>
-f, --force
--create-reflog
--column[=<风格>]
--contains <提交>
--no-contains <提交>
--merged <提交>
--no-merged <提交>
--sort <key>
--points-at <对象>
--format <格式>
--color[=<何时>]
-i, --ignore-case
$ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/ [tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
七、查看信息
1.git status用法:git status [<选项>] [--] <路径规格>...
#用法:git status [<选项>] [--] <路径规格>...
-v, --verbose #冗长输出
-s, --short #以简洁的格式显示状态
-b , --branch #显示分支信息
--show-stash #显示贮藏区信息
--ahead-behind #计算完整的领先/落后值
--porcelain [=<版本>] #机器可读的输出
--long #以长格式显示状态(默认)
-z, --null #条目以 NUL 字符结尾
-u, --untracked-files [=<模式>]
#显示未跟踪的文件,“模式”的可选参数:all 、normal 、no。(默认:all )
--ignored [=<模式>] #显示已忽略的文件,可选模式:traditional、matching、no。(默认:traditional)
--ignore-submodules [=<何时>]
#忽略子模组的更改,“何时”的可选参数:all 、dirty、untracked。(默认:all )
--column [=<风格>] #以列的方式显示未跟踪的文件
--no-renames #不检测重命名
-M, --find-renames [=<n>]
#检测重命名,可以设置索引相似度
#查询repo的状态.
git status
git status -s
2.git log用法:git log [<选项>] [<版本范围>] [[--] <路径>...]
用法:git log [<选项>] [<版本范围>] [[--] <路径>...]
或:git show [<选项>] <对象>...
-q, --quiet 不显示差异输出
--source 显示源
--use-mailmap 使用邮件映射文件
--mailmap --use-mailmap 的别名
--decorate-refs <模式>
只修饰与 <模式> 匹配的引用
--decorate-refs-exclude <模式>
不修饰和 <模式> 匹配的引用
--decorate[=...] 修饰选项
-L <n,m:file> 处理文件中第 n 到 m 之间的行,从 1 开始
# 显示当前分支的版本历史
$ git log
# 显示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 reflog
# 显示指定文件是什么人在什么时间修改过
$ git blame [file]
# 显示暂存区和工作区的代码差异
$ git diff
# 显示暂存区和上一个commit的差异
$ git diff --cached [file]
# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD
# 显示两次提交之间的差异
$ git diff [first-branch]...[second-branch]
# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"
# 显示某次提交的元数据和内容变化
$ git show [commit]
# 显示某次提交发生变化的文件
$ git show --name-only [commit]
# 显示某次提交时,某个文件的内容
$ git show [commit]:[filename]
# 显示当前分支的最近几次提交
$ git reflog
HEAD@{0}代表HEAD当前的值,HEAD@{3}代表HEAD在3次变化之前的值.git会将变化记录到HEAD对应的reflog文件中,其路径为.git/logs/HEAD, 分支的reflog文件都放在.git/logs/refs目录下的子目录中
$ git rebase [branch]
3.git rebase -i 用法
或:git rebase [-i] [选项] [--exec <命令>] [--onto <新基线>] --root [<分支>]
或:git rebase --continue | --abort | --skip | --edit-todo
--onto <版本>
--keep-base
--no -verify
-q , --quiet
-v, --verbose
-n, --no -stat
--signoff
--ignore-whitespace
--committer-date-is-author-date
--ignore-date
-C <n>
--whitespace <动作>
-f, --force-rebase
--no -ff
--continue
--skip
--abort
--quit
--edit-todo
--show-current-patch
--apply
-m, --merge
-i, --interactive
--rerere-autoupdate
--empty <{drop,keep,ask}>
--autosquash
-S, --gpg-sign[=<key-id>]
--autostash
-x , --exec <exec >
-r, --rebase-merges[=<模式>]
--fork -point
-s, --strategy <策略>
-X, --strategy-option <选项>
--root
--reschedule-failed-exec
--reapply-cherry-picks
八、远程同步
1.git remote 用法
用法:git remote [-v | --verbose]
或:git remote add [-t <分支>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <名称> <地址>
或:git remote rename <旧名称> <新名称>
或:git remote remove <名称>
或:git remote set-head <名称> (-a | --auto | -d | --delete | <分支>)
或:git remote [-v | --verbose] show [-n] <名称>
或:git remote prune [-n | --dry-run] <名称>
或:git remote [-v | --verbose] update [-p | --prune] [(<组> | <远程>)...]
或:git remote set-branches [--add] <名称> <分支>...
或:git remote get-url [--push] [--all] <名称>
或:git remote set-url [--push] <名称> <新的地址> [<旧的地址>]
或:git remote set-url --add <名称> <新的地址>
或:git remote set-url --delete <名称> <地址>
-v, --verbose 冗长输出;必须置于子命令之前
$ git remote update --更新远程仓储
# 下载远程仓库的所有变动
$ git fetch [remote]
# 显示所有远程仓库
$ git remote -v
# 显示某个远程仓库的信息
$ git remote show [remote]
# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]
2.git pull 用法:git pull [<选项>] [<仓库> [<引用规格>...]]
-v, --verbose
-q, --quiet
--progress
--recurse-submodules[=<on-demand>]
-r, --rebase[=(false|true| merges|preserve| interactive)]
-n
--stat
--log[=<n>]
--signoff[=...]
--squash
--commit
--edit
--cleanup <模式>
--ff
--ff-only
--verify-signatures
--autostash
-s, --strategy <策略>
-X, --strategy-option <option=value>
-S, --gpg-sign[=<key-id>]
--allow-unrelated-histories
--all
-a, --append
--upload-pack <路径>
-f, --force
-t, --tags
-p, --prune
-j, --jobs[=<n>]
--dry-run
-k, --keep
--depth <深度>
--shallow-since <时间>
--shallow-exclude <版本>
--deepen <n>
--unshallow
--update-shallow
--refmap <引用映射>
-o, --server-option <server-specific>
-4 , --ipv4
-6 , --ipv6
--negotiation-tip <版本>
--show-forced-updates
--set-upstream
$ git pull [remote] [branch]
3.git push用法:git push [<选项>] [<仓库> [<引用规格>...]]
-v, --verbose
-q , --quiet
--repo <仓库>
--all
--mirror
-d, --delete
--tags
-n, --dry-run
--porcelain
-f, --force
--force-with-lease[=<引用名>:<期望值>]
--recurse-submodules (check|on-demand|no )
--thin
--receive-pack <receive-pack >
--exec <receive-pack >
-u, --set-upstream
--progress
--prune
--no -verify
--follow-tags
--signed[=(yes|no |if -asked)]
--atomic
-o, --push -option <server-specific>
-4 , --ipv4
-6 , --ipv6
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
$ git push origin develop_dev
$ git push [remote] [tag]
$ git push [remote] --tags
$ git push origin --delete [branch-name]
九、撤销
1.git reset 用法
usage: git reset [
or : git reset [- q] [< tree- ish> ] [
or : git reset [- q] [
or : git reset
- q,
#control recursive updating of submodules
- p,
- N,
#read pathspec from file
# 重置暂存区的指定文件,与上一次commit 保持一致,但工作区不变
$ git reset [file]
# 重置暂存区与工作区,与上一次commit 保持一致
$ git reset
# 重置当前分支的指针为指定commit ,同时重置暂存区,但工作区不变
$ git reset [commit ]
# 重置当前分支的HEAD为指定commit ,同时重置暂存区和工作区,与指定commit 一致
$ git reset
# 重置当前HEAD为指定commit ,但保持暂存区和工作区不变
$ git reset
# 新建一个commit ,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit ]
十、储藏
1.git stash 用法
usage: git stash list [<options>]
or : git stash show [<options>] [<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 clear
or: git stash [push [-p| --patch] [-k|--[no -]keep-index ] [-q|--quiet]
[-u| --include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]]
or: git stash save [-p| --patch] [-k|--[no -]keep-index ] [-q|--quiet]
[-u| --include-untracked] [-a|--all] [<message>]
1.1 stash当前修改,查看储存
$ git stash save "保存一个readme.md文件"
$ git stash list
stash@{0}: On master: 保存一个readme.md文件
stash@{1}: On master: groups.cs
stash@{2}: WIP on master: 6a9a740 添加了一个文件
stash@{3}: WIP on master: 77acf37 初始提交
1.2 重新应用缓存的stash
$ git stash pop
$ git stash apply
$ git stash apply stash@{1 }
1.3 移除stash
#删除stash中的项目,删除上一个,也可指定参数删除指定的一个项目.
$ git stash list
stash@{0 }: On master: groups.cs
stash@{1 }: WIP on master: 6 a9a740 添加了一个文件
stash@{2 }: WIP on master: 77 acf37 初始提交
$ git stash drop stash@{0 }
Dropped stash@{0 } (6 fa4af7aa7fc9f33547b3f62493d3c7c3ba9923f)
#或者使用git stash clear 命令,删除所有缓存的stash
$ git stash clear
1.4 查看指定stash的diff
# 可以使用git stash show命令,后面可以跟着stash名字
$ git stash show
user.cs | 0
1 file changed, 0 insertions(+), 0 deletions(-)
# 在该命令后面添加-p或--patch可以查看特定stash的全部diff
$ git stash show -p
1.5 从stash创建分支
$ git stash branch stash_dev
1.5 暂存未跟踪或忽略的文件
添加到暂存区的修改(staged changes)
Git跟踪的但并未添加到暂存区的修改(unstaged changes)
在工作目录中新的文件(untracked files)
被忽略的文件(ignored files)
git stash push -u -m "全部储藏"
git stash push -a -m "全部储藏"
git stash -a -m "全部储藏"
十一、常见开发场景
1.开发新功能
git checkout -b feature01
#开发完成后,添加所有修改到暂存区
git add .
#提交暂存区文件到本地库
git switch dev/git checkout dev
git merge feature01
git push origin dev
2.解决冲突
git merge feature02
3.新功能开发到一半,需要同步远程库最新代码
git add .
git commit -m [message]
git pull origin dev
4.复制某个commit对应代码到当前分支
git add .
git commit -m [message]
git log feature01
git cherry-pick [commit id ]
5.dev分支Bug修复
git stash
git checkout dev/git checkout dev
git checkout -b bug-fix
git add .
git commit -m “buf fix”
git checkout dev
git merge --no-ff -m “merge bug-fix” bug-fix
git checkout feature01
git stash list
git statsh pop
git merge dev
6.版本回退
git log [branch-name]
git reset --hard [commit-id]
======================================================================
创作不易,本人热衷开源共享 《git常用命令梳理总结》
======================================================================
转载请附上链接:
https://www.cnblogs.com/cndevops/p/14993331.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构