-2

For the past few days I used a different laptop, I set up git and logged in, but I accidently used my username with a wrong email.

Today I noticed that my commits from the past few days did not show up on my github profile dashboard so checked what's going on and noticed that I used the wrong email to log in.

I tried a few different ways to fix it, but none of those worked.

1.

git rebase -i -p <The last commit with the good email>

Then for each commit.

git commit --amend --author="good name <good email>" --no-edit
git rebase --continue

Instead of just changing the author of the commits, it made new commits with my good email but didn't change the old ones.

2.

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

as seen here, but it didn't do anything...

3. In one of the answers somebody mentioned that I should use the same command as I used.

git rebase -i -p <The last commit with the good email>

but without the "-p". I tried it, but it also made new commits and didn't keep the old commit's with the old dates.


So, I need help getting all the new commits of my history and fixing the old ones to have the good email.

BeeFriedman
  • 1,800
  • 1
  • 11
  • 29
  • Okay, you didn't give us any error messages you're getting, but: make sure you didn't remove your new commits :) As already posted in that answer you already mentioned and also down below, if having fixed everything locally, you have to bring it on the server the right way and that would be by doing it with the `forced` flag: "git push --force" – Janos Vinceller Jul 18 '21 at 17:43
  • @JanosVinceller I didn't get any errors.. It just didn't change the author at all. – BeeFriedman Jul 18 '21 at 20:15
  • @BeeFriedman OK: would `git filter-repo` work better? – VonC Jul 18 '21 at 20:17
  • Try this too: https://gist.github.com/carlosmcevilly/2221249 Maybe this one help, that one uses a `--reset-author` flag. – Janos Vinceller Jul 18 '21 at 20:20
  • This one fixes your config first, maybe that helps at the push too. – Janos Vinceller Jul 18 '21 at 20:23
  • @JanosVinceller, I tried the link that you provided. It created new commits with the right name but it didn't change the old one. – BeeFriedman Jul 19 '21 at 05:00
  • Maybe you should do exactly that. Creating new commits and pushing those and removing the old ones. – Janos Vinceller Jul 19 '21 at 05:26
  • @JanosVinceller good idea, but that's my backup plan, because I don't want to have all my commits from the past week (about 12) on one day.. – BeeFriedman Jul 19 '21 at 05:44
  • 2
    ***YOU CAN'T CHANGE COMMITS***. You can make new ones and re-hang labels so the labels refer to them instead of the originals, but ***YOU CAN'T CHANGE COMMITS***. – jthill Jul 20 '21 at 19:42
  • They showed you how to make new commits and re-hang labels. – jthill Jul 21 '21 at 16:42
  • Great! I guess I didn't know the exact terminology. – BeeFriedman Jul 21 '21 at 17:06

4 Answers4

6

Thank you for everybody who helped out! I ended up getting a lot of different approaches, but it didn't work out for me, I ended up having to rebase my repo multiple times to the state before the changes because things got messed up.
In the end I contacted Github support and this is what they told me to do, and it worked. I am posting it here in hopes that it would help out somebody who has a similar issue.

P.S. This is pretty similar to this answer suggested here but that answer didn't work when I tried it. This one has some minor changes (cloning a bare copy and performing the change there) and it did work.


1 - Before running this script, you'll need:

The old email address that appears in the author/committer fields that you want to change The correct name and email address that you would like such commits to be attributed to 2 - Create a fresh, bare clone of your repository:

  git clone --bare <external repo URL>.git
  cd <reponame>.git

3 - Copy and paste the script, replacing the following variables based on the information you gathered:

OLD_EMAIL
CORRECT_NAME
CORRECT_EMAIL
  #!/bin/sh

  git filter-branch --env-filter '

  OLD_EMAIL="your-old-email@example.com"
  CORRECT_NAME="Your Correct Name"
  CORRECT_EMAIL="your-correct-email@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

4 - Press Enter to run the script.

5 - Review the new Git history for errors.

6 - Push the corrected history to:

  git push --mirror <GitHub repo URL>.git

7 - Clean up the temporary clone:

  cd ..
  rm -rf <reponame>.git
BeeFriedman
  • 1,800
  • 1
  • 11
  • 29
3

Try first and clone the repository in a separate local folder, in order to test the following command.

You need to install newren/git-filter-repo in order to rewrite the author from your old account to your new account.

See "How to change commit author for multiple commits using filter branch?" and
"git filter-repo / User and email based filtering"

git filter-repo --mailmap my-mailmap

with my-mailmap:

Correct Name <correct@email.com> <old@email.com>

That will rewrite your commits with the right author.

