-1

I have a list of 4 commits, already pushed on gitlab. For some weird reasons, the first commit comes from someone else and shall be removed. How todo it safely? I need the branch to start at code v1 (hash 913548f53bea1cf43efa715cc207a5de593e0aa0)

The git log looks like this:

code v3 (most recent code)
739570b254d8cd30a3c8141fa797149a0da1f8fa 

code v2
b37cde6732da175c9e881ddf69ffc9a58bcbb22f 

code v1
913548f53bea1cf43efa715cc207a5de593e0aa0 

// all these commits need to be removed
code v0
0ddc5f398a4578e39cba197bc6b20e973c1f8a87 
98af45c8c781e7c10fb8156684cb42ed9d385de3 
036b4859a955142f0ab684fb17d9f3f4ef84b679 
78c707dc4de10ac82e202d7c3a4911093c9854af 
955e7885c9985e59406dfa5ec7675f1ea8ffbf2e 
40fb8a8a9638acf69b38cb305d74279515b84c15
864118fd08bd9259f24fd8a2dfd26d849885c873 
4c8f26e712d185ac5a6ef472442517456195312b 
901a36ada5b573a76eee453e685d808434a67690 
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
  • Check this [answer](https://stackoverflow.com/questions/3293531/how-to-permanently-remove-few-commits-from-remote-branch/41726152) here. You might have to checkout to the desired commit and after that `git push -f` – samlima Mar 05 '21 at 16:09
  • Side note (and minor): the output from `git log` is not a "code snippet" because it's not something you can run. If you're using the text editor, don't add the ` – torek Mar 06 '21 at 03:35

1 Answers1

1

Destructive

Removing commits is rewriting history - which is frowned upon, since it causes desyncs between remotes looking at the same repository.

If you have force push rights, you can rewrite the branch locally and then push it.

git rebase -i <commit-id> is very useful for this. You will get an editor where you can see each commit and edit the first word on the line to "pick" or "drop" (and some other choices not relevant to this question). Save & Exit the editor to apply the rebase operations. Depending on the changes in the dropped commits you may get conflicts, which you resolve in your editor as normal, then run git rebase --continue.

Finally force push with git push -f.

If the commits you "drop" from your branch is part of another branch (such as main) they will not be affected.

If this is the very start of the git history, you can use --root instead of <commit-id> to get ALL commits in this tree.

Non-destructive

The "safe" option is to git revert each commit you do not wish to have, this will undo the changes, but the commits will be there - and there will be no breakage of other remotes (which may or may not have branches pointing to the undone commits).

Beware that this can still have unintended consequences when merging later, since that content was removed.

Peter Lindsten
  • 539
  • 4
  • 8