Daniel Viglione
Daniel Viglione

Reputation: 9457

Sync local git branch with remote git branch that deleted a specific commit

I share a remote git repository. A user actually removed a specific commit that they did not want in the git history (the commit is not the latest commit). Perhaps they did something like this:

git reset --hard <sha1-commit-id>
git push origin HEAD --force

I am not 100 percent if these are the commands they used. All I know is the commit is no longer in the remote repository. I have the commit in my local git repository though. And it is not HEAD (the latest commit). It is like 10 commits back in git log output:

git log
...
commit a5df5
Author:
Date:
This is the commit that I have locally but no longer in remote

Now whenever I do a git push, it reappears back in the remote repository. How can I ensure this commit no longer is pushed to the remote repository?

I am thinking to do this:

git fetch origin latest_stuff
git reset --hard origin/latest_stuff

Will this ensure that specific git commit is gone from my local machine?

Upvotes: 1

Views: 436

Answers (1)

eftshift0
eftshift0

Reputation: 30267

You have to be extremely careful to make other people aware of the situation so that things like this don't happen.

So... how can it be fixed? It can be corrected on your side, actually. So... say the commit that you want to be removed has ID xxxxxxxx, then:

git checkout xxxxxxxx~1 # we go back one commit from the one we want to discard from history
git cherry-pick xxxxxxxx..the-branch # replay the history of the branch _after_ the revision you want to delete
git branch -f the-branch # set pointer of branch to new location
git push origin -f the-branch

That way the revision has been removed from history... it has implications if there are yet more developers working on the project (because they would have the revision you want to delete on their branches). Make sure that all developers are aware and rebase their work if needed before trying to push on the "new" clean branch.

Upvotes: 1

Related Questions