问题
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.
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:
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:
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.
回答1:
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
来源:https://stackoverflow.com/questions/61239912/git-reset-and-or-delete-commit-on-remote-branch