Search code examples
gitbranchrebasesquash

How do I make a copy of a branch for testing rebase?


I've done this before by creating a clone and doing the rebase in the clone, but I suspect I could do it safely on a separate branch.

I have a feature branch feat-x with about 25 commits on it. I'd like to (safely) squash several of these together.

(I say "safely" because the first couple of times I squashed I didn't get it right -- but was working in a clone so just threw it away until I figured out the right incantation.)

What sequence of commands will give me feat-x-exp that is a copy of feat-x so I can experiment with squashing without disturbing feat-x?


Solution

  • I think you want to just do:

    git checkout -b feat-x-exp feat-x
    

    This checks out a copy of feat-x that you can experiment with. Once you've moved off the feat-x branch, you rebase commands won't affect the original branch (so long as you avoid the two argument form that's a shortcut for checking out before a rebase).

    If you mess up, you can go back to the state of the original branch with:

    git reset --hard feat-x
    

    If your experiment is successful, you can move feat-x to match feat-x-exp and remove the experimental branch:

    git checkout feat-x
    git reset --hard feat-x-exp
    git branch -d feat-x-exp