Supose you have the following scenario:
* 1bd2200 (HEAD, master) another commit
* d258546 bad commit
* 0f1efa9 3rd commit
* bd8aa13 2nd commit
* 34c4f95 1st commit
Where you want to remove d258546 i.e. "bad commit".
You shall try an interactive rebase to remove it: git rebase -i 34c4f95
then your default editor will pop with something like this:
pick bd8aa13 2nd commit
pick 0f1efa9 3rd commit
pick d258546 bad commit
pick 1bd2200 another commit
# Rebase 34c4f95..1bd2200 onto 34c4f95
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
just remove the line with the commit you want to strip and save+exit the editor:
pick bd8aa13 2nd commit
pick 0f1efa9 3rd commit
pick 1bd2200 another commit
...
git will proceed to remove this commit from your history leaving something like this (mind the hash change in the commits descendant from the removed commit):
* 34fa994 (HEAD, master) another commit
* 0f1efa9 3rd commit
* bd8aa13 2nd commit
* 34c4f95 1st commit
Now, since I suppose that you already pushed the bad commit to gitlab, you'll need to repush your graph to the repository (but with the -f
option to prevent it from being rejected due to a non fastforwardeable history i.e. git push -f <your remote> <your branch>
)
Please be extra careful and make sure that none coworker is already using the history containing the "bad commit" in their branches.
Alternative option:
Instead of rewrite the history, you may simply create a new commit which negates the changes introduced by your bad commit, to do this just type git revert <your bad commit hash>
. This option is maybe not as clean, but is far more safe (in case you are not fully aware of what are you doing with an interactive rebase).