Git 指南

# 工作常用

# 配置

# 配置帐户
git config --global user.name xxx
git config --global user.email yyy
# check 配置是否正确
git config --global --list
# 把 git 的默认编辑器换成 im
git config --global core.editor vim 
# 为 Git 命令创建一个快捷方式(别名)
git config --global alias.<alias-name> <git-command>
# 定义当前机器所有用户使用命令时用到的文本编辑器,如 git commit。<editor> 参数用编辑器的启动命令(如 vi)替代。
git config --system core.editor <editor>
# 用文本编辑器打开全局配置文件,手动编辑
git config --global --edit
# 在指定目录下创建一个裸仓库
git init --bare <directory>

# 查看

# 查看当前 git 状态
git status
# 查看某次提交的内容
git show 提交记录id
# 比较暂存区域中的文件和最新提交的文件之间的差异
git diff --staged
# 比较两个提交之间的差异。
git diff <commit> <commit>
# 比较两个分支之间的差异。
git diff <branch>..<branch>
# 查看远程分支
git branch -r
git checkout XXX
git remote add origin <server>

# 还原

# 还原工作区的文件
git restore filename
# 还原暂存区中的文件
git restore --staged filename
# 从指定的提交中还原文件:
git restore --source=HEAD-1 filename
# 还原整个目录
git restore dir/
# 回溯版本 - 回溯版本后的版本可能会丢失
git reset --hard df66027f00c(版本号)

# 分支

# 列出本地分支;-r 远程分支 ;-a 全部分支(本地 + 远程)
git branch
# 创建新分支;-d 删除分支
git branch branch-name
git branch -d branch-name
# -D 命令用于强制删除指定的本地分支,即使该分支包含未合并的提交。
git branch -D branch-name
# 切换分支;-b 创建并切换新的分支
git checkout branch-name
git checkout -b branch-name
# 指定分支合并到当前分支
git merge branch-name

# 提交

# 将文件从工作区添加到暂存区,以便将其包含在下一次提交中
git add
# 提交带有签名的提交
git commit -s
# 追加提交,将 add 的内容追加到最近一次提交
git commit --amend
# --no-edit 选项表示不打开编辑器,直接使用原来的提交信息。这个选项通常用于只修改文件内容而不修改提交信息的情况。
git commit --amend --no-edit

# 暂存

# 将修改暂存,需要先 add
git stash
git stash list
git stash clear
git stash apply
# 拿出第一个暂存的修改
git stash pop

# 远程相关

# 工作常用,用于拉取远程代码并且将自己的修改置于 head
git pull --rebase
# 从远程仓库获取最新的代码,并且删除本地已经不存在的远程分支。
git fetch -p
# 提交远程
git push origin branch-name
git push ssh://账号@项目路径  HEAD:分支
# 将指定的提交 (可多个提交) 应用到当前分支中。
git cherry-pick <commit>..
# 继续之前被中断的 cherry-pick 操作(一般是冲突了之后会中断,手动解决后使用)
git cherry-pick --continue

--rebase 是将远程代码拉取到本地,并且将自己的修改放置在最前面。 pull 对比 pull --rebase

# 原理相关

imgimgimg

# 文件的状态:

  • untracked 未跟踪
  • unmodified 未修改
  • modified 已修改
  • staged 已暂存
  • committed 已提交

# Git 提交规范

  • feat:新功能
  • fix:修补 bug
  • docs:文档
  • style : 格式
  • refactor:重构
  • test:增加测试
  • chore:构建过程或辅助工具的变动

# Gerrit

使用 commit --amend 追加前面提交的时候,gerrit 通过 changeId 来判断,从而使得 push 的时候可以追加到之前的提交,只是在远程加了 patchsetN

更新于 阅读次数 本文阅读量:

请我喝[茶]~( ̄▽ ̄)~*

Windlinxy 微信支付

微信支付

Windlinxy 支付宝

支付宝