Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git sign off previous commits?

Tags:

git

sign

git-sign

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

like image 229
camelCaseD Avatar asked Oct 24 '12 05:10

camelCaseD


People also ask

How do I remove a previous commit in git?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

What is git sign-off?

Git signoff simply adds a line to your commit message with your full name and email address. It is intended to signal that you created this commit, have permission to submit it, and you adhere to the project licensing.

How do you add a sign-off?

If you already have the commit, use git commit -s --amend to add the above signoff line. Or, if you are going to be sending this as a patch or patch series, you can use git format-patch -s or --signoff to add the signoff to the patch itself, without modifying the commit.


1 Answers

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-branch and interpret-trailers as suggested by vonc et. al. should be used. Here is what worked for me.

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

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

The command git filter-branch with the switch --msg-filter will 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:

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 --force or better --force-with-lease.

Note 2) if you intend to write your custom script, beware that git filter-branch changes 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 $PATH or provided as an absolute path.

like image 73
fgiraldeau Avatar answered Sep 25 '22 20:09

fgiraldeau