一个计算机技术爱好者与学习者

0%

好好学Git:Git提交后变更

1. 前言

执行 git commit 后,我们不能保证这些commit信息是完美的,有时就需要优化变更,比如:

  • 提交的 Git 用户信息配置错误
  • 提交的 message 错误
  • 多次提交的 message 相同

此时就需要用到一些变更 commit 的小技巧了。

2. 怎样修改 commit user ?

2.1. 问题描述

git的 user.name 和 user.email 配置错了,然后还提交了很多commit,怎么办?
不要慌,git支持修改历史commit记录的用户名和邮箱。

2.2. 解决办法

参考Changing author info,使用脚本进行修改。

1、clone项目

1
2
git clone --bare https://github.com/user/repo.git
cd repo

2、创建修改用户名的脚本 change-author.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh

git filter-branch -f --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.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

其中 OLD_EMAIL、CORRECT_NAME、CORRECT_EMAIL 三个变量需要替换。

3、执行脚本

1
sh change-author.sh

4、检查结果,强制同步到远程仓库

1
2
git log
git push --force --tags origin 'refs/heads/*'

3. 怎样修改上次 commit message ?

3.1. 问题描述

git commit -m "something",之后,想要修改“something”为“something_new”,该怎样修改?

3.2. 解决办法

使用git commit --amend,可以修改最后一次commit的附加信息。

4. 怎样修改最近几次的commit message?

4.1. 问题描述

已知最近几次提交记录为(最新的commit为efaec33):

1
2
3
4
commit efaec33c2a081e427c5201898b3fc61179475f69
commit 5fdf644e089dcb245b1f3dde5e8942fc935ffccc
commit 550ae58fc222c5c4cd99027a580423d2567969e6
commit f3e9a940df8371d5282f151a5c6e6e74a1e3dead

最近三次的commit message描述都有问题,该怎样修改?

4.2. 解决办法

1、rebase到想要修改记录的前一条commit

1
2
3
4
git rebase -i HEAD~3
# or
git log
git rebase -i f3e9a94

2、按照提示的命令编辑commit信息,改成e e e,保存。

1
2
3
e 550ae58 add: xxx
e 5fdf644 modify: xxx
e efaec33 modify: xxx

3、修改commit信息

1
2
3
4
5
6
7
8
9
10
git commit --amend # 修改 550ae58
# GIT_COMMITTER_DATE="2023-09-02T08:00:00" git commit --amend --date="2023-09-02T08:00:00" # 修改 550ae58 ,同时修改时间戳
git rebase --continue

git commit --amend # 修改 5fdf644
git rebase --continue

git commit --amend # 修改 efaec33
git rebase --continue
# Successfully rebased and updated refs/heads/main.

和修改上次commit信息相比,修改多次commit信息也是使用 git commit --amend ,只不过变成了挨个处理。

PS:取消rebase

1
git rebase --abort

参考文档:Git修改旧提交的时间

5. 怎样合并多次 commit ?

5.1. 问题描述

已知最近几次提交记录为(最新的commit为efaec33):

1
2
3
4
commit efaec33c2a081e427c5201898b3fc61179475f69
commit 5fdf644e089dcb245b1f3dde5e8942fc935ffccc
commit 550ae58fc222c5c4cd99027a580423d2567969e6
commit f3e9a940df8371d5282f151a5c6e6e74a1e3dead

最近三条的提交记录,commit message相同或者意思相近,想要合并成一条commit,该怎样操作?

5.2. 解决办法

1、rebase到想要合并记录的前一条commit

1
2
3
4
git rebase -i HEAD~3
# or
git log
git rebase -i f3e9a94

自动进入vi编辑模式,此时看到的commit信息是按照时间顺序排列的,最新的commit在最下面。

2、按照提示的命令编辑commit信息,改成p s s,保存。

1
2
3
pick 550ae58 add: xxx
s 5fdf644 modify: xxx
s efaec33 modify: xxx

如果修改完报错:error: cannot ‘squash’ without a previous commit
那么可以继续修改:

1
git rebase --edit-todo

3、解决冲突后提交(不使用git commit)

1
2
git add .
git rebase --continue

6. 怎样删除某次 commit ?

6.1. 问题描述

已知最近几次提交记录为(最新的commit为efaec33):

1
2
3
4
commit efaec33c2a081e427c5201898b3fc61179475f69
commit 5fdf644e089dcb245b1f3dde5e8942fc935ffccc
commit 550ae58fc222c5c4cd99027a580423d2567969e6
commit f3e9a940df8371d5282f151a5c6e6e74a1e3dead

现在想要删除 550ae58fc222c5c4cd99027a580423d2567969e6 这次commit,该怎么操作?

6.2. 解决办法

1、rebase到要删除的commit的前一个commit

1
git rebase -i f3e9a940df8371d5282f151a5c6e6e74a1e3dead

自动进入vi编辑模式,此时看到的commit信息是按照时间顺序排列的,最新的commit在最下面。

2、找到删除的commit(第一个),标记为 drop,保存

1
2
3
drop 550ae58 add: xxx
pick 5fdf644 modify: xxx
pick efaec33 modify: xxx

3、解决冲突后提交(不使用git commit)

1
2
3
4
git status
git rm <conflict-file>
git add .
git rebase --continue
  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-git-commit/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~