Home > database >  git use only from specific range of commit
git use only from specific range of commit

Time:09-30

I have a branch with several commits, example:

commit40
commit39
commit38
...
commit02
commit01

How could I make a new branch using the previous one with a range of commits? let's say since commit 38 to 40

CodePudding user response:

You can use cherry-pick technique. https://www.atlassian.com/git/tutorials/cherry-pick

CodePudding user response:

  1. checkout a new branch from commit37

    git checkout -b <new_branch_name> <commit37>

  2. delete all content, and commit it.

    rf -rm ./*; git add . ; git commit -m "cleanup";

  3. cherry-pick commits from 38-40

    git cherry-pick commit37..commit40

in the cherry pick command commit37 is not included.

CodePudding user response:

Based on a comment from OP, which includes this sentence:

There was a "merge with" in between 38 and 30 (example) that added a lot of code that I don't need in the new branch...

I think what might work best for you is to have a branch that has commits 1-30, and then 38-40. You can instead do a similar process without having any of the previous commits 1-38, however, the closer you can get to the state you want, the better, so it's best to "skip" the fewest commits possible in order to avoid future conflicts. Given that, here's how to simply omit commits 31-37:

git fetch
git switch -c commit30
git merge commit37 --strategy=ours # this "skips" commits 31-37
git merge commit40 -X theirs

Adding -X theirs to the last merge lessens the chances of having conflicts, but you still may have them if files were added or deleted, and also edited. At least you'll only have them one time, rather than on a per commit basis (such as with rebase or cherry-picking a range).

  •  Tags:  
  • git
  • Related