0

I've got a got commit history that looks like this:

enter image description here

As shown, a branch (blue) was created from master (red) and later merged back into master. The master branch had not changed at all.

Is it possible retroactively to flatten this loop into a linear sequence of 5 non-squashed commits?

I guess if the last merge commit was done as a git rebase of master that would have given me the linear sequence.

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137

2 Answers2

1

To keep a flat git history, you should use rebase instead of merge when you want to update branches. You could see an explanation of rebase versus merge in another post.

From your current state, if your HEAD (master) is the merge, you could do

  • Clean your working dir (or stash changes)

  • git reset --hard LastCommit , where LastCommit is the last commit from red branch which is not on the blue one.

  • git rebase blueBranch redBranch

git-rebase - Reapply commits on top of another base tip

Community
  • 1
  • 1
Flows
  • 3,675
  • 3
  • 28
  • 52
  • But presumably, `blueBranch` does not contain the final merge commit that merge it into `master`. That commit (i.e. the HEAD) is part of the `master`'s commits. – Adi Shavit Jun 23 '16 at 13:00
  • @AdiShavit That merge commit would not be needed when you do a rebase. Is that what you mean? – Brian J Jun 23 '16 at 13:09
  • 2
    General warning, only rebase if your commits are all local. If you have already pushed those commits, rebase can lead to all kinds of problems. – Brian J Jun 23 '16 at 13:10
  • Not necessarily. If it was fast-fwd, then it should automatically appear as a linear history. Although I suspect GitHub PRs always appear as actual commits even when fast-fwd was possible (making your comment about local changes even more relevant). – Adi Shavit Jun 23 '16 at 13:10
  • The last commit of `blueBranch` doesn't need to be a merge commit. – Flows Jun 23 '16 at 13:19
0

In this case, git reset HEAD^2 --hard is enough.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53