While helping a friend with a git problem today, I had to introduce a
branch that needed to be totally separate from the master
branch.
The contents of this branch really had a different origin from what
had been developed on the master
branch, but they were going to be
merged into the master
branch at a later time.
I remembered from reading John Wiegley's Git from the bottom up how branches are essentially a label to a commit that follows a certain convention and how a commit is tied to a tree of files and, optionally to parent commits. We went to create a parentless commit to the existing repository using git's plumbing:
So we got rid of all files in the index ...
$ git rm -rf .
... extracted directories and files from a tarball, added those to the index ...
$ git add .
... and created a tree object ...
$ git write-tree
(git-write-tree
told us the sha1sum of the created tree object.)
Then, We committed the tree, without specifying parent commits...
$ echo "Imported project foo" | git commit-tree $TREE
(git-commit-tree
told us the sha1sum of the created commit object.)
... and created a new branch that points to our newly created commit.
$ git update-ref refs/heads/other-branch $COMMIT
Finally, we returned to the master
branch to continue work there.
$ git checkout -f master
This seems to have worked as planned. But this is clearly not the kind of procedure I would recommend to someone who is just getting started using git, to put it mildly. Is there an easier way of creating a new branch that is entirely unrelated to everything that has happened in the repository so far?
To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.
An orphan branch is a separate branch that starts with a different root commit. So the first commit in this branch will be the root of this branch without having any history. It can be accomplished by using the Git checkout command with the ––orphan option.
There is a new feature (since V1.7.2) which makes this task a little more high-level than what's in any of the other answers.
git checkout
now supports the --orphan
option. From the man page:
git checkout [-q] [-f] [-m] --orphan <new_branch> [<start_point>]
Create a new orphan branch, named <new_branch>, started from <start_point> 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.
This doesn't do exactly what the asker wanted, because it populates the index and the working tree from <start_point>
(since this is, after all, a checkout command). The only other action necessary is to remove any unwanted items from the working tree and index. Unfortunately, git reset --hard
doesn't work, but git rm -rf .
can be used instead (I believe this is equivalent to rm .git/index; git clean -fdx
given in other answers).
In summary:
git checkout --orphan newbranch
git rm -rf .
<do work>
git add your files
git commit -m 'Initial commit'
I left <start_point>
unspecified because it defaults to HEAD, and we don't really care anyway. This sequence does essentially the same thing as the command sequence in Artem's answer, just without resorting to scary plumbing commands.
From Git Community Book:
git symbolic-ref HEAD refs/heads/newbranch
rm .git/index
git clean -fdx
<do work>
git add your files
git commit -m 'Initial commit'
Although the solution with git symbolic-ref
and removing index works, it might be conceptually cleaner to create new repository
$ cd /path/to/unrelated
$ git init
[edit and add files]
$ git add .
$ git commit -m "Initial commit of unrelated"
[master (root-commit) 2a665f6] Initial commit of unrelated
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
then fetch from it
$ cd /path/to/repo
$ git fetch /path/to/unrelated master:unrelated-branch
warning: no common commits
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
From /path/to/unrelated
* [new branch] master -> unrelated-branch
Now you can delete /path/to/unrelated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With