133

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

CLARIFICATION: Git has confusingly similarly named concepts git commit -s, --signoff (lower case -s) and git commit -S, --gpg-sign (upper case -S).
This question is about signoff(lower case -s).
However some answers here are about gpg-sign(upper case -S), that better discussed in dedicated questions on superUser Can you GPG sign old commits? and stackoverflow Is there a way to gpg sign all previous commits?

4

11 Answers 11

182

To signoff the previous commit, use amend option:

git commit --amend --signoff

Since Git 2.13, you can use the --signoff rebase option to specify range of commits to signoff (credits to @sschuberth). Example to signoff last two commits:

git rebase --signoff HEAD~2

To signoff multiple commits using Git prior to version 2.13, use filter-branch and interpret-trailers as suggested by @vonc et. al. 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.

3
  • 3
    Might require a git push -f.
    – Max
    Jun 9, 2019 at 17:16
  • 1
    I tried to change the last 6 commits by changing to 'HEAD~6..HEAD' and it changed a lot more commits. bascially a mess Aug 14, 2019 at 11:43
  • 1
    Please note that nowadays you can use git rebase --signoff {sha-or-HEAD~} Nov 10, 2022 at 16:30
64

These days (starting with Git 2.13) you can generally do something like

git rebase --signoff HEAD~2

to add Signed-off-by footers to the last 2 commits (in this example).

If your range includes the root commit, add the --root option to rebase.

5
  • I just tested it and this had the same effect as the other suggested answers. To signoff every commit, replace HEAD~2 with the hash of the first commit.
    – fantom
    Apr 13, 2021 at 2:53
  • 4
    This should somehow get to the top
    – Yaniv K.
    Apr 25, 2021 at 6:34
  • 4
    To sign commits to, to be 100% verifiable, do git rebase --signoff -S HEAD~2 Feb 1, 2022 at 9:39
  • "replace HEAD~2 with the hash of the first commit" not quite @fantom, replace with the hash of the parent of of the first commit. The parameter is the "base" commit and it won't be altered.
    – tuxayo
    Aug 5, 2022 at 2:11
  • this worked perfectly, followed by git push -f.
    – Triguna
    Oct 3, 2022 at 15:54
36

NOTE: the answer is about gpg-sign(upper case -S), that better discussed in dedicated questions on superUser Can you GPG sign old commits? and stackoverflow Is there a way to gpg sign all previous commits?

Try this one to redo old commits with a -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.

4
  • 1
    This will sign off ALL commits, right? How do I sign off only the last X commits? Feb 11, 2017 at 13:48
  • 18
    @ÁkosVandra if you're still looking for an answer: git filter-branch -f --commit-filter 'git commit-tree -S "$@"' HEAD~X..HEAD, where X is the number of last X commits. e.g. for last 2 commits: git filter-branch -f --commit-filter 'git commit-tree -S "$@"' HEAD~2..HEAD Apr 25, 2017 at 4:26
  • 2
    What about doing so only for MY commits, not all?
    – kerner1000
    Mar 7, 2019 at 11:34
  • 5
    I think there is a slight confusion, capital -S is for GPG-sign commit, I think that the OP meant lower -s to simply add Sign-off-by line at the end of the commit message.
    – fgiraldeau
    Jun 10, 2019 at 20:16
31

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)

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

4
  • 3
    For more granular approach, first run interactive git rebase -i HEAD~x. Then, in editor, "inject" x git commit -S -s --amend --no-edit after each picked commit you'd like to mess with. Proposed solution in this answer is probably the cleanest (and most git) way to go with this problem. +1
    – Vexy
    Apr 6, 2020 at 13:36
  • 2
    The best and better answer I love than the select one. : )
    – tai271828
    Oct 26, 2020 at 22:28
  • 1
    This was smooth as cream. Thanks Keshan! One point is to be noted that this rebases till the commit-hash excluding the commit-hash itself.
    – borz
    Jul 1, 2021 at 6:17
  • Try with additionally specifying --root.
    – sschuberth
    Sep 25, 2021 at 7:56
15

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

The solution that is worked for me is going back, and then sign each commit with -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 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"
0
12

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

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

(example from "git filter-branch magic")

Or, following Curt J. Sampson's suggestion, using 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.

vorburger adds in the comment an example:

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

git filter-branch --msg-filter \
  "cat - && echo && git interpret-trailers --trailer 'sign: Michael Vorburger <[email protected]>'" \
  HEAD
8
  • Consider using git interpret-trailers with git filter-branch --msg-filter rather than adding the Signed-off-by: or other trailers by hand. This will let you, e.g., avoid duplicating trailers.
    – cjs
    Sep 5, 2017 at 12:14
  • @CurtJ.Sampson Yes! Thank you. I was literally documenting that yesterday: stackoverflow.com/a/41361273/6309.
    – VonC
    Sep 5, 2017 at 12:19
  • @CurtJ.Sampson I have amended the answer accordingly.
    – VonC
    Sep 5, 2017 at 12:27
  • Using git version 2.20.1, I had to omit "'Signed-off-by" in --trailer 'sign: and do it like this: git filter-branch --msg-filter \ "cat - && echo && git interpret-trailers --trailer 'sign: Michael Vorburger <[email protected]>'" \ HEAD
    – vorburger
    Apr 8, 2019 at 10:35
  • 1
    I had to remove the command prefix cat - && echo &&, otherwise it fails with command not found error.
    – fgiraldeau
    Jun 10, 2019 at 20:18
12

An interactive rebase with the -S flag will do the job.

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

Run:

$ git rebase -S -i HEAD~n

# The `-S` flag is important.
# It tells Git to sign the following commits.

This gives a list of the last n commits.

Now, change pick to edit prefix for all the commits you want to sign.

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.

Warning

There's one gotcha - it can rewrite your commits.

If you sign a 4-month old commit, it might overwrite its date and make it look like it was created today. So, not recommended when you want to preserve your commit history.

0
6

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:

$ 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
$
3

A quick solution to signoff last X number of commits.

git rebase --signoff @~X

For example, signoff last 10 commits

git rebase --signoff @~10

I found this an easy solution for my case. Source: https://pmhahn.github.io/git-signoff/

3

A workaround that does not rewrite history:

  1. create a new branch
  2. merge from the old one with flags --no-commit --no-ff
  3. git reset to remove all the commits (signed or unsigned)
  4. git commit -S -am "commit message"
  5. push the new branch with just one SIGNED commit
git status
  ...
  On branch feature/branch_unsigned_commits
  ...
git checkout -b feature/branch_unsigned_commits_take2
git merge --no-commit --no-ff feature/branch_unsigned_commits
git reset
git commit -S -am "commit message"
git push
1
  • This still rewrites the history, it just does so on a new branch. Aug 24, 2021 at 9:27
0

Sign all unsigned commits till a particular commit hash -

  1. Find the earliest unsigned commit (commit-hash) using -
    git log --show-signature
  2. Run command -
    git rebase --exec 'git commit --amend --no-edit -n -S' -i commit-hash
  3. In rebase interactive shell press - :x
  4. This will sign every commit from most recent to right before the commit-hash
  5. (Optional) Resolve conflicts if any (git add <filename> => git commit -S -m "message" => git rebase --continue))
  6. Force push the changes to your git origin -
    git push -f

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.