While writing the question, and finding What is the difference between "git branch" and "git checkout -b"? in the list of similar questions, I found the answer myself:
$ git checkout -b new_branch_name
I guess I was reading the man page for the wrong command, I was expecting this as part of the branch command, not for checkout. Quoting the man page for checkout:
Specifying
-bcauses a new branch to be created as ifgit-branch(1)were called and then checked out.
Just what I was looking for.
Answer from MvG on Stack OverflowWhile writing the question, and finding What is the difference between "git branch" and "git checkout -b"? in the list of similar questions, I found the answer myself:
$ git checkout -b new_branch_name
I guess I was reading the man page for the wrong command, I was expecting this as part of the branch command, not for checkout. Quoting the man page for checkout:
Specifying
-bcauses a new branch to be created as ifgit-branch(1)were called and then checked out.
Just what I was looking for.
Git introduced switch in version 2.23 to handle changing of branches specifically and avoid the use of checkout which can be confusing by the sheer amount of operations it can do.
Among other possibilites,
git switch <branch> # to switch to an existing branch
git switch -c <new_branch> # to create a new branch and switch to it
fetch and checkout a remote git branch in just one command - Stack Overflow
git clone and checkout in a single command - Stack Overflow
git checkout and merge in one command - Stack Overflow
How do i create a Branch using the terminal in VS Code?
git clone u://r/l --branch x
still clones everything but sets the local HEAD to that branch so it's the one checked out.
Source:
--branch <name>
-b <name>
Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD, point to<name>branch instead. In a non-bare repository, this is the branch that will be checked out.--branchcan also take tags and detaches the HEAD at that commit in the resulting repository.
Is your problem the checkout being to large or the repository itself?
As git clone, well, clones a repository you usually get the whole repository in its full size. (unless you are doing a shallow clone as you already suggested.)
If it's really about the checkout of the wrong branch git help clone says:
--no-checkout, -n
No checkout of HEAD is performed after the clone is complete.
After cloning with -n you can manually check out
If your branch names are generally long, and you constantly merge the branch you just checkout from, you can use:
checking out from branch you want to merge
git checkout branch
git merge -
the '-' is shorthand for the previous branch, very handy for quick checking out and merging
You have several options:
- Script
Write a script which execute your commands git alias
Write it in an alias or function inside your.gitconfigfileUse & operator
git checkout master && git merge branch & ... & ...