Git 签署以前的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13043357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:48:58  来源:igfitidea点击:

Git sign off previous commits?

gitsigngit-sign

提问by camelCaseD

I was wondering how to sign(-s) off previous commits that I have made in the past in git?

我想知道如何签署(-s)关闭我过去在 git 中所做的先前提交?

回答by fgiraldeau

To signoff the previous commit, use amend option:

要签署先前的提交,请使用修改选项:

git commit --amend --signoff

Edit: the amend does signoff only the latest commit. To signoff multiple commits, filter-branchand interpret-trailersas suggested by voncet. al. should be used. Here is what worked for me.

编辑:修改只签收最新的提交。签署多个提交,filter-branchinterpret-trailersvonc等人所建议的那样。阿尔。应该使用。这对我有用。

First, configure git to replace the token signby Signed-off-by. This has to be done only once and is needed in the next step.

首先,配置混帐更换令牌signSigned-off-by。这必须只执行一次,并且在下一步中需要。

git config trailer.sign.key "Signed-off-by"

The command git filter-branchwith the switch --msg-filterwill eval the filter once for each commit. The filter can be any shell command that receives the commit message on stdin and outputs on stdout. You can write your own filter, or use git interpret-trailers, which is indepotent. Here is an example that will signoff the latest two commits of the current branch using the current user and email:

git filter-branch带有开关的命令--msg-filter将为每次提交评估过滤器一次。过滤器可以是在 stdin 上接收提交消息并在 stdout 上输出的任何 shell 命令。您可以编写自己的过滤器,或使用git interpret-trailers,这是无效的。这是一个使用当前用户和电子邮件签署当前分支的最新两次提交的示例:

export SIGNOFF="sign: $(git config --get user.name) <$(git config --get user.email)>"
git filter-branch -f --msg-filter \
    "git interpret-trailers --trailer \"$SIGNOFF\"" \
     HEAD~2..HEAD

Note 1) Modifying commit messages change the commit id, which means pushing over already published branches will have to be forced either with --forceor better --force-with-lease.

注 1) 修改提交消息会更改提交 ID,这意味着必须使用--force或更好的--force-with-lease强制推送已经发布的分支。

Note 2) if you intend to write your custom script, beware that git filter-branchchanges the current directory to <repo>/.git-rewrite/t. Using a relative path to the script won't usually work. Instead, the script should be in your $PATHor provided as an absolute path.

注 2) 如果您打算编写自定义脚本,请注意git filter-branch将当前目录更改为<repo>/.git-rewrite/t. 使用脚本的相对路径通常不起作用。相反,脚本应该在您的$PATH或作为绝对路径提供。

回答by Sebastian Mancke

Try this one to redo old commits with a -S:

尝试使用以下命令重做旧提交-S

git filter-branch -f --commit-filter 'git commit-tree -S "$@"' HEAD

After that, you have to git push -f. But be careful, the commit ids will change and other people will become out of sync.

之后,你必须git push -f。但要小心,提交 ID 会改变,其他人会变得不同步。

回答by VonC

Considering sign-offs modify the commit message , uses git filter-branchto achieve that.

考虑到签收修改提交消息,用于git filter-branch实现这一点。

git filter-branch --msg-filter \
    "cat - && echo && echo 'Signed-off-by: Dan McGee <[email protected]>'" \
    HEAD

(example from "git filter-branchmagic")

(来自“git filter-branch魔法”的例子)

Or, following Curt J. Sampson's suggestion, using git interpret-trailers:

或者,按照Curt J. Sampson建议,使用git interpret-trailers

git config trailer.sign.key "Signed-off-by"
git filter-branch --msg-filter \
    "cat - && echo && git interpret-trailers --trailer 'sign: 'Signed-off-by: Dan McGee <[email protected]>'" \
    HEAD

caveat: this will change the SHA1 of your existing commits, and you might have to force push the result, which can be problematic if your commits are already shared by others.

警告:这将更改您现有提交的 SHA1,您可能必须强制推送结果,如果您的提交已被其他人共享,这可能会出现问题。

vorburgeradds in the commentan example:

vorburger在评论中添加一个例子:

Using git version 2.20.1, I had to omit "Signed-off-by" in --trailer 'sign:, and do it like this:

使用 git 2.20.1 版,我不得不省略 " Signed-off-by" in --trailer 'sign:,并这样做:

git filter-branch --msg-filter \
  "cat - && echo && git interpret-trailers --trailer 'sign: Michael Vorburger <[email protected]>'" \
  HEAD

回答by Keshan Nageswaran

If anyone still looking for a better-automated way of signing off commits.

