一份偏实战的 Git 速查表。短句、短命令,适合临时救火与日常回忆。

远程分支

删除远程分支

git push origin --delete <branch>
# 旧写法(等价)
git push origin :<branch>

删除本地分支

git branch -d <branch>      # 已合并

git branch -D <branch>      # 强制删除

提交统计

统计提交者的 commit 数量

git shortlog -sn

撤销与回滚

删除某一次 commit(保留历史)

git revert --strategy resolve <commit>

撤销工作区改动(未暂存)

git restore <file>

撤销暂存区改动(保留工作区)

git restore --staged <file>

中文乱码(diff / log)

在命令行下输入以下命令:

最后一条是因为 git log 默认使用 less 分页,需要 less 使用 utf-8 编码。
想长期生效就写到 shell 配置里。

git config --global core.quotepath false          # status 显示中文

git config --global gui.encoding utf-8            # 图形界面编码

git config --global i18n.commit.encoding utf-8    # 提交信息编码

git config --global i18n.logoutputencoding utf-8  # 输出 log 编码

export LESSCHARSET=utf-8

修改历史作者(老仓库)

git filter-branch --env-filter '
    if [ "$GIT_AUTHOR_NAME" = "Old Name" ]; then \
        export GIT_AUTHOR_NAME="New Name" GIT_AUTHOR_EMAIL="new@mail.com"; \
    fi
'

提示:这会重写历史,请确保团队沟通与备份到位。

Tags

列出所有标签

git tag

创建标签

Git 有两类标签:轻量标签和附注标签。

轻量标签

git tag <tagname>

附注标签

git tag -a v2.1.0 -m "xxx feature is released in this tag."

推送标签

git push origin v1.0.9
# 推送全部标签
git push origin --tags

删除标签

git tag -d v1.0

git push --delete origin v1.0