Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change my previous commits' authors? [duplicate]

Tags:

git

github

On the first project I've done using a github repo, my initial commits were all over the place, and not linked with my github account, instead the shown authors for said commits were names such as my pc User default name, from commiting and pushing directly from the IDE (Apache NetBeans). Now I'd like to change those so that my github account would have registered those commits as contributions.
Upon further research, I found some methods that seemed to do the job, but either those were not the solution to my problems, or I executed them wrongly somehow.
Here's an example of a unlinked commit, and here is what they should look like.

Here's the first method i've tried:

1. Rewrite your previous commits:

  • Execute the following to rewrite the commits with the correct author's name (without considering email):
git filter-branch --commit-filter '
  if [ "$GIT_AUTHOR_NAME" = "WrongName" ]; then
    GIT_AUTHOR_NAME="YourName"
    git commit-tree "$@"
  else
    git commit-tree "$@"
  fi' HEAD
  • Replace “WrongName” for the incorrect author and “YourName” for the correct one.

2. Force o Push to a remote repository:

  • After rewriting your commits, force push to GitHub:
git push --force

After following these steps, I got this message:

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

So here's what I did:

1. Verifying Backup Commits:

  • Use the following to list the commits in the directory:
git show-ref refs/original/*
  • Note down the hashes from the commits you wish to remove.

2. Removing Backup Commits:

  • Execute the following to delete backup commits:
git update-ref -d refs/original/refs/heads/master
  • This should remove the backup commit associated to the master branch.

3. Force Push to the Remote Repo:

  • After removing backup commits, do a force push to GitHub:
git push --force

It is worth noting that none of these worked, or at least, did not give any response to executing each code (no messages). Also found this person who had a similar experience as to the found "solutions" to their problem, although it is a different problem.

like image 409
Caio Cunha Avatar asked Feb 02 '26 08:02

Caio Cunha


1 Answers

There's a built-in mechanism for switching commit authors git commit --amend --author 'Test-Auth <[email protected]>' -- this fixes latest commit.

ex:

$ git log
commit 5f5dccf73ecfe838ea2ed84a8fcc6b4379fc23dd (HEAD -> master)
Merge: 1320d0a 5baab1e
Author: Alex Dascal <-------@---------->
Date:   Wed May 29 09:28:37 2024 +0300

    Merge remote-tracking branch 'origin'

commit 1320d0a8dfd533397c5a06df6421d79d36d884e8
Author: Alex Dascal <-------@---------->
Date:   Wed May 29 09:27:39 2024 +0300

    Test commit

$ git commit --amend --author 'Test-Auth <[email protected]>'
[master fd1a225] Merge remote-tracking branch 'origin'
 Author: Test-Auth <[email protected]>
 Date: Wed May 29 09:28:37 2024 +0300
 Committer: Alex Dascal <-------@---------->

$ git log
commit fd1a22503d08af6d230016e9b38872a246469c58 (HEAD -> master, origin/master, origin/HEAD)
Merge: 1320d0a 5baab1e
Author: Test-Auth <[email protected]>
Date:   Wed May 29 09:28:37 2024 +0300

    Merge remote-tracking branch 'origin'

If you want to change multiple commits at once this will become a bit tedious but you can git rebase -i --root and then in vim pick the commits you want to change or use :%s/^pick/edit/g to change all of them and edit the author by using the command show above. When you are done you can force push back into your repo.

!!Changing anything on a commit (even author) will reset it's hash - Just FYI

like image 184
Alex D. Avatar answered Feb 04 '26 01:02

Alex D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!