如果有人仍在寻找一种更好的自动化方式来签署提交。

Try this:

尝试这个:

git rebase --exec 'git commit --amend --no-edit -n -S' -i commit-hash

This will rebase everything till the commit-hash (X commits)

这将重新设置所有内容,直到提交哈希(X 提交)

Then git push -fis required to push back the change in history back to remote

然后git push -f需要将历史更改推回远程

回答by Varun Garg

For me just ammending signof, didn't actually verify my commits on github.

对我来说只是修改signof,实际上并没有验证我在github上的提交。

The solution that is worked for me is going back, and then sign each commit with -S

对我有用的解决方案是回去,然后用 -S

git commit --amend -S

Also if you check if your commit is actually signed, and your email/name is simply not appended, use this command

此外,如果您检查您的提交是否实际签名,并且您的电子邮件/姓名根本没有附加,请使用此命令

git show HEAD --show-signature

Extra tip:If you are already amending your commits, you may want your real name in them (see using git log). You may be using your github handle name, which is not needed. Only correct email is needed and in field of username you should use your full name and github will track it correctly with your github handle name. So to correct your user name and sign last commit use:

额外提示:如果您已经在修改您的提交,您可能希望在其中包含您的真实姓名(请参阅使用git log)。您可能正在使用您的 github 句柄名称,这不是必需的。只需要正确的电子邮件,在用户名字段中,您应该使用您的全名,github 将使用您的 github 句柄名称正确跟踪它。因此,要更正您的用户名并签署上次提交,请使用:

git commit --amend --author="FULL NAME <email>" -S

and also set full name for user name in future by

并在将来设置用户名的全名

git config --global user.name "FULL NAME"

回答by Martin

I had a similar issue. Here, thanks to Robin Johnson from Gentoo Linux is a trick to add the signature to all my previous unpushed commits:

我有一个类似的问题。在这里,感谢来自 Gentoo Linux 的 Robin Johnson 是一个将签名添加到我之前所有未推送提交的技巧:

$ git pull && git rebase --gpg-sign --force-rebase origin/master && git push --signed
Already up-to-date.
Current branch master is up to date, rebase forced.
First, rewinding head to replay your work on top of it...
Applying: sci-biology/KING: new package
Applying: dev-lang/yaggo: version bump, fix install procedure
Applying: sci-libs/htslib: version bump
Applying: sci-biology/bcftools: version bump
Applying: sci-biology/samtools: version bump
Applying: sci-biology/libBigWig: new release with io.h renamed to bigWigIO.h
Applying: sci-biology/MaSuRCA: add more URLs to HOMEPAGE
Applying: sci-biology/SPAdes: update comments on bundled dev-libs/boost
Applying: sci-biology/khmer: added a comment how to proceed with src_compile()
Applying: sci-biology/picard: version bump
Applying: sci-biology/ruffus: pint EGIT_REPO_URI to the archive URL of code.google.com
Applying: sci-biology/vcftools: the 0.1.15_pre release was just renamed to 0.1.15 by upstream
Applying: sci-biology/nanopolish: new package
Applying: sci-biology/libBigWig: version bump
Counting objects: 75, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (75/75), done.
Writing objects: 100% (75/75), 14.51 KiB | 0 bytes/s, done.
Total 75 (delta 55), reused 0 (delta 0)
remote: To github.com:gentoo/sci.git
remote:    29c5e3f5d..b37457700  master -> master
To git+ssh://git.gentoo.org/proj/sci.git
   29c5e3f5d..b37457700  master -> master
$

回答by roshnet

An interactive rebase with the -Sflag will do the job.

带有-S标志的交互式变基将完成这项工作。

Let's say you need to sign off the last ncommits (make sure to checkout the latest of those ncommits).

假设您需要签署最后n 次提交(确保签出这n 次提交中的最新一次)。

Run:

跑:

$ git rebase -i HEAD~n

This gives a list of the last ncommits.

这给出了最后n提交的列表。

Now, change pickto editprefix for all the commits you want to sign.

现在,将 要签名的所有提交更改pickedit前缀。

Once done, close the editor. A new editor will open with everything about the commit.

完成后,关闭编辑器。一个新的编辑器将打开,其中包含有关提交的所有内容。

Since nothing needs to be changed in the commit, save the file and exit the editor. You can also change the commit message while at it.

由于提交中不需要更改任何内容,因此请保存文件并退出编辑器。您还可以更改提交消息。

Repeat this for other commits.

对其他提交重复此操作。

To push the latest history, git push remote branch -f.

要推送最新的历史记录,git push remote branch -f.