You first need to create a new branch locally. By default, this will use your current HEAD as a base.
Creating a branch without history
However, you can also create a new branch without any history using git checkout --orphan
:
# create a new branch without a history and check it out
git checkout --orphan yournewbranch
# edit your files
# create a commit with these files
git add .
git commit
# push that commit and create the remote branch
git push -u your_remote yournewbranch
Alternatively, you can just create a new repository with that branch and push that:
git init -b yournewbranch
git add .
git commit
git remote add origin https://yourgitserver.com/your/repo
git push -u origin yournewbranch
Add branch from other repository locally
If you already have the remote branch and want to add it to your repository, you can just checkit out using git checkout
:
git checkout -b yournewlocalbranch remotes/yourremote/remotebranchname
This assumes the new branch exists on the remote yourremote
with the name remotebranchname
and you want the branch to be named yournewlocalbranch