I think you want:
git checkout -B <branch-name>
To be clear, remember that git checkout
never makes any new commits, but it can create, or reset, a branch name. That is, suppose you have:
C--D--E <-- branch1 (HEAD)
/
...--A--B
\
F--G--H <-- branch2
You can make the name branch1
point to commit H
, and in effect check out commit H
, now with the command:
git reset --hard branch2
which discards your current work-tree and index contents by replacing them with those from commit H
and gives you:
C--D--E [abandoned]
/
...--A--B
\
F--G--H <-- branch1 (HEAD), branch2
But I think you instead want to make the name branch2
point to existing commit E
, abandoning commits F-G-H
, and also attach HEAD
to the name branch2
. That's what:
git checkout -B branch2
will do, resulting in:
C--D--E <-- branch1, branch2 (HEAD)
/
...--A--B
\
F--G--H [abandoned]
This operation will leave your index and work-tree undisturbed, so that if you have changed various files to not match the contents stored in commit E
, they remain changed in your work-tree (and if changed in the index, they remain changed in the index as well).