网站首页 > 精选文章 正文
背景
由于 Github 和公司 Git 使用账号不一样,偶尔没注意,提交出错后就需要修改 commit 信息。
修改最后一次提交 commit 的信息
# 修改最近提交的 commit 信息
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <jiang_liheng@163.com>"
# 仅修改 message 信息
$ git commit --amend --message="modify message by daodaotest"
# 仅修改 author 信息
$ git commit --amend --author="jiangliheng <jiang_liheng@163.com>"
修改历史提交 commit 的信息
操作步骤:
- git rebase -i <commit id> 列出 commit 列表
- 找到需要修改的 commit 记录,把 pick 修改为 edit 或 e,:wq 保存退出
- 修改 commit 的具体信息git commit --amend,保存并继续下一条git rebase --continue,直到全部完成
- 中间也可跳过或退出git rebase (--skip | --abort)
# 列出 rebase 的 commit 列表,不包含 <commit id>
$ git rebase -i <commit id>
# 最近 3 条
$ git rebase -i HEAD~3
# 本地仓库没 push 到远程仓库的 commit 信息
$ git rebase -i
# vi 下,找到需要修改的 commit 记录,```pick``` 修改为 ```edit``` 或 ```e```,```:wq``` 保存退出
# 重复执行如下命令直到完成
$ git commit --amend --message="modify message by daodaotest" --author="jiangliheng <jiang_liheng@163.com>"
$ git rebase --continue
# 中间也可跳过或退出 rebase 模式
$ git rebase --skip
$ git rebase --abort
批量修改历史 commit 信息
创建批量脚本changeCommit.sh:
$ cat changeCommit.sh
#!/bin/sh
git filter-branch --env-filter '
# 之前的邮箱
OLD_EMAIL="jiangliheng@126.com"
# 修改后的用户名
CORRECT_NAME="jiangliheng"
# 修改后的邮箱
CORRECT_EMAIL="jiangliheng@163.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
执行脚本成功后,强制推送到远程服务器:
$ git push --force --tags origin 'refs/heads/*'
- 上一篇: Git 分支管理策略与工作流程
- 下一篇: Git每次push都要输入用户名和密码该如何解决?
猜你喜欢
- 2024-12-16 Git 两个常见错误
- 2024-12-16 SVN与Git的区别,读完之后,大部分程序员都收藏了...
- 2024-12-16 这样配置使用GIT命令clone/push/pull等不再重复输入密码
- 2024-12-16 Git常用操作
- 2024-12-16 git清除历史纪录
- 2024-12-16 如何使用 Git 工作树(Worktrees)来提高工作效率
- 2024-12-16 我写一个git用法吧
- 2024-12-16 一篇文章明白Git的14条常用命令
- 2024-12-16 掌握git命令,图解一目了然
- 2024-12-16 Git 版本控制:团队协作与项目管理的利器
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)