Skip to content

Instantly share code, notes, and snippets.

@wewoor
Last active April 29, 2022 03:19
Show Gist options
  • Select an option

  • Save wewoor/f4cf6b03e7044ca4850224b3fca8be83 to your computer and use it in GitHub Desktop.

Select an option

Save wewoor/f4cf6b03e7044ca4850224b3fca8be83 to your computer and use it in GitHub Desktop.
常用的 git 命令

常用的 git 命令

修改最近的一次 commit

$ git commit --amend

撤回上一次commit

$ git reset --soft HEAD~ #缓存操作
# 或者 
$ git rest HEAD~

查看log

$ git log # commit log
$ git reflog # 操作 log 

查看冲突文件列表

$ git diff --name-only --diff-filter=U

git 合并某个commit

$ git cherry-pick <commit-id>

git 删除多个分支

# git branch | grep -E '.*(feat|fix|xiaowei|release).*' | awk -F/ '{print $0}' | xargs -I {} git branch -D {}

# Use the following command to remove all branches with PREFIX prefix on remote server.
$ git branch -r | awk -F/ '/\/PREFIX/{print $2}' | xargs -I {} git push origin :{}

# You may want to do a dry-run first to see if it is the branches that you want to remove:
$ git branch -r | awk -F/ '/\/PREFIX/{print $2}'
  • 清理本地过时的远程分支
$ git remote prune origin

批量删除远程分支

$ git branch -r | grep 'origin/fix.*' | awk -F/ '{print $2"/"$3}' | xargs -I {} git push origin :{}

# 删除数字后缀的分支
$ git branch -r | grep -E 'origin/feat/[0-9]+' | awk -F/ '{print $2"/"$3}' | xargs -I {} git push origin :{}

# 删除数字版本分支
git branch -r | grep -E 'origin/[0-9].[0-9].[0-9].*' | awk -F/ '{print $2}' | xargs -I {} git push origin :{}

删除 Tag

# 删除本地所有tag
$ git tag -d $(git tag -l)
# 删除所有远程Tag
$ git push origin --delete $(git tag -l) # Pushing once should be faster than multiple times

删除 untrackerd 文件

# 删除未追踪的文件
git clean -f
# 删除所有未追踪以及忽略的文件
git clean -df

GC .git 文件

# 垃圾回收,修建老的git/objects
git gc --aggressive --prune

# 展示top20 
git verify-pack -v .git/objects/pack/pack-{hash}.idx | sort -k 3 -n | tail -n 20

# 删除历史文件
git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch filename' --prune-empty -f -- --all

git subtree

# 推送 dist 目录内容到 gh-pages 目录
git subtree push --prefix dist origin gh-pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment