Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Modifying Git History: Commit Messages, Authors, and Remote Repositories

Tech 1

Git Commit Command Reference

# Commit with inline message
git commit -m "<message>"

# Stage all tracked file changes and commit in one step
# Note: -a flag only affects already-tracked files, not new ones
git commit -a

# Amend the current commit with new changes or message
git commit --amend

# Create a commit with no actual changes
git commit --allow-empty

# Bypass pre-commit hooks
git commit -n
git commit --no-verify

# Specify author during commit
git commit --author="John Doe <john@example.com>"

# Update author information on the previous commit
git commit --amend --author="John Doe <john@example.com>"

# Update committer info to match new global config
git commit --amend --reset-author --no-edit

# Update content of previous commit without changing message
git add --all && git commit --amend --no-edit

Changing Committer Information

To modify the author of the most recent commit:

git commit --amend --author="John Doe <john@example.com>"

Or when global user configuration has been updated:

git config --global user.name 'newusername'
git config --global user.email 'newemail@example.com'
git commit --amend --reset-author --no-edit

Note: In some workflows, committer and author information may differ.

Rewriting Historical Commit Messages

When multiple commits have been pushed but contain incorrect information, interactive rebase allows modificationn of historical entries.

git rebase -i HEAD~3

The interactive editor displays:

pick abc1234 First commit message
pick def5678 Second commit message
pick ghi9012 Third commit message

Change pick to edit for commits requiring modification, then save and exit.

For example, to modify the first commit, update the line:

edit abc1234 First commit message
pick def5678 Second commit message
pick ghi9012 Third commit message

Once the rebase process pauses at the target commit, update the author:

git commit --amend --reset-author

The editor opens, allowing message edits. Save and exit, then resume:

git rebase --continue

The first commit now reflects the updated author information as the most recent entry in the history.

Bulk Updating Committer Information on GitHub

To replace outdated commit metadata across an entire repository, execute the following script from the project root:

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="old-address@example.com"
CORRECT_NAME="Updated Name"
CORRECT_EMAIL="updated@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Replace the placeholder variables with actual values before execution.

Force-push the rewritten history:

git push --force --tags origin 'refs/heads/*'

Configuring Multiple Remote Repositories

git remote add <remote-name> <remote-url>

Example setup for syncing to both GitHub and GitLab:

git remote add github https://github.com/username/project
git remote add gitlab https://gitlab.com/username/project
git remote -v

Output:

github  https://github.com/username/project (fetch)
github  https://github.com/username/project (push)
gitlab  https://gitlab.com/username/project (fetch)
gitlab  https://gitlab.com/username/project (push)

Push to specific remotes:

git push github main
git push gitlab main

This configuration enables simultaneous pushing to multiple hosting platforms, and branches can be transferred between remotes using standard fetch and push operations.

Tags: git

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.