Reputation: 43
Let's begin by saying that the gerrit project is incorrectly configured as it allowed what happened
Instead of pushing to refs/for/my_branch, I pushed directly to my branch, i.e.
git push
Instead of ending up in the gerrit machinery, the commit ended up at the top of my branch, bypassing all the review whatnot. I tried fixing the issue by:
git push -f origin <previous_commit_hash>:refs/head/my_branch
I hoped it'd bring the branch to it's previous state, so I could push again, this time correctly. This action had only the effect of marking the previous commit with a green label saying heads/my_branch.
Next, I tried a hard reset locally and then pushing directly to gerrit:
git reset --hard <previous_commit_hash>
git push -f origin my_branch
The push got rejected, because "Updates were rejected because the tip of your current branch is behind its remote counterpart..."
The commit is just a csv file with content for a help tab, so it rather won't break anything per se, but I don't want to have a messy history or other unintended consequences. Preferably, I'd like to bring my remote branch to it's original state and resubmit my changes, this time correctly. Since this happened, nobody has pushed to or pulled from this branch. What's the best way to proceed?
Upvotes: 0
Views: 3727
Reputation: 22321
The problem happened because you execute:
git push -f origin <previous_commit_hash>:refs/head/my_branch
But the correct is to execute:
git push -f origin <previous_commit_hash>:refs/heads/my_branch
With "heads" instead of "head".
If you use "refs/heads/my_branch" Gerrit understands that you want to push straight to the branch "my_branch". When you used "refs/head/my_branch" Gerrit understood that you would like to create a new branch named "head/my_branch".
Execute the correct command to fix the issue and remove the "head/my_branch" using the Gerrit UI.
Upvotes: 1
Reputation: 39824
You could use git revert
to create a new commit (reverting the change) and git push
it just as you did with the accidental commit. This will bring the branch HEAD to the same functional state as it was before, but with a different SHA. After which you should immediately fix the repository configuration that allowed such direct pushes.
The history will still show both commits but IMHO it's a better/safer approach than re-writing the origin's history (which bears the risk of not being properly picked up by local repositories).
You may need to pay attention not to pick up just one of the commits and the different SHA may temporarily cause some problems - tools (like gerrit) may complain that local repositories are not up to date and require merges to pick up the latest origin branch changes.
Upvotes: 0