Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all commits by author

Tags:

git

git-filter

How do I remove all commits by certain author (committed by mistake - such an author should not be visible in the commits history).

I have found some code to rename -

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="name"
CORRECT_EMAIL="[email protected]"
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


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

Is there some filter not to rename but remove such commits?

like image 422
Dancyg Avatar asked Aug 30 '16 16:08

Dancyg


People also ask

How do you get rid of all commits in git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

How do I change the author of multiple commits?

Update the author details of historical commitsCheck through the commits in the list, and hit ctrl+x , followed by enter to apply the changes.

How do I remove old commits from GitHub?

First, remove the commit on your local repository. You can do this using git rebase -i . For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up. Then, force push to GitHub by using git push origin +master .


1 Answers

You can do it like this:

  1. Create a new branch based on the commit you want to start with:

    git checkout -b <branch-name> <base-commit>
    
  2. Cherry-pick all commits that don’t have the matching author:

    git log --author "<name>" --invert-grep --reverse --format="format:%H" HEAD..master | xargs git cherry-pick
    

The log filters out all commits made by the author and then cherry-picks them one after one.

Explanation for the parameters (partly quoted from git log manpage):

  • --author "name"
    Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression). With more than one --author=, commits whose author matches any of the given patterns are chosen (similarly for multiple --committer=).
  • --invert-grep
    Limit the commits output to ones with log message that do not match the pattern specified with --grep=
  • --reverse Output the commits in reverse order. […]
  • --format="format:%H" Use a custom format, in this case only the commit hash
like image 86
Andreas Wolf Avatar answered Sep 19 '22 18:09

Andreas Wolf