Skip to main content
Added inline code formatting, added syntax highlighting, removed horizontal scrollbar.
Source Link
user456814
user456814

'A'A was the initial commit, but now you want 'B'B to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp <sha1_for_B> # remove the temporary tag git tag -d tmp 
# Go back to the last commit that we want # to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp <sha1_for_B> # remove the temporary tag git tag -d tmp 

'A' was the initial commit, but now you want 'B' to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp <sha1_for_B> # remove the temporary tag git tag -d tmp 

A was the initial commit, but now you want B to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want # to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp <sha1_for_B> # remove the temporary tag git tag -d tmp 
added 17 characters in body
Source Link
Lara Bailey
  • 801.6k
  • 110
  • 650
  • 667

'A' was the initial commit, but now you want 'B' to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp B<sha1_for_B> # remove the temporary tag git tag -d tmp 

'A' was the initial commit, but now you want 'B' to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp B # remove the temporary tag git tag -d tmp 

'A' was the initial commit, but now you want 'B' to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp <sha1_for_B> # remove the temporary tag git tag -d tmp 
Source Link
Lara Bailey
  • 801.6k
  • 110
  • 650
  • 667

'A' was the initial commit, but now you want 'B' to be the initial commit. git commits are whole trees, not diffs even if they are normally described and viewed in terms of the diff that they introduce.

This recipe works even if there are multiple commits between A and B, and B and C.

# Go back to the last commit that we want to form the initial commit (detach HEAD) git checkout <sha1_for_B> # reset the branch pointer to the initial commit, # but leaving the index and working tree intact. git reset --soft <sha1_for_A> # amend the initial tree using the tree from 'B' git commit --amend # temporarily tag this new initial commit # (or you could remember the new commit sha1 manually) git tag tmp # go back to the original branch (assume master for this example) git checkout master # Replay all the commits after B onto the new initial commit git rebase --onto tmp B # remove the temporary tag git tag -d tmp