Edit: Thank you all for the answers. Just forcing an overwrite of remote files is what I knew would happen with git push --force but I was more caught up with not preserving the commit history of main (Again, I wanted the commit history rewritten - not necessarily have new files overwrite remote files) - but after reading through the answer, I realize what I was forgetting how the force push would overwrite the remote with my local branch, including the commit history
This all started when I noticed someone did a merge commit in the history instead of rebasing like they were supposed to do
I followed this previously answered question to fix this, that is, rewrite the commit history in a way that a rebase was done instead of a merge commit. This rewrite was just done on a local branch and I confirmed the code still builds/works as expected, and the HEAD of both my branch and main match. The only difference is the actual commit objects from commit c onwards (See picture below) - but I understand why this creates a problem, I essentially have an entirely different history on my branch now
The question is: what do I want to do now? Since I rewrote the commit history, my local branch has diverged from origin/main, which is expected. In my head I want to just take my branch and force push to main such that main has the new history that I wrote on this branch
I understand rewriting history this way is probably strongly discouraged - but is there a proper way to handle something like this if there is a desire to cleanup the history of main to not have any merge commits?
Steps I took to get here:
- The
mainbranch of a local repository had a history that looked like this:
a b d e f g h i j k
o---o---o---o---o---o---o---o---o---o
\---o---/
c
Where HEAD was at k and the merge commit is e - the goal was to rebase c on top of d and delete e entirely
- Checked out a local branch from
mainsuch that it mirrored the above commit history - On my local branch, rebased back to
asince that is where the divergence happened
$ git rebase -i <sha of a>
- In the interactive rebase, selected
cfor editing and droppedeentirely - Resolved merge conflicts and stepped through the
git rebase --continuecommands until my local branch looked like the following and I was back atHEAD. Since the rebasing rewrites the history, commits aftercare different objects entirely but don't actually have any different content than what is currently in the remote repository
a b d c l m n o p q
o---o---o---o---o---o---o---o---o---o
- Observe the following with
git status
$ git status
On branch cleanup
Your branch and 'origin/main' have diverged,
and have 6 and 7 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
Which again, is expected. My concern here is with how to continue, that is, force update the main branch to have the cleaner history I now have on my local branch