本文介绍了我如何从git存储库中删除作者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了一个Git仓库并公开发布它(例如,在GitHub等),并且我收到了贡献者对仓库的请求,无论出于何种原因删除或遮盖了它们的名称,是否有这样一种方法可以轻松完成?

基本上,我有这样的要求,可能希望用匿名贡献者或者SHA-1哈希替代他们的名字和电子邮件地址的电子邮件地址或类似的东西。

解决方案

Jeff很正确,正确的轨道是git filter-branch。它期望一个与环境变量一起使用的脚本。对于你的用例,你可能想要这样的东西:

  git filter-branch --env-filter'
如果[$ GIT_AUTHOR_NAME=Niko Schwarz];然后\
导出GIT_AUTHOR_NAME =Jon DoeGIT_AUTHOR_EMAIL [email protected]; \
fi
'

您可以测试它是否如此工作:

  $ cd / tmp 
$ mkdir filter-branch&& cd filter-branch
$ git init
/private/tmp/filter-branch/.git/中初始化的空Git存储库
$
$ touch hi&& git add。 &安培;&安培; git commit -m bla
[master(root-commit)081f7f5] bla
0个文件已更改,0个插入(+),0个删除( - )
创建模式100644 hi
$ echo howdi>>嗨&& git commit -a -m bla
[master a466a18] bla
1个文件已更改,1个插入(+),0个删除( - )
$ git log
commit a466a18e4dc48908f7ba52f8a373dab49a6cfee4
作者:Niko Schwarz< [email protected]>
日期:Thu Aug 12 09:43:44 2010 +0200

bla

commit 081f7f50921edc703b55c04654218fe95d09dc3c
作者:Niko Schwarz< niko.schwarz @ gmail.com>
Date:Thu Aug 12 09:43:34 2010 +0200

bla
$
$ git filter-branch --env-filter'
>如果[$ GIT_AUTHOR_NAME=Niko Schwarz];那么\
> export GIT_AUTHOR_NAME =Jon DoeGIT_AUTHOR_EMAIL [email protected]; \
> fi
> '
Rewrite a466a18e4dc48908f7ba52f8a373dab49a6cfee4(2/2)
Ref'refs / heads / master'被重写
$ git log
commit 5f0dfc0dc9a325a3f3aaf4575369f15b0fb21fe9
作者:Jon Doe< john @ bugmenot.com>
日期:星期四8月12日09:43:44 2010 +0200

bla

commit 3cf865fa0a43d2343b4fb6c679c12fc23f7c6015
作者:Jon Doe< john @ bugmenot。 COM>
日期:星期四8月12日09:43:34 2010 +0200

bla


If I create a Git repository and publish it publicly (e.g. on GitHub etc.), and I get a request from a contributor to the repository to remove or obscure their name for whatever reason, is there a way of doing so easily?

Basically, I have had such a request and may want to replace their name and e-mail address with something like "Anonymous Contributor" or maybe a SHA-1 hash of their e-mail address or something like that.

解决方案

Jeff is quite right, the right track is git filter-branch. It expects a script that plays with the environment variables. For your use case, you probably want something like this:

git filter-branch --env-filter '
    if [ "$GIT_AUTHOR_NAME" = "Niko Schwarz" ]; then \
        export GIT_AUTHOR_NAME="Jon Doe" GIT_AUTHOR_EMAIL="[email protected]"; \
    fi
    '

You can test that it works like this:

$ cd /tmp
$ mkdir filter-branch && cd filter-branch
$ git init
Initialized empty Git repository in /private/tmp/filter-branch/.git/
$
$ touch hi && git add . && git commit -m bla
[master (root-commit) 081f7f5] bla
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hi
$ echo howdi >> hi && git commit -a -m bla
[master a466a18] bla
 1 files changed, 1 insertions(+), 0 deletions(-)
$ git log
commit a466a18e4dc48908f7ba52f8a373dab49a6cfee4
Author: Niko Schwarz <[email protected]>
Date:   Thu Aug 12 09:43:44 2010 +0200

    bla

commit 081f7f50921edc703b55c04654218fe95d09dc3c
Author: Niko Schwarz <[email protected]>
Date:   Thu Aug 12 09:43:34 2010 +0200

    bla
$
$ git filter-branch --env-filter '
> if [ "$GIT_AUTHOR_NAME" = "Niko Schwarz" ]; then \
> export GIT_AUTHOR_NAME="Jon Doe" GIT_AUTHOR_EMAIL="[email protected]"; \
> fi
> '
Rewrite a466a18e4dc48908f7ba52f8a373dab49a6cfee4 (2/2)
Ref 'refs/heads/master' was rewritten
$ git log
commit 5f0dfc0dc9a325a3f3aaf4575369f15b0fb21fe9
Author: Jon Doe <[email protected]>
Date:   Thu Aug 12 09:43:44 2010 +0200

    bla

commit 3cf865fa0a43d2343b4fb6c679c12fc23f7c6015
Author: Jon Doe <[email protected]>
Date:   Thu Aug 12 09:43:34 2010 +0200

    bla

Please beware. There's no way to delete the author's name without invalidating all later commit hashes. That will make later merging a pain for people that have been using your repository.

这篇关于我如何从git存储库中删除作者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:59