Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebase this branch?

Tags:

git

git-rebase

The image below consists of two parts: the first part is the repository sample model, the second part is the repository state that I want to get using the git rebase.

I didn't forget anything in the second part. This is exactly what I want.

enter image description here

like image 977
Wynell Avatar asked Dec 23 '22 18:12

Wynell


2 Answers

If the number of commits you want to manipulate is as small as in the picture : use cherry-pick

git checkout tmp1
# make sure you have a clean working directory before running reset --hard :
git reset --hard <e>
git cherry-pick <h> <i>

git rebase has a syntax to select a range of commits :

git checkout tmp1
# you need to mention <g>, the *parent* of the first commit you
# want to replay, not <h>
git rebase --onto <e> <g> tmp1

git checkout tmp2
git reset --hard <k>
git rebase --onto <e> <g> tmp2

a note about git reset --hard :

git reset --hard <target> is one of the few dangerous git commands, that can delete modifications from your disk without saving them somewhere first.

It sets the active commit to <target>, and discards any modifications on all tracked files (that's the dangerous part).

You should at least know the effects before using it.

Some easy ways to avoid loosing your un-committed work are :

  • git stash your work before using git reset --hard,
  • commit your work before using it (even after a git reset, the commit will still be available through git reflog),
like image 115
LeGEC Avatar answered Jan 04 '23 19:01

LeGEC


In that situation I'd choose to just rebuild branches rather than wandering into ninja rebase attempts :

git checkout -b new-tmp1 master
git cherry-pick h i
git checkout -b new-tmp2 master
git cherry-pick j k

Then if new-tmp1 and new-tmp2 suit your needs, just move old refs :

git branch -f tmp1 new-tmp1
git branch -f tmp2 new-tmp2
like image 45
Romain Valeri Avatar answered Jan 04 '23 18:01

Romain Valeri