git 命令文档

📖 git 命令文档

0%

配置命令

git config user.name <name>
git config user.email <email>
git config --global user.name <name> # 配置全局用户名
git config --global user.email <email> # 配置全局邮箱

# 颜色标识(默认开启)
git config --global color.ui true
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto

# 配置代理
git config --global --unset http.proxy <proxy>
git config --global --unset http.proxy <proxy>

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 查看配置
git config list

基础命令

# 初始化仓库
git init

# 克隆仓库
git clone <url>

# 添加文件到index
git add <pathspec>

# 删除index中的文件
git rm <pathspec>
# 删除未提交缓存
git rm --cached <pathspec>

# 查看仓库状态
git status

# 查看日志
git log
# 查看分支合并
git log --graph
# 精简查看
git log --graph --pretty=oneline --abbrev-commit

git commit -m <message> # 提交文件, 附带版本说明
git commit --amend -m <message> # 合并上一次提交(用于反复修改)
git commit -am <message> # 将add和commit合为一步

git remote -v # 查看远程仓库地址
git remote add origin <url> # 添加origin分支
git remote rm origin # 删除origin分支

# 重新设置远程仓库地址
git remote set-url origin <git.url>

git pull # 拉 简写形式
git pull origin master # 同步远程仓库更新到master分支

git push # 推 简写形式
git push -u origin master # 推送本地仓库到origin分支

git push -f # 强制推送分支, 会覆盖修改

# 设置当前分支的关联的远程分支
git branch --set-upstream-to=origin/master master

# 新建分支并切换 main
# 如果没有刚初始化项目表现为重命名, 原来的没有提交自动被丢弃了
git switch -c main

进阶命令

# 查看 difference(差异)
git diff # 工作区与暂存区
git diff --cached # 暂存区与版本库

# 回滚到上一版本 HEAD~100 前100个版本 可指定<版本号>
git reset --hard HEAD^
git reset <版本号>

# 查看所有版本 包括回退过的
git reflog

# 查看分支
git branch
git branch <branch> # 创建分支
git branch -d <branch> # 删除分支

# 添加标签
git tag <name>

# 查看标签详情
git show <name>

# 推送标签到远程(检查是否有标签推送权限)
git push origin --tags

# 恢复(删除)工作区更改
git checkout
git checkout <branch> # 切换分支(可以使用tag名称切换)
git checkout -b <branch># 创建并切换分支

# 合并分支
git merge <branch>

# 获取所有远程分支(不更新本地分支,另需merge)
git fetch
# 获取所有原创分支并清除服务器上已删掉的分支
git fetch --prune

# 合并分支但不丢弃分支信息
git merge --no-ff -m "merge with no-ff" <branch>

# 创建临时分支
git stash
git stash list # 查看临时分支
git stash drop # 删除stash
git stash apply # 恢复分支内容
git stash pop # 恢复分支内容并删除stash
git stash apply stash@{0}# 恢复指定的stash

# 复制一个特定的提交到当前分支
git cherry-pick <版本号>

# 切换分支
git checkout 1acd5de
# 保存修改后分支
git switch -c <新分支名>
# 恢复切换前分支
git switch -

# 本地仓库强制替换远程仓库
git push --force --set-upstream origin <branch>

替换分支

# 将master替换成main
git checkout main
git merge -s ours master # git --strategy=ours ours master
git checkout master
git merge main

子模块

# 查看子模块
git submodule

# 初始化
git submodule init

# 添加子模块
git submodule add <项目地址> <路径>
# 添加指定分支
git submodule add -b v1.0 <项目地址> <路径>

# 更新(克隆)子模块
git submodule update --init --remote --recursive

# 删除子模块
git rm -rf --cached <路径>

zsh 快捷命令

gapa    	git add --patch
gc! git commit -v --amend
gcl git clone --recursive
gclean git reset --hard && git clean -dfx
gcm git checkout master
gcmsg git commit -m
gco git checkout
gd git diff
gdca git diff --cached
gp git push
grbc git rebase --continue
gst git status
gup git pull --rebase

分支命名规范

分支 命名 说明
主分支 main/master 主分支,所有提供给用户使用的正式版本,都在这个主分支上发布
开发主分支 dev 开发分支,永远是功能最新最全的分支
功能分支 feature-* 新功能分支,某个功能点正在开发阶段
发布版本 release-* 发布定期要上线的功能
修复发布版本分支 bugfix-release-* 修复测试bug
紧急修复分支 bugfix-master-* 紧急修复线上代码的 bug

工作流程图

Github

Gitlab

------------ 已触及底线了 感谢您的阅读 ------------
  • 本文作者: OWQ
  • 本文链接: https://www.owq.world/git/
  • 版权声明: 本站所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处( ̄︶ ̄)↗