git修改历史提交信息(包含作者信息)

修改 git repo 历史提交的 Author

本文:https://blog.csdn.net/xiaowu_zhu/article/details/83024558
最近学到了 git 的一招对我来说的新技巧:修改历史提交的 author。

在某天打开码云,查看项目时,发现贡献者好几个,我就纳闷了我一个人写的代码怎么会有好几个贡献者,最后通过git log查看提交信息发现,确实是在提交的过程中用了好几个用户名,于是就百度了一下git命令,最后让我找到了git rebase

使用方式

使用 git rebase -i HEAD~n 命令,n表示要修改前 n 次所有的提交, 说白了就是你要查看多少条提交记录。比如,我要查看从现在到30条范围内的提交记录,所以可以使用 git rebase -i HEAD~30-i中的 i 是 interactive,交互的意思。

输入此命令后,显示以下结果:

pick ac0fcc6 add file2
pick a0cbfbe add file3
pick 16ee6eb add file4

# Rebase d57f11f..16ee6eb onto d57f11f (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell

通过列表找到,需要要修改的提交信息,根据提示,如,上面的例子中,我需要修改第二行和第三行的提交信息,那我们就可以将第二行和第三行的 pick 改成 edit 或 e,保存退出。

保存上面的修改并退出后,git 会依次执行上面的操作,当操作为 pick 时,直接 commit。当操作为 edit 时,会中断,并提示以下信息:

You can amend the commit now, with

    git commit --amend 

Once you are satisfied with your changes, run

    git rebase --continue

这里的意思是说,你可以使用 git commit --amend 来修改此次提交,修改以后,觉得满意了,执行 git rebase --continue 继续剩下的流程。

由于我们的主要目的是修改提交者的信息,因此光用 git commit --amend 是不够的,我们要使用 git commit --amend --author "xw <aa.hbl@gmail.com>" 这样的操作,这一点是修改提交者信息的关键所在。

使用上面的命令成功修改此次提交的提交者信息后,一定要记得执行 git rebase --continue 继续。

最终完成以后提示如下:

$ git rebase --continue
Successfully rebased and updated refs/heads/master.

注意: 在本次操作中用到了git命令行和vim编辑模式,不明白的话就自行百度吧。

posted @ 2018-10-12 11:34  jxiaow  阅读(665)  评论(0编辑  收藏  举报