गिस्ट्स

अपने कंप्यूटर पर Git को-ऑथर ट्रेलर अपने आप हटाएँ

markdown · 25 अक्टूबर 2026

अपने कंप्यूटर पर Git को-ऑथर ट्रेलर अपने आप हटाएँ

GitHub पर देखें

# Automatically strip Git co-author trailers on your machine

This setup removes `Co-authored-by:` lines from new commit messages and lets the commit continue if the repository's other checks pass. A pre-push check also rejects pushes containing commits with co-author lines, catching commits that bypassed cleanup. It applies to your OS user, across repositories using the global hooks setting. Existing commits and the commit's author identity stay unchanged.

## Requirements

- macOS or Linux: Git, a POSIX shell, and Perl on your PATH.
- Windows: run from Git Bash with Git and Perl available (`command -v git perl`), or install inside WSL for repositories you use from WSL. A WSL installation does not configure Windows Git.

Run the installer separately on each machine and for each OS user. Clients must use the same Git configuration and support Git hooks.

## Install

Copy the following block into your terminal. Alternatively, save it as `install.sh` and run `sh install.sh`. No administrator privileges are required.

The installer creates executable hooks under `~/.config/git/coauthor-cleanup/hooks` and sets the global `core.hooksPath`. If an absolute global hooks path already exists, it preserves that path and delegates to its hooks. A relative global hooks path needs manual resolution first; the installer stops with an explanation. Running the installer again updates the installed scripts.

```sh
#!/bin/sh
set -eu

for dependency in git perl; do
  command -v "$dependency" >/dev/null 2>&1 || {
    printf 'Required command missing: %s\n' "$dependency" >&2
    exit 1
  }
done

hooks="$HOME/.config/git/coauthor-cleanup/hooks"
previous=$(git config --global --path --get core.hooksPath || true)
if [ -n "$previous" ] && [ "$previous" != "$hooks" ]; then
  case "$previous" in
    /*|[A-Za-z]:/*) ;;
    *)
      printf 'Existing global hooksPath is relative: %s\nResolve it before installing.\n' "$previous" >&2
      exit 1
      ;;
  esac
  git config --global coauthorCleanup.globalDelegateHooksPath "$previous"
fi

mkdir -p "$hooks"
for hook in applypatch-msg pre-applypatch post-applypatch pre-commit \
  pre-merge-commit prepare-commit-msg commit-msg post-commit pre-rebase \
  post-checkout post-merge pre-push pre-receive update proc-receive \
  post-receive post-update reference-transaction push-to-checkout \
  pre-auto-gc post-rewrite sendemail-validate fsmonitor-watchman \
  p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit \
  post-index-change
do
  cat > "$hooks/$hook" <<'HOOK'
#!/bin/sh
set -eu

hook=${0##*/}
delegate=$(git config --local --path --get coauthorCleanup.delegateHooksPath 2>/dev/null || true)
if [ -z "$delegate" ]; then
  delegate=$(git config --global --path --get coauthorCleanup.globalDelegateHooksPath || true)
fi
if [ -z "$delegate" ]; then
  common_dir=$(git rev-parse --git-common-dir 2>/dev/null || printf '%s' "${GIT_DIR:-.git}")
  delegate=$common_dir/hooks
fi

if [ "$hook" = pre-push ]; then
  scratch=$(mktemp -d)
  trap 'rm -rf "$scratch"' EXIT
  trap 'exit 1' HUP INT TERM
  cat > "$scratch/refs"
  while read -r local_ref local_oid remote_ref remote_oid; do
    case "$local_oid" in *[!0]*) ;; *) continue ;; esac
    if ! local_commit=$(git rev-parse --verify "$local_oid^{commit}" 2>/dev/null); then
      continue
    fi
    if remote_commit=$(git rev-parse --verify "$remote_oid^{commit}" 2>/dev/null); then
      git rev-list "$local_commit" "^$remote_commit" > "$scratch/commits"
    else
      git rev-list "$local_commit" > "$scratch/commits"
    fi
    while read -r commit; do
      git show -s --format=%B "$commit" > "$scratch/message"
      if LC_ALL=C grep -iE '^[[:blank:]]*co-authored-by[[:blank:]]*:' "$scratch/message" >/dev/null; then
        printf 'Push blocked: commit %s contains a Co-authored-by line.\nRemove it from the commit message and retry.\n' "$commit" >&2
        exit 1
      fi
    done < "$scratch/commits"
  done < "$scratch/refs"
  if [ -x "$delegate/$hook" ]; then
    "$delegate/$hook" "$@" < "$scratch/refs"
  fi
  exit 0
fi

if [ -x "$delegate/$hook" ]; then
  "$delegate/$hook" "$@"
fi

case "$hook" in
  prepare-commit-msg|commit-msg)
    perl -i -ne 'print unless /^[\t ]*co-authored-by[\t ]*:/i' -- "$1"
    ;;
esac
HOOK
  chmod +x "$hooks/$hook"
done

git config --global core.hooksPath "$hooks"
printf 'Installed: %s\n' "$hooks"

```

## How it works

