Question

One time, I accidentally pushed a commit to the remote repository with the wrong author email. I used

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Niklas Rosenstein'; GIT_AUTHOR_EMAIL='<<email>>'; GIT_COMMITTER_NAME='Niklas Rosenstein'; GIT_COMMITTER_EMAIL='<<email>>';" HEAD

to rewrite the history. Then I used

$ git push origin development
    Enter passphrase for key '----':
    To git@gitlab_cip:rosenstein/beak.git
     ! [rejected]        development -> development (non-fast-forward)
    error: failed to push some refs to 'git@gitlab_cip:rosenstein/beak.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
    hint: before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull origin development
    Enter passphrase for key '----':
    From gitlab_cip:rosenstein/beak
     * branch            development -> FETCH_HEAD
    Merge made by the 'recursive' strategy.

$ git push origin development
    Enter passphrase for key '----':
    Counting objects: 430, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (336/336), done.
    Writing objects: 100% (402/402), 43.93 KiB | 0 bytes/s, done.
    Total 402 (delta 262), reused 85 (delta 65)
    To git@gitlab_cip:rosenstein/beak.git
       dd06969..2b2ddb4  development -> development

Which probably was a huge mistake.

graph

I got all commits doubled! The wrong commit's author was fixed (well duplicated, then changed), but the original commit exists, too!

I later read that I (probably) should have used

git push origin --force development fix

Can I fix this somehow?

Was it helpful?

Solution

If nobody has recently pulled from your remote repo, you can:

git checkout development

# Make sure you don't have any work in progress
# That will cancel the merge (assuming you didn't make any new commits on it)
git reset --hard HEAD^1

# replace the development branch by your new history
git push origin --force development

(From the SPECIFYING REVISIONS section of the git rev-parse man page)

I used ^1 instead of ~1 in order to select the first parent (which is in your development branch), instead of the second parent (which is the one from origin/development, merged into your branch).
That being said, ~1 (first ancestor) would probably work too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top