Fixing a branch of a branch

I mistakenly created a branch from another branch rather than creating it from the master branch. My source branch had 2 commits that I wanted to roll back. Those commits were never merged into the master branch. I reverted the two commits from the source branch. I am trying to do the same from my secondary branch, but when I view those commits on the web site, the revert option is not showing. Is there a way I can do this?

Hi @JavaUser

You can do this from the command line in a couple of ways. If you want to just “delete” those two commits, you need to:

  1. clone the repo
  2. and checkout the branch where you want to revert the two commits
  3. then run git rebase -i HEAD~2. The 2 there is the two commits you want to change
  4. that should open up your default your default editor with a “TODO list” which will look a bit like this:
pick abc1234 Commit message here...
pick def5678 Commit message here...

# Rebase abc1234..def5678 onto abc1234 (2 commands)
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
# 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

You want to drop those two commits, so change the pick next to the commit hashes at the top, to drop, then save this file and exit your editor.

Git will automatically execute your two drop commands.

You then need to push this new history to GitLab. Because you have changed the history, you need to push with force:

git push -f MY_BRANCH_NAME

This worked. I forgot to mention that I had another commit after the two I wanted to delete. I adjusted the commands, and it fixed my problem. Thank you.