git 学习: 学习指南/git notes
工作中的常用 git 用法笔记
版本 git --version
# git version 2.37.2
常见用法 Tag 用法 代码 将tag代码恢复到当前分支 git reset --hard v2.0.1
git push -f origin 分支名称
删除远程Tag # 语法
git push --delete origin tagname
# 删除远程的 v1.0.0
git push --delete origin v1.0.0
一键删除本地所有的tag # 删除本地所有 tag
git tag -l | xargs git tag -d
# 删除远程所有 tag
git ls-remote --tags origin | grep -v "{}" | awk '{print $2}' | xargs git push --delete origin
顺便删除 gihtub release # 列出所有的符合要求的
gh release list | awk 'NR>1 {print $2}'
gh release list --limit 9999 | awk 'NR>1 {print $2}' | xargs -I {} gh release delete {} -y
查出项目最新的tag git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' --count=1
升级 semver #!/bin/bash
version="release-2.108.4" # 您的版本号字符串
# 提取版本号的各个部分
major=$(echo "$version" | cut -d'-' -f2 | cut -d'.' -f1)
minor=$(echo "$version" | cut -d'.' -f2)
patch=$(echo "$version" | cut -d'.' -f3)
# 递增修订版本号
patch=$((patch + 1))
# 创建新的版本号字符串
new_version="release-$major.$minor.$patch"
echo "原始版本号:$version"
echo "新版本号:$new_version"
常见用法 branch 用法 代码 删除远程分支 # 删除远程的 gh-pages
git push origin --delete gh-pages
新建并切换到分支 git checkout -b feature/aric
重命名 # 1. 切到其它分支
git checkout <其他分支名>
# 2. 重命名
git branch -m feature/aric feature/aric-privacy
# 3. 推到远程
git push origin -u feature/aric-privacy
cheery-pick 将某条提交,合并到某个分支 假设,我们的目标分支是 staging
首先 checkout
到这个分支 找到需要 merge 进来的 git hash 如 2346383610259c15ba929822e7de0dad4588e315
执行:git cherry-pick 2346383610259c15ba929822e7de0dad4588e315
确认没有 conflict
最后 git push
其它 用法 代码 git rebase/cherry-pick git rebase/cherry-pick: 合并最近的 message
git cherry-pick 目标hash
回滚到某个githash # reset + force push to origin
git reset --hard d8f5f44f8a890f4170269aafde0d8921e534d937
git push -f
清理/网上方法 # 未经测试
# https://stackoverflow.com/questions/2116778/reduce-git-repository-size
# brew install bgf
bfg -b 100M # To remove all blobs from history, whose size is superior to 100Mb
git reflog expire --expire=now --all
git gc --prune=now --aggressive
体积优化(已验证) # 检查体积大小
git count-objects -vH
# 找出大文件的目录
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')"
# 清理
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch 文件' --prune-empty --tag-name-filter cat -- --all
# 释放gc(实际起作用的就是git gc那2行)
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# 向远程推送
git push origin --force --all
git remote prune origin
取短 hash # 方法1
git rev-parse --short HEAD
# 方法2
git log -1 --pretty=format:%h
解决代码冲突时的 “use theirs”
# 重点是第1句
git checkout --theirs <file-path>
git add <file-path>
git commit -m "Resolve conflict using 'theirs' strategy"
解决代码冲突时的 “use ours”
git checkout --ours <file-path>
git add <file-path>
git commit -m "Resolve conflict using 'ours' strategy"
git merge 远程的 master
分支 # 有时候可能要提前
git fetch origin
# merge远程分支
git merge origin/master
# merge 本地分支
git merge master
git撤销commit到未提交状态 https://blog.csdn.net/ywb201314/article/details/106191221
清理 git git stash 出现的场景:你正在开发一个功能,这个时候,你同事在 dev 分支上
git alias Basically you just need to add lines to ~/.gitconfig
[alias]
st = status
ci = commit -v
参考