LMLPHP后院

GIT之filter-branch修改作者信息技术

maybe yes 发表于 2017-06-24 10:28

有朋友问我,怎么修改 git 提交过的且已推送到服务器的 commit 作者信息?

这个问题?答案从严格上来讲,是不可以的。仅仅就修改而言,有些情况下是可以修改的,如果软件打过 tag,或者提交的时候签名了,就修改不了。有些情况下可以修改,其实是不建议这么做的,特别是团队开发的时候。因为一个提交一旦推送到远程,就可以假定一定有人下载到他的本地仓库,你这一修改,hash 值就一定改变,其他人更新就会冲突,在团队水平不一致的时候,是个大坑。

摘录一部分官方给出的解释:

The --env-filter option can be used to modify committer and/or author identity. For example, if you found out that your commits have the wrong identity due to a misconfigured user.email, you can make a correction, before publishing the project, like this:

git filter-branch --env-filter '
	if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
	then
		GIT_AUTHOR_EMAIL=john@example.com
		export GIT_AUTHOR_EMAIL
	fi
	if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
	then
		GIT_COMMITTER_EMAIL=john@example.com
		export GIT_COMMITTER_EMAIL
	fi
' -- --all

尝试了一下,修改作者邮箱:

git filter-branch --env-filter '
        if test "$GIT_AUTHOR_EMAIL" = "may@localhost.localdomain"
        then
                GIT_AUTHOR_EMAIL=rhett@126.com
                export GIT_AUTHOR_EMAIL
        fi
' -- --all

运行结果如下:

Rewrite fba1923870630f9f4e289a20a3ff4788916fe535 (167/180) (6 seconds passed, remaining 0 predicted)    
Ref 'refs/heads/dev' was rewritten
Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
Ref 'refs/remotes/origin/dev' was rewritten
WARNING: Ref 'refs/remotes/origin/master' is unchanged
WARNING: Ref 'refs/tags/v1.0.0' is unchanged
WARNING: Ref 'refs/tags/v1.0.1' is unchanged
WARNING: Ref 'refs/tags/v1.0.2' is unchanged
WARNING: Ref 'refs/tags/v1.0.3' is unchanged
WARNING: Ref 'refs/tags/v1.0.4' is unchanged
WARNING: Ref 'refs/tags/v1.0.5' is unchanged
WARNING: Ref 'refs/tags/v1.0.6' is unchanged
WARNING: Ref 'refs/tags/v1.0.7' is unchanged
WARNING: Ref 'refs/tags/v1.0.8' is unchanged
WARNING: Ref 'refs/tags/v1.0.9' is unchanged
WARNING: Ref 'refs/tags/v1.1.0' is unchanged
WARNING: Ref 'refs/tags/v1.1.1' is unchanged
WARNING: Ref 'refs/tags/v1.1.2' is unchanged
WARNING: Ref 'refs/tags/v1.1.3' is unchanged
WARNING: Ref 'refs/tags/v1.1.4' is unchanged
WARNING: Ref 'refs/tags/v1.1.5' is unchanged
WARNING: Ref 'refs/tags/v1.1.6' is unchanged
WARNING: Ref 'refs/tags/v1.1.7' is unchanged
Ref 'refs/tags/v1.1.8' was rewritten
WARNING: You said to rewrite tagged commits, but not the corresponding tag.
WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag.
相关文章
评论列表
huanxi4u
有大神说,git 版本控制工具还是石器时代的东西。
LMLPHP,可爱滴WEB开发框架

2017-09-24 00:30:17 1506184217 0.006348