Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Feb 16, 2022 git · development · pairing

Add Co-Authored-By to all commits after pairing

TL;DR

Here's the full solution:

shell git filter-branch --msg-filter "ruby -e'puts [$stdin.read,nil,*ARGV].join(%{\\n})' -- 'Co-Authored-By: John Doe <[email protected]>'" origin/master..HEAD

Now let's break it down, piece by piece.

The commits

This will list all commits that are in your branch but not on origin/master

shell origin/master..HEAD

The filter

The simplest ruby script that will:

  1. read the original commit message from $stdin
  2. append any provided argument with a new line

ruby puts [ $stdin.read, nil, # extra empty line, for good measure *ARGV ].join(%{\n})

And we use -- to separate file names to execute from CLI arguments:

shell $ ruby -e 'p ARGV' -- "anything after '--'" "ends up in ARGV" ["anything after '--'", "ends up in ARGV"]

Putting it all together

We're lucky git filter-branch has an option for changing commit messages, we'll use that to append Co-Authored-By: … information to every commit in the current branch.

The --msg-filter option will execute a script that will take the old message as its standard input and use the standard output as the new message.

shell git filter-branch --msg-filter "ruby -e'puts [$stdin.read,nil,*ARGV].join(%{\\n})' -- 'Co-Authored-By: John Doe <[email protected]>'" origin/master..HEAD

The git-rebase option no-one knows that will shock your coworkers!

A few weeks ago, a moment before pushing upstream a glorious bug-fix, I ran the test suite locally, just to discover that I broke it!

Having lost all confidence in the commit history, I was out for blood, looking for the piece of code that ruined the whole PR!

With more than a hundred git aliases I consider myself quite a git nerd, so I reached out for a tool I knew: git rebase.

My plan was:

  1. mark each commit with edit
  2. run the test suite with bin/rake at each stop
  3. identify the offending commit and fix whatever bugs I encountered
  4. amend the commit and call it a day

That didn't feel right tho, it was too clunky, I had a hunch that I could have done better than this! So, after reading once again the git rebase documentation, this little option popped out: git rebase --exec!

What --exec does to git rebase

The magic of --exec is that it will execute a command after each commit in your interactive rebase. If the command fails, it will stop the rebase, allow you to fix whatever needs to be fixed, and move along with git rebase --continue.

This means that I was able to swiftly run this command after each commit and ensure that I had a green suite at every step of the PR.

bash git rebase --interactive --exec "bin/rake" origin/master

Don't forget that this neat trick can be used for all kinds of problems, like, for example, running a linter for every commit of a PR.

Mar 4, 2013 git · autocomplete · shorthand · shortcuts · bash

Autocomplete "git" as "g"

For all you lazy Git bums, here’s how you can type “g” got “git” and still have autocompletion (paste into your ~/.bash_profile or .dotfiles system):

alias g='git'
complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
    || complete -o default -o nospace -F _git g