Getting into rebase -> merge cycle with Git

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 !!

  • How to synchronize two branches in the same Git repository?
  • How to Reorder Commits (rebase) with TortoiseGit
  • Git rebase a branch onto master failed, how to resolve?
  • How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?
  • How do I squash two non-consecutive commits?
  • How do I apply autostash after an aborted rebase?
  • How to rewrite git history to match a popular git workflow
  • Reapply Git commits from copied fork repository to original repository
  • One Solution collect form web for “Getting into rebase -> merge cycle with Git”

    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.

    Git Baby is a git and github fan, let's start git clone.