-2

I have a repository on bitbucket which is aligned with the online repository (origin), while using Atlassian Sourcetree as tool for managing it.

Let's say it has 20 commits.

Sourcetree for some reason haves its share of fun changing project settings and I wrongly pushed 2 commits (let's say commit 15 and 16 as some other coworker which uses the machine too.

I want to put my name and mail in those commits.

Let's imagine the tree like this (without branches, just master)

Commit number -> comment -> commit id -> author/mail

  • 18 -> commit comment blabla -> 29huh23 -> me me@mycompany.com
  • 17 -> commit 17 feature XYZ -> abs2881 -> me me@mycompany.com
  • 16 -> commit 16 feature KWZ -> anu2716 -> someone other@othercompany.com
  • 15 -> commit 15 feature IHZ -> 11suhs2 -> someone other@othercompany.com
  • 14 -> commit 14 feature UYZ -> 1uuhw87 -> me me@mycompany.com

someone other@othercompany.com should become me.

I'm admin on the repo.

How to do it in a simple way? It's possible from Sourcetree? Should I do it from the terminal?

Could it be a workable solution to manually modify files in the .git folder and create a new repo and commit there?

Question is not duplicate as suggeste duplicate answers does not work well.

EDIT - CORRECT PROCEDURE FOR ANYONE LOOKING HOW TO DO IT WITHOUT TOO MUCH RESEARCH

Since everyone is marking this as duplicate while is not, as other procedures are INCOMPLETE and leave someone (like me) who is not very cool with git with problems and questions, the correct procedure is:

From SourceTree, launch terminal with the terminal button on top right when the repo is open.

Then paste this and execute, with the substitutions suitable for your case

    #!/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

This though, will produce a secondary "backup" branch, because when rewriting history, which left me with much doubts.

I had to do this additional step to remove the "backup" branch and actually push the new history to the repo and find the new history with correct name/mail:

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

Here the full guide.

Thanks to everyone who pointed me to the right direction.

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • 1
    This might help: https://stackoverflow.com/a/32914963/2020827 – sergej May 29 '18 at 12:41
  • 1
    Possible duplicate of [Change the author and committer name and e-mail of multiple commits in Git](https://stackoverflow.com/questions/750172/change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-git) – phd May 29 '18 at 14:03
  • @phd No it's not. – Liquid Core May 29 '18 at 14:12

1 Answers1

4

There is not a "simple" way to do this. The author and committer information are part of the commit; changing them changes the commit ID. Changing the commit ID means the subsequent commits' parent info has to change, and that changes those commit's IDs. In other words, to change it you need a history rewrite.

History rewrites are not particularly difficult to do, but they come with a significant cost when the affected commits have already been shared (or, maybe more precisely, when the "old" version of an affected commit would be removed from the already-shared history of a branch).

To do such a rewrite, it is necessary to coordinate with everyone else who has already received the "old" commits. I often hear people say this would be too burdensome, because of how many people have a repo. In that case, you can't afford to do the rewrite. If you ignore that advice, what will probably happen is someone else, trying to "fix" the error they receive, will undo your rewrite, and then you'll have a truly splintered repository that's of little use to anyone until an even bigger coordinated effort is completed to clean it up.

With all that said, if you still want to do a history rewrite just to clean up a few commits' email addresses, check out git filter-branch with the --env-filter option.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • I'm the only assigned resource to that project so it wouldn't be a problem. – Liquid Core May 29 '18 at 12:44
  • @LiquidCore - Ok. I mean, you don't have to convince me. As I said: "if you still want to do a history rewrite just to clean up a few commits' email addresses, check out git filter-branch with the --env-filter option." – Mark Adelsberger May 29 '18 at 12:54
  • I found this: https://stackoverflow.com/questions/38588584/using-git-filter-branch-to-rename-author-and-committer-of-several-branches-inclu Could you help me building the command? How would I go to change mail and name/surname – Liquid Core May 29 '18 at 12:58
  • I tried the script here: https://stackoverflow.com/questions/32910842/remove-committer-information-from-git-commits/32914963#32914963 But it created another new branch with the renames parallel to the wrong history branch – Liquid Core May 29 '18 at 13:04
  • 1
    `git filter-branch` creates "backup refs" in case the new history isn't right. Because it's a very powerful command, it wants to make sure you can back out of mistakes. If the newly created history is correct, and the "old" history is, as I assume, named something like `original/refs/heads/master`, then the operation worked and you just need to clean up the unwanted backup refs. – Mark Adelsberger May 29 '18 at 13:27
  • turns out that the cleanup command is: git push --force --tags origin 'refs/heads/*' – Liquid Core May 29 '18 at 13:38
  • By running that script and executing this last command I fixed the repo. – Liquid Core May 29 '18 at 13:40
  • @sergej it did but the answer is incomplete, as the cleaning it's not explicited and has to be searched in the documentation – Liquid Core May 29 '18 at 13:48
  • @sergej If you want to argument I suggest you do it in the chat, not in the comments. – Liquid Core May 29 '18 at 13:54
  • 2
    @LiquidCore - Answers involving `filter-branch` come up *a lot*, and I think it's perhaps more reasonable for those of us writing these answers over and over again to expect people to read the command's documentation, than for those asking the questions to expect us to repeat over and over again what's already in the documentation. If his answer pointed you in the right direction, you should probably up-vote instead of down. But in the end, you use your votes how you see fit. I've up-voted, so as it is Sergj is break-even on score and ahead on reputation from this exchange. – Mark Adelsberger May 29 '18 at 13:55
  • @LiquidCore - and if you want to get petty about it, remember that questions can be voted as well. – Mark Adelsberger May 29 '18 at 13:55
  • @MarkAdelsberger Your answer pointed me to the right direction. Then I had to do a trial/error with the script, documentation and hints over the internet. So sergej answer is incomplete and not correct. Your explanation was clear and led me to investigate and find a solution. About the rest, I think an answer should provide all the code needed to solve the problem unless you need to post a huge code, which is not the case. – Liquid Core May 29 '18 at 14:13
  • @sergej I gave a more detailed explanation above. If you want to act like a kid, do it, it doesn't matter to me. We're grown adults with opinions. – Liquid Core May 29 '18 at 14:14