记录一些不常用但比较有用的git用法

配置自己的用户名和邮箱

1
2
git config --global user.name "你的名字或昵称"
git config --global user.email "你的邮箱"

生成ssh公钥

生成ssh key

1
2
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"# Creates a new ssh key using the provided email
# Generating public/private rsa key pair...

查看public key

1
2
cat ~/.ssh/id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....

从众多提交中保留只需要的提交

1
git cherry-pick <commit id>

如果没有冲突,则显示如下:

1
2
3
Finished one cherry-pick.
# On branch dev
# Your branch is ahead of 'origin/dev' by 3 commits.

如果存在冲突,则需要解决冲突然后继续。

改写历史,去除大文件(减小仓库体积)

1
2
3
git filter-branch --tree-filter 'rm -f path/to/large/files' --tag-name-filter cat -- --all
git push origin --tags --force
git push origin --all --force