Skip to content

git常用操作

常规操作

  1. git init:初始化仓库
  2. git add:暂存代码
  3. git commit:提交暂存的代码

分支操作

  1. git branch:查看分支
  2. git checkout [branch]:切换至分支
  3. git branch [branch]:在当前代码基础上创建分支
  4. git merge [branch]:合并指定分支提交到当前分支
  5. git branch -d [branch]:删除指定分支
  6. git rebase [branch]:从指定的分支最后的提交作为当前分支的起点

远程操作

  1. git clone [url]:克隆远程仓库到本地
  2. git remote -v:展示所有远程仓库
  3. git remote add [remote name] [url]:为当前仓库设置一个指定名称的远程仓库
  4. git remote rm [remote name]:删除远程仓库
  5. git remote rename [old remote name] [new remote name]:重命名
  6. git fetch:拉取远程仓库的所有更新到本地
  7. git pull:拉取当前分支的更新到本地,并和当前分支合并
  8. git push [remote name] [branch/tag]:推送分支或者标签到远程仓库

回溯操作

  1. git reset -q [files]:将暂存区指定的文件撤销到工作区
  2. git reset --hard [head]:回溯到指定提交,所有工作区和暂存区文件被清理
  3. git reset --soft [head]:回溯到指定提交,

使用tag管理版本

查看 tag 列表

  • git tag:列出所有标签
  • git tag -l "v3.3.*":过滤标签

新建 tag

  • git tag v1.0:在当前commit上添加一个tag
  • git tag -a v1.1 -m "修复1.0的bug":添加一个带注解的tag
  • git tag -a v0.9 9fceb02 -m "1.0的前一个版本":给指定的commit添加tag

查看 tag 详情:

  • git show [tagName]:查看tag的详细信息

推送 tag 到远程服务器

  • git push origin [tagName]:推送单个 tag 到远程
  • git push origin --tags:推送本地所有 tag 到远程

切换到指定 taggit checkout [tagName] 这个时候不位于任何一个分支,你可以在此修改代码进行提交。但是切换分支时不会保存,你可以使用以下两种方式保存当前状态下修改的代码:

  1. 基于此状态创建一个新分支:git checkout -b <new-branch-name>
  2. 在此状态下新增commit,使用git switch -c <new-branch-name> 将提交保存到新分支。可使用git switch -撤销此操作。

删除 tag

  1. git tag -d [tagName]:本地删除指定tag
  2. git push origin :refs/tags/[tagName]:远程删除

git-show 获取提交的具体信息

将对应的commit信息作为命令行变量:

bash
  export COMMIT_INFO=$(git show -s --format=fuller $CI_COMMIT_SHA)
  echo -e "$COMMIT_INFO \n"

具体参数可查看git-show文档