Delete commit on gitlab

  1. git reset --hard CommitId
  2. git push -f origin master

1st command will rest your head to commitid and 2nd command will delete all commit after that commit id on master branch.

Note: Don't forget to add -f in push otherwise it will be rejected.


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).


We've had similar problem and it was not enough to only remove commit and force push to GitLab.
It was still available in GitLab interface using url:

https://gitlab.example.com/<group>/<project>/commit/<commit hash>

We've had to remove project from GitLab and recreate it to get rid of this commit in GitLab UI.


I will assume that you want to edit all commits to remove some strings from them, like for example leaked credentials.

Really purging old commits from a Gitlab server is a tricky task, as mentioned in the Gitlab purge doc, because it involves deleting all internal refs from the server. This is possible with Gitlab versions above or equal to 11.6 (see said doc).

These are the main steps :

  • install git-filter-repo
  • download all git references from Gitlab by exporting the project
  • then loop these steps :
    • update the repository contents with git filter-repo
    • upload the modified contents
    • cleanup dangling references When this is done, the Gitlab project will be purged of all references to those old commits.

a word of warning

Beware that any local copy of the repo made before this will still contain the deleted commits. Also, users trying to pull from those old local copies will end up in a mess.

the detailed process

  • install git-filter-repo
  • export the project from Gitlab following the Gitlab export doc: go to Settings->Advanced, click the Export project button, follow the link you receive by email to download the export
  • create a temporary folder and jump to it : mkdir tmp && cd tmp
  • unpack the exported archive : tar xf path/to/downloaded...export.tar.gz
  • clone the project bundle : git clone --bare --mirror export.bundle
  • copy all expressions that you want changed to expressions.txt

Example content :

a_sample_password==>DELETED
  • jump to the cloned repo : cd project.git
  • update the origin remote : git remote set-url origin YOUR_REMOTE_ORIGIN
  • run git filter-repo --replace-text ../expressions.txt
  • go to Gitlab Settings->Repository->Protected and unprotect any protected branches
  • upload the results with git push origin --force 'refs/heads/*'
  • remove dangling commits with git push origin --force 'refs/replace/*'
  • wait 30 minutes
  • in Gitlab Settings->Repository, run Repository Cleanup with the file filter-repo/commit-map
  • re-protect the unprotected branches in Gitlab Settings->Repository->Protected

See more examples at git-filter-repo examples. Gitlab cleanup details are at Gitlab docs on repo cleanup. I made a sample repository to illustrate the process.

Tags:

Git

Gitlab