git change user name in history commits

Git 修改历史提交记录中的用户名

背景

之间git提交时的用户名不对,但已经提交了4个commit了,所以需要修改历史记录中的用户名.

方案

使用git rebase

git rebase会把基于 rebase的版本后的commit取消掉,并临时保存为path到.git/reabase目录中,然后把当前分支更新到rebase的分支,最后把你修改的成为path的版本以补丁的形式应用到rebase的版本上.
1564128143617

操作

这里把要rebase到4个commit之前的一个版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[Fri Jul 26 talen@tp-arch-tianfei capacity]$ git rebase -i HEAD~5
pick 7e1b101 X0
pick 983d257 X1
pick 17c4a0e X2
pick 41e90ef X3
pick 822104d X4
# Rebase 5131eea..822104d onto 41e90ef (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
1
2
3
4
5
Stopped at 4ac3916...  X0
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue

vi修改X1-X4的pick为edit,X0是rebase的版本,wq:退出

1
[Fri Jul 26 talen@tp-arch-tianfei capacity]$ git commit --amend --author="tianfei.hao <tianfei.hao@example.com>" --no-edit

修改正确的用户名及邮箱

1
2
[Fri Jul 26 talen@tp-arch-tianfei capacity]$ git rebase --continue
[Fri Jul 26 talen@tp-arch-tianfei capacity]$ git push origin new_journey --force

强制提交操作

坚持原创技术分享,您的支持将鼓励我继续创作!