Skip to content

命令备忘录:Git ​

删除远程分支 ​

shell
git push origin --del rzluan-branch/feature1

克隆指定分支到指定文件夹 ​

shell
git clone -b [branch_name] [git_url] [dir_path]

变更统计 ​

shell
git log --author=rzluan --after="2019-11-01 00:00:00" --before="2019-11-30 23:59:59" --name-only | grep '^src/*' | sort |  uniq 
git log --author=rzluan --after="2019-11-01 00:00:00" --before="2019-11-30 23:59:59" --numstat | grep "src" | awk '{a[$3]+=$1;b[$3]+=$2}END{for(j in a) print j,"代码变更"(a[j]+b[j])"行"}'
git log --author=rzluan --after="2019-11-01 00:00:00" --before="2019-11-30 23:59:59" --numstat | grep "src" | awk '{a[$3]+=($1+$2)}END{for(j in a) b+=a[j]} END{print "总行数",b}'

修改commit记录 ​

修改当前 commit 记录:

shell
# 1. 修改提交时间
DATE="2020-05-08 12:00:30"
GIT_COMMITTER_DATE=$DATE \
git commit --amend --date=$DATE --no-edit
# 2. 修改提交人信息
GIT_COMMITTER_NAME="luanrz" \
GIT_COMMITTER_EMAIL="luanrz@qq.com" \
git commit --amend --author "luanrz <luanrz@qq.com>"
# 3. 强制推送至远程仓库
git push -f

修改历史 commit 记录(rebase 变基):

shell
# 1. 开始 rebase
# 使用 rebase 指定待修改 commit 的父 commit_id,
# 在交互式界面将待修改 commit 项的 pick 修改为 e / edit
git rebase --committer-date-is-author-date -i 77490aff2de6989217fce54d6b34985cb331676f
# 2. 查看最近 3 次 commit 信息(可选)
git log -3 --pretty=fuller
# 3. 重写 author 与 date
NAME="luanrz"
EMAIL="luanrz@qq.com"
DATE="2026-10-09T17:21:18+0800"
GIT_COMMITTER_NAME="$NAME" \
GIT_COMMITTER_EMAIL="$EMAIL" \
GIT_COMMITTER_DATE="$DATE" \
git commit --amend --author="$NAME <$EMAIL>" --date="$DATE" --no-edit
# 4. 检查修改结果
git log -1 --pretty=fuller
# 5. 完成 rebase 
# 注意:对于非 edit 状态对应的commit,其 committer 也会重写
git rebase --continue
# 6. 强制推送至远程仓库
git push -f

撤销commit记录 ​

shell
# 撤销commit,撤销暂存区,工作空间代码改动不变
git reset HEAD
# 同上
git reset --mixed HEAD^
# 撤销commit,暂存区不变,工作空间代码改动不变
git reset --soft HEAD^
# 撤销commit,清空暂存区,清空工作空间代码改变
git reset --hard HEAD^
# 撤销n次commit
git reset HEAD~n
# 修改commit注释
git commit --amend

清除所有commit记录 ​

参见Git – Remove All Commits – Clear Git History (Local & Remote)

shell
# 切换到一个临时分支,该分支的状态和`git init`很像
git checkout --orphan temp_branch
# 在一步可以做一些特殊操作,如删除当前文件夹下所有文件、新增文件等等,后续新分支将会以此处的文件状态为准
git add -A # 提交工作区中所有文件变动,到临时分支暂存区
git commit -am "The first commit" # 提交暂存区中所有文件变动,到临时分支本地仓库
git branch -D main # 删除需要清除所有commit记录的分支
git branch -m main # 将当前临时分支修改成刚刚删掉的分支名
#git push -f origin main # 推送到远程仓库(!!!危险操作!!!)

查看每个文件的commit时间 ​

shell
echo "最新commit时间, 首次commit时间, 文件名" > commit_date.csv
find src | grep md | while IFS= read -r file; do
  last_date=$(git log -1 --pretty=format:"%ad" --date=iso-strict -- "$file" 2>/dev/null)
  first_date=$(git log --follow --diff-filter=A --pretty=format:"%ad" --date=iso-strict -- "$file" | tail -1 2>/dev/null)
  echo "$last_date, $first_date, $file"
done | sort -r >> commit_date.csv

初始化个人 git 仓库 ​

shell
for repo in blog blog-hexo config daily dobby esdumper gnome-shell-extension-app-folder-grid-dimensions hello-data-structure hello-docker hello-java hello-python hello-spring-cloud java-console-todo learn luanrz.github.io qc TheMuseum; do git clone git@github.com:luanrz/$repo.git; done

其等价于:

shell
git clone git@github.com:luanrz/blog.git
git clone git@github.com:luanrz/blog-hexo.git
git clone git@github.com:luanrz/config.git
git clone git@github.com:luanrz/daily.git
git clone git@github.com:luanrz/dobby.git
git clone git@github.com:luanrz/esdumper.git
git clone git@github.com:luanrz/gnome-shell-extension-app-folder-grid-dimensions.git
git clone git@github.com:luanrz/hello-data-structure.git
git clone git@github.com:luanrz/hello-docker.git
git clone git@github.com:luanrz/hello-java.git
git clone git@github.com:luanrz/hello-python.git
git clone git@github.com:luanrz/hello-spring-cloud.git
git clone git@github.com:luanrz/java-console-todo.git
git clone git@github.com:luanrz/learn.git
git clone git@github.com:luanrz/luanrz.github.io.git
git clone git@github.com:luanrz/qc.git
git clone git@github.com:luanrz/TheMuseum.git

单独处理非 github 仓库:

shell
git clone ssh://aur@aur.archlinux.org/gnome-shell-extension-app-folder-grid-dimensions-git.git