You will then need to git push --force (assuming one main branch) in order to override the remote history with your new commits. If you are not alone working on the remote repository, make sure to let your collaborators know.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • in your answer you are suggesting to use a third party tool. Is there no way to do it using only git? – BeeFriedman Jul 19 '21 at 02:50
  • 1
    @BeeFriedman Not reliably, for reasons detailed in https://github.com/newren/git-filter-repo#why-filter-repo-instead-of-other-alternatives. Give it a try. it works really well. – VonC Jul 19 '21 at 05:52
  • 1
    Read the last line of the first paragraph at VonC's link – Inigo Jul 19 '21 at 07:07
  • @VonC, I tried using this tool and I am having some issues. The only way I could run it is as a python script, "git filter-repo.py" and when I add the flags and run it nothing happens. Could you pls explain in more detail how to use it on windows. Thanks! – BeeFriedman Jul 20 '21 at 15:34
  • @BeeFriedman I tested it on Windows (in a simple `CMD`) without issue: `git filter-repo` (no `.py` extension) is enough, provided python and the script (that is, a clone of https://github.com/newren/git-filter-repo) are in your `%PATH%`. – VonC Jul 20 '21 at 15:55
  • Ok, so I installed it using pip that's why I need the .py extension. Now I cloned it as you said, but I am getting the following error "bash: syntax error near unexpected token `<' " – BeeFriedman Jul 20 '21 at 16:38
  • @BeeFriedman Do you mean `git filter-repo --mailmap my-mailmap`, with a `my-mailmap` including `<` and `>`? Because those `<` and `>` are representing a placeholder value, and should not be present in the actual file. – VonC Jul 20 '21 at 16:43
  • So I just write --mailmap "name" "email" "old email"? – BeeFriedman Jul 20 '21 at 16:45
0

You were very close to the correct method. Just don't add -p while rebasing.

According to git documentation, https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---preserve-merges

-p option - Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces.

I did the same steps as you mentioned in your question, without -p, and it worked flawlessly. Here is the list of steps in case you need it.


Firstly, clone the repo locally, and do an interactive rebase for all the commits.

git rebase --interactive COMMIT~

For example :-

git rebase --interactive 8020fc5fe36d75b066f7c06a29c92a29ccf6e2b4~

where 8020fc5fe36d75b066f7c06a29c92a29ccf6e2b4 is the oldest commit from which you want to start fixing.

You can get this value from git log

After running git rebase, you will have a text editor, with options to pick/edit/... for each commit.

Lets take an example, where you get something like this...

pick 8020fc5 1
pick e99c3c7 2
pick 7c4f81e 3
pick 3b30037 4
pick 98d901b 5
pick 8ded5a1 6
pick 5a61adc 7
pick 8fe082c 8
pick 1abebc1 9

For all the commits you want to change the author for, change the command to edit from pick

Like in the following example, I want to change authors for 3 and 5, so

pick 8020fc5 1
pick e99c3c7 2
edit 7c4f81e 3 #changed here
pick 3b30037 4
edit 98d901b 5 #changed here
pick 8ded5a1 6
pick 5a61adc 7
pick 8fe082c 8
pick 1abebc1 9

Now save the file and exit.

Then, you will be at the point in time, just before applying the first commit you have set to edit.

Without changing anything, do the following steps..

git commit --amend --author="NAME <EMAIL>" --no-edit
git rebase --continue

After this you, the repository will be at the point just before applying the second commit you selected to edit, so repeat it till you are done

Update:

git rebase --committer-date-is-author-date HASH where HASH is the first commit with the correct timestamp.

Then do git log --format=fuller to see if the commit timestamps are what you want.

Anubhav Gupta
  • 421
  • 5
  • 12
  • I followed your answer, and it commited everything with today's date. I want them to have the original dates. How could I undo this and fix it. – BeeFriedman Jul 20 '21 at 15:32
  • I am sorry, I had the mistaken idea that github showed author's date and not the commiter's date. It can be easily fixed, and I have updated my answer. Hopefully it fixes your issue. However, Could you do git log and please confirm whether the timestamps you are seeing in git log are the ones you want to see on github? If thats the case, then follow the updated answer. – Anubhav Gupta Jul 21 '21 at 00:59
  • Yeah in th log they have the right dates. I'll try your updated answer. Thanks! – BeeFriedman Jul 21 '21 at 01:01
  • wait, I am still updating it. – Anubhav Gupta Jul 21 '21 at 01:02
  • Finished adding whatever I had to add, `git rebase --committer-date-is-author-date HASH` where HASH is the first commit with the correct timestamp. – Anubhav Gupta Jul 21 '21 at 01:07
  • Do comment whether it solved your issue or not. – Anubhav Gupta Jul 21 '21 at 01:22
  • Thank you for your help! I ended up following github supports instructions. – BeeFriedman Jul 21 '21 at 03:38
0

You can use git-filter-repo with .mailmap file to change author info like user name, email for few commits in the past, even for all commits in a branch

You could see my repo for detail, just few steps:

https://github.com/vuongtlt13/change-git-author