git 学习: 学习指南/git notes/fixup/rebase

工作中的常用 git 用法笔记
更新于: 2024-03-26 11:29:22

版本

git --version
# git version 2.37.2

常见用法 Tag

用法代码
创建tag
# 创建tag,并带 message
git tag -a v1.4 -m 'version 1.4'
将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

用法代码
删除分支(本地/远程)
# 删除本地
git branch -D feature/feat-unused
# 删除远程
git push origin --delete feature/feat-unused
删除远程分支
# 删除远程的 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
重命名实战(当前分支)
# 1. 将当前分支(beta-as-develop)重命名为 develop
git checkout beta-as-develop
git branch -m develop
# 2. 将 develop 推到远程
git push origin -u develop
# 3. 将远程的 beta-as-develop 分支删除
git push origin --delete beta-as-develop
cheery-pick
将某条提交,合并到某个分支
  1. 假设,我们的目标分支是 staging
  2. 首先 checkout 到这个分支
  3. 找到需要 merge 进来的 git hash 如 2346383610259c15ba929822e7de0dad4588e315
  4. 执行:git cherry-pick 2346383610259c15ba929822e7de0dad4588e315
  5. 确认没有 conflict
  6. 最后 git push

其它

用法代码
git rebase/cherry-pick

git rebase/cherry-pick: 合并最近的 message

git cherry-pick 目标hash
git cp(我配置的快捷)
git fixup(精细化管理git message)
# usage
git commit --fixup <commit>

# example
git add .
git commit --fixup 3050fc0de
# Created a fixup commit for `3050fc0de`
git rebase HEAD~5 --autosquash
# Now the fixup commit has been squashed
回滚到某个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 merge/reabase
# 以 rebase 方式来 merge 代码
git pull --rebase
# 以 merge 方式来(注意: 不是 --merge 命令)
git pull --no-rebase
# 全局(true: rebase | false: merge)
git config --global pull.rebase true
git config --global pull.rebase false
git 删除某个文件,保留线上
右侧命令执行完,.yarnrc 不会变灰
# 1. 添加 .yarnrc 到 .gitignore 中去
# 2. 从缓存中移除
git rm --cached .yarnrc
# 3. 提交修改
git add . && git add -m "feat: rm .yarnrc from git" && git push

清理 git

  • 查看大小
  • 找出占用空间的大文件
  • 清理,释放内存

git stash

出现的场景:你正在开发一个功能,这个时候,你同事在 dev 分支上

git alias

Basically you just need to add lines to ~/.gitconfig

[alias]
    st = status
    ci = commit -v

git 忽略大小写

# 配置层面(需要变成敏感,就改成false)
git config core.ignorecase true
git config --global core.ignorecase true
# 命令方式修改文件
git mv --cached name.txt NAME.TXT
git mv -f mynewapp.sln MyNewApp.sln

参考