Git passes a message-file path to two hooks:

1. `prepare-commit-msg` cleans the initial message before the editor opens, including messages supplied with `-m` or `-F`.
2. `commit-msg` cleans the final message after editing.

Both hooks remove lines matching this case-insensitive pattern:

```text
^[\t ]*co-authored-by[\t ]*:
```

For example:

```text
Fix login redirect

Co-authored-by: Assistant <assistant@example.com>
Signed-off-by: Developer <developer@example.com>
```

becomes:

```text
Fix login redirect

Signed-off-by: Developer <developer@example.com>
```

The scripts first run an existing executable hook, then clean the message if that hook succeeds. A failed existing check still stops the commit. Delegation chooses the repository-specific path configured below, then the saved previous global path, then the repository's default hooks directory. It does not run all three directories.

## Push protection

The `pre-push` hook checks commit messages before Git sends the push. If any checked commit contains a co-author line, the push fails and prints the offending commit hash. Existing pre-push hooks still receive the original ref updates after this check passes.

For an existing remote ref whose old commit is available locally, it checks commits reachable from the new tip but not the old tip. For a new branch or tag, or when the old remote commit is unavailable locally, it conservatively checks all history reachable from the new tip. This can reject historical co-authors too. Branch deletions and tags pointing to non-commit objects have no commit messages to inspect.

The hook does not rewrite history or invalidate signatures automatically. For the latest unpushed commit, `git commit --amend --no-edit` reruns message cleanup; then retry the push. Older commits require deliberate history editing. If multiple refs are pushed, any failing ref blocks the entire push.

## Connect repositories that have their own hooks path

A repository's local `core.hooksPath` overrides the global setting. Check from inside the repository:

```sh
git config --show-origin --get core.hooksPath
git config --local --path --get core.hooksPath
```

If the second command returns a path, save that exact path as the delegate before removing the local override. For example, for a Husky repository returning `.husky/_`:

```sh
git config --local coauthorCleanup.delegateHooksPath '.husky/_'
git config --local --unset core.hooksPath
```

Replace `.husky/_` with your repository's actual previous path. Never point the delegate at `~/.config/git/coauthor-cleanup/hooks` itself, or it will recurse.

Hook managers may restore their local `core.hooksPath` during dependency installation. Repeat this integration if that happens. Conditional includes, worktree configuration, or command-line overrides can also select another path; use `--show-origin` to inspect the effective value.

## Verify with a disposable repository

This creates an empty test commit in a temporary repository. It does not change your working project or publish anything.

```sh
test_repo=$(mktemp -d)
git -C "$test_repo" init -q
git -C "$test_repo" -c user.name='Hook Test' -c user.email='test@example.invalid' \
  -c commit.gpgSign=false commit --allow-empty \
  -m 'Test automatic cleanup' \
  -m 'Co-authored-by: Example <example@example.invalid>'
git -C "$test_repo" log -1 --format=%B
printf 'Test repository: %s\n' "$test_repo"
```

The displayed message should contain `Test automatic cleanup` and no co-author line. If your delegated hooks require project tooling, they may reject the disposable commit; check their output.

## Limits

- This is automatic cleanup, not an unbreakable policy. A command can override or disable hooks.
- `git push --no-verify` bypasses push protection. A local or command-line hooksPath override also bypasses this global setup. There is no global Git setting that makes client hooks mandatory.
- `git commit --no-verify` skips `commit-msg`, but still runs `prepare-commit-msg`. An initial `-m` message is cleaned; a co-author typed into the editor afterward can remain when using `--no-verify`.
- GitHub web commits, other machines, `git commit-tree`, and tools that do not invoke these hooks are outside its scope.
- Existing history is unchanged. Operations that replay existing commits are not guaranteed to clean every message; push protection can catch their co-author lines before upload.
- The rule deletes whole matching lines anywhere in a message, including examples in its body. It does not remove other attribution formats, such as `Co-authored by` without the hyphens.
- Removing every matching line could leave an empty message; Git's usual empty-message rules still apply.
- The installer writes dispatchers for the hook names listed in its loop. A new Git hook introduced later must be added if you use it.

## Disable or uninstall

Restore the previous global hooks path if one was saved; otherwise remove this global setting:

```sh
previous=$(git config --global --get coauthorCleanup.globalDelegateHooksPath || true)
if [ -n "$previous" ]; then
  git config --global core.hooksPath "$previous"
  git config --global --unset coauthorCleanup.globalDelegateHooksPath
else
  git config --global --unset core.hooksPath
fi
```

For each repository you connected manually, restore its original path too:

```sh
previous=$(git config --local --get coauthorCleanup.delegateHooksPath || true)
if [ -n "$previous" ]; then
  git config --local core.hooksPath "$previous"
  git config --local --unset coauthorCleanup.delegateHooksPath
fi
```

The scripts can remain on disk while disabled. You may delete `~/.config/git/coauthor-cleanup` afterward if you no longer need it.

## References

- [Git hook lifecycle and bypass behavior](https://git-scm.com/docs/githooks)
- [Git core.hooksPath configuration](https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath)