Question

I have a branch mainline tracking remote branch origin/mainline

My local branch went out of sync with the remote branch so I did a git pull and now when i do a git status I get:

Your branch is ahead of 'origin/mainline' by 2 commits.

Now i want to squansh these into a single commit so i ran

git rebase -i HEAD~2

It opens up the file where I can squash the commit and pick one to use. I was expecting it to show just 2 commits (my original top level commit + the merge after pull ) but it has 3 commits in them with no merge commit. If i go ahead and sqash these into single one Git complains that I have a conflict, ( the same conflict i had after a pull).

If i resolve the conflict and run git rebase --continue it end up in the state where I have 2 commits!! Please help me resolve this, im stck here for almost a day !!

Was it helpful?

Solution

Rebase doesn't normally preserve merges, you have to pass the -p or --preserve-merges flag to get it to do that.

However, your process for synchronizing with the remote is incorrect, and might lead you to rewriting commits that have already been pushed and shared with other people, which can lead to a lot of problems with synchronization and conflicts.

If you wanted to do a squash, then instead of doing a pull and a merge, you should have fetched and rebased instead:

git fetch origin
git rebase origin/mainline mainline
git rebase -i mainline~2

You can combine the last two commands if you want, but doing them separately has the advantage that you can then verify that you didn't mess anything up with the squash by doing git diff mainline@{1} afterwards.

So, to get out of your current situation, you could do a hard reset back to mainline^, or whichever commit you were at before you tried to do the merge/pull, then run the rebase commands above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top