Fundamentally, git checkout -b
and git rebase
do two entirely different things:
git checkout -b
(or git switch -c
) creates a new branch name (and makes this the current branch, with all that this entails—in some cases that's trivial and in others it is not).
git rebase
copies some set of commits, and then moves an existing branch name. The set may be empty, in which case the branch name move happens as if with git reset --hard
, except that Git makes sure the the index and working tree are "clean" first. (There is some complication that can occur here with autostash though.)
This means that it makes little sense to compare them.
Once we know, however, that a branch name simply holds the hash ID of some particular commit, we can see that in some specific cases, no commits are copied and therefore git rebase
acts much more like git reset --hard
. So even though git reset --hard
and git rebase
also do very different things, we can compare these two, in some very special cases.
To this, we can add the fact that git checkout -B
—note that the B
option here is in uppercase, not lowercase—can act like git reset --hard
. So now we do have some situations in which the two can be compared. But this is for -B
, not for -b
.
Finally, we can note that if our git rebase
is only going to do the "move the branch" step (the equivalent of git reset --hard
), and we already have the branch name pointing to the place we'd like it to move to, then the entire git rebase
operation itself is a no-op.
In your case, you've decided not to replace just a git checkout -b
with git rebase
, but rather to replace a two-command sequence:
git checkout name1
git checkout -b name2
with a different two-command sequence:
git checkout -b name2
git rebase name1
When the second command acts like git reset --hard name1
only, these two sequences produce the same end result. But there's a single command that would produce the right end result, which is probably the one you should use:
git checkout -b name2 name1
This is short and sweet and does what you want all in one step.