Git命令备忘

仓库信息

  • 查看仓库体积
    git count-objects -vH

用户操作

  • 记住用户名密码
    git config --global credential.helper store
    // 之后cd到项目目录,执行git pull命令,会提示输入账号密码。输完这一次以后就不再需要,并且会在根目录生成一个.git-credentials文件

  • 清除用户名和密码
    // 运行一下命令缓存输入的用户名和密码
    git config --global credential.helper wincred
    // 清除掉缓存在git中的用户名和密码
    git credential-manager uninstall

  • 清除未提交但暂存的内容
    git clean -d -x -n

  • 最后一条提交的Hash值(40位Hash)
    git rev-parse HEAD

  • 最后一条提交的Hash值(8位Hash)
    git rev-parse --short HEAD

  • 最后一条提交的Hash值(8位Hash)
    git rev-list HEAD --abbrev-commit --max-count=1

  • 最后一条全路径
    git describe --all --long

  • 历史记录(树形)
    git log --graph

  • 历史记录(指定develop分支、指定日期、指定关键词task|bug|story)
    git -C project-design log develop --first-parent --no-merges --grep="task|bug|story" --pretty=format:"%s" --abbrev-commit --after="2023-08-22"

  • 简化版历史记录(树形)
    git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %cn %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

  • 历史记录(文本)
    // 40位Hash值
    git log --pretty=oneline
    // 8位Hash值
    git log --oneline

  • 最后一条提交记录(6位Hash)
    git log --pretty=oneline --abbrev-commit --abbrev=5 -1

  • 最后一条提交记录(多行)
    git show

  • 最后一条提交记录(单行,8位Hash)
    git show --oneline

  • 所有提交的Hash值
    git rev-list HEAD

  • 可能是提交次数
    git rev-list HEAD|wc -l

  • 添加一个提交
    git add .
    git commit -m 'create'
    git push


Config操作

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

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

  • 设置提交代码时的用户信息
    git config [--global] user.name "[name]"
    git config [--global] user.email "[email address]"


Branch操作

  • 查看所有分支
    // 本地
    git branch
    // 远程
    git branch -r

  • 查找branch名
    git branch --contains 'hash值'

  • 拉取指定分支
    git clone -o origin/[分支名] -b [tag名] [仓库地址]

  • 推送分支到远程
    git push origin [分支名]

  • 删除分支
    // 本地
    git branch -d [分支名]
    // 远程
    git branch -r -d origin/[分支名]


Tag操作

  • 查看全部Tag(仅标签名)
    git tag

  • 查看全部Tag(带时间)
    git for-each-ref --sort=taggerdate --format '%(refname) %(taggerdate)'
    // or 不同风格
    git log --tags --simplify-by-decoration --pretty="format:%ci %d"

  • 当前版本打标签
    git tag -a v1.4 -m 'version 1.4'
    git push origin v1.4

  • 指定版本打标签
    git tag -a v1.4 -m 'version 1.4' [commit-id]
    git push origin v1.4

  • 指定标签处再打个标签
    // 比其它打标签多一个-f
    git tag -f -a release-1.4 -m 'release 1.4' [指定tag标签]
    git push origin release-1.4
  • 推送所有标签到远程
    git push origin --tags

  • 查看指定标签的提交内容
    git show [tag标签]

  • 查看指定标签的文件内容
    git show v1.0.1:[文件名]

  • 查看某个提交的标签名
    git tag --points-at [commit-id]

  • 拉取指定Tag代码
    git checkout [tag标签]

  • 查看最后一个标签的hash值
    git rev-list --tags --max-count=1

-通过tag名查找hash值
git rev-parse [tag名]

  • 查看最后一个标签名
# tag_name-9位tag hash值
git describe --tags
# or 主分支最后一个tag_name
git describe --tags `git rev-list --tags --max-count=1` 
# or 当前分支最后第一个tag_name
git tag --sort=-taggerdate | head -n 1

  • 下载指定的Tag
    git clone --branch [tag标签] [git地址]

  • 删除本地标签
    git tag -d [tag标签]

  • 删除远程标签
    git push origin :refs/tags/[tag标签]

  • 删除所有本地标签并拉取远程标签
    方法一:

git tag -l | xargs tag -d | git fetch 

方法二:

git tag -d $(git tag -l)
git fetch --tags
  • 指定标签与指定提交作对比
    git diff [tag标签] [版本Hash]

  • 指定标签与当前版本作对比
    git diff [tag标签] HEAD

  • 下载特定的分支
    git clone -b [分支名称] [git地址]

  • 查看指定标签在哪个分支上
    git branch --contains [tag标签]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容