DEV Community

Dragoș Străinu
Dragoș Străinu

Posted on Originally published at strdr4605.com

Git: temporary change commit author

It may happen that sometimes you need to temporarily do commits with another author's name and email.
You can do this with this command:

GIT_COMMITTER_NAME="Other Name" GIT_COMMITTER_EMAIL="Other Email" git commit -m Test --author "Other Name <adriana.sanduta@isa.utm.md>"
Enter fullscreen mode Exit fullscreen mode

But you may want to do it multiple times, so you can create a shell function:

# Add me to shell config file. ~/.zshrc or ~/.bashrc
# Git with author
git_w_a(){
  COMMITTER_NAME="Other Name"
  COMMITTER_EMAIL="other_email@example.com"
  echo "
    Running:
    GIT_COMMITTER_NAME=\"$COMMITTER_NAME\" GIT_COMMITTER_EMAIL=\"$COMMITTER_EMAIL\" git $@ --author \"$COMMITTER_NAME <$COMMITTER_EMAIL>\"
  "
  GIT_COMMITTER_NAME="$COMMITTER_NAME" \
  GIT_COMMITTER_EMAIL="$COMMITTER_EMAIL" \
  git $@ \
  --author "$COMMITTER_NAME <$COMMITTER_EMAIL>"
}
Enter fullscreen mode Exit fullscreen mode

Usage:

git commit -m "Test" # Commit with default user name and email
git_w_a commit -m "Test" # Commit with other user name and email
Enter fullscreen mode Exit fullscreen mode

Don't forget to change the COMMITTER_NAME and COMMITTER_EMAIL

Top comments (0)