0

I have a small issue regarding git. At the beginning of the project i have clean flow (figure 1 and 2). Figure 1 is from Intellij, while figure 2 is after running gitk & command. As expected we see same states.

This image is from Intellij This image is from gitk

But after merging "branch3" with "master", we realize that it is not good and want to delete that commit. We execute following command: git reset --hard HEAD~1. As expected, after executing this command we are at following state:

This image is from gitk after git reset

And from that state we create new branch "branch4". Till this point everything is working as expected. But after we push "branch4" and merge it with "master" branch we receive following issue:

This image is from IntellijThis image is from gitk

As you can see we still have "Commit on branch3" and "Merged in branch 3". Please advise me how can i delete those commits, as I don't want to have any record of these commits (to be similar like the first image). Because, obviously git reset --hard does not delete remote commits. I would like to emphasize that pushing was done using following command git push --force

What command should I use instead?

P.S. With git reset --soft I got the same.

  • Your pull request 15 that merged branch3 was already pushed to the remote. So resetting locally will only move the local branch. If you merge that again with the remote branch, of course you are adding those commits you previously removed. – poke Apr 15 '20 at 23:16

1 Answers1

2

Get your local commit history looking exactly like you want it. Then force push (push --force or push -f) each branch to the remote. This will rewrite history on the remote for each branch you force push to look exactly as it does locally.

If your remote git server or repo is configured to reject history rewriting this will fail. You will have to change that configuration. (GitHub doesn't do this by default, but GitLab does. I don't know about bitbucket)

After you've gotten the branches you're trying to fix looking right locally and on the remote, you may want to cleanup traces of the abandoned branch (your branch3). See How do I delete a Git branch locally and remotely?

⚠️ Be aware of the caveats about rewriting the history of shared branches:

  • If anyone has changes on their clones of the branches you are about to force push, it can get difficult/messy to merge them in.
  • If anyone has committed changes to the remote repo, your force push will overwrite them. You might want to consider git push --force-with-lease
Inigo
  • 12,186
  • 5
  • 41
  • 70
  • `git push --force origin branch4` does not work. I again end with the same image (messy commit history). Tried both in github and bitbucket – Aleksandar Pluskoski Apr 16 '20 at 07:10
  • 1
    @AleksandarPluskoski you didn't quite follow my instructions. You need to force push ALL of the branches for which you have rewritten history, and that includes `master`. Again, get everything the way you want it to look locally, and then checkout each branch locally and force push it to your remote. After history for `master` on your remote looks right, you may want to cleanup traces of `branch3` locally and on the remote. I'll update the answer with that. – Inigo Apr 16 '20 at 07:36