Reputation: 25
Someone gave me a repo to work on. I assumed it's a company repo and I hapilly started creating branches , then finally I merged them into a single one , deleted the temporary branches and pushed. Today he messaged me that is his personal repo, so it would be nice if possible for me to remove my mess from his repo. I saved my work and moved it to a new repo but I have no idea how to remove the commits so I don't "stain" his history with my name :D
git reset --hard hislastcommitsha
does not work. It moves the head but the commits are still in place. I can even check them out. I want them gone like they never existed.
I assume they are not removed because even though there is only one branch (master) those commits come from previous merging of multiple branches (development and topic branches).
Is there anything I can do to remove my commits/name from his history ?
Much appreciated
Upvotes: 1
Views: 73
Reputation: 21998
(Base assumption : you have enough rights on the repo to push with --force
because in the contrary, your friend asking you to clean anything would have been an unsatisfiable demand)
When you pushed the branch (master
), git in fact pushed any new commits that were reachable through ancestry, begining from the commit where your new ref master
was pointing to at the moment.
But when you initially cloned the original repo, its own version of master
was pointing at the commit we're trying to restore on top, hislastcommitsha
So you need to
# as you already guessed, rewind your version of master
git reset --hard hislastcommitsha
# then push it to his repo
git push --force origin master
However, important note : at this point, the new commits you (wrongly) pushed are not deleted from his repo, they're just unreferenced by any branch, so they're now candidates for garbage collection.
This is the reason you still can check them out in your local repo.
If for any reason your friend doesn't want to wait for typical garbage collection time, he has the option to trigger it manually with git gc
and whatever parameters would fit your situation, that's up to him.
Upvotes: 1