Reputation: 88678
I want to create a branch foo
(and have it checked out) that'll start on the same commit as origin/bar
, but I don't currently have origin/bar
checked out. Is there a one-liner that does this?
Upvotes: 15
Views: 6345
Reputation: 142412
Git has a flag for creating such a branch:
https://git-scm.com/docs/git-checkout/
--orphan
Create a new orphan branch, named , started from and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.The index and the working tree are adjusted as if you had previously run
git checkout <start_point>
.This allows you to start a new history that records a set of paths similar to by easily running
git commit -a
to make the root commit.This can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is
clean
, but whose full history contains proprietary or otherwise encumbered bits of code.If you want to start a disconnected history that records a set of paths that is totally different from the one of , then you should clear the index and the working tree right after creating the orphan branch by running
git rm -rf .
from the top level of the working tree. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc.
Upvotes: -1
Reputation: 56637
It depends whether you want your new branch to track the remote origin/bar
.
To track the remote:
git checkout -b foo origin/bar
To not track the remote:
git checkout -b foo --no-track origin/bar
(You can always add a tracking relationship later.)
Upvotes: 3
Reputation:
You can do it in a single command: git checkout
allows git branch
's --no-track
option to be specified directly.
git checkout -b foo --no-track origin/bar
Upvotes: 15
Reputation: 14583
This should do the trick:
git branch foo origin/bar
git checkout foo
And if you insist on oneline:
git checkout -b foo origin/bar
Upvotes: 1