Creating a new empty branch for a new project
Asked Answered
C

11

434

We are using a git repository to store our project. We have our branches departing from the original branch. But now we want to create a small new project to track some documentation. For that we would want to create a new empty branch to start storing our files, and I would want other users of the network to clone that branch.

How can we do that?

I tried some things, but they didn't work.

$ mkdir proj_doc; cd proj_doc
$ git init
$ git add .
$ git commit -m 'first commit'
$ git br proj_doc
$ git co proj_doc
$ git br -d master
$ git push origin proj_doc

It seems to push the branch ok, but when I do a fetch or pull, it downloads information from other branches, and then I also get some extra files from other projects. What's the best solution?

Cotta answered 20/12, 2012 at 9:34 Comment(6)
Why do you need to store this in a branch? Branches are usually for some deviations from the same code base. Maybe just starting a new repository would be a better solution.Haematosis
Well, we did it before, IIRC, and I would like to do it again, so I'm curious ;)Cotta
"for a new project" - As @honk I would suggest to put this in a new repository. Two options from there to integrate them. Make it a submodule in the original project, e.g. docs/ pointing to that other repo. Or, if you want to merge the code later, add it as a remote.Designate
One additional downside of the orphan approach is that you need to keep your .gitignore'd files, and also constantly switch between the two roots (branches). So I'm also for the new repo approach, in a new folder, having the same remotes, and pushing to another branch.Develop
possible duplicate of In git, is there a simple way of introducing an unrelated branch to a repository?Rozanneroze
we needed a pull request to pull in an older code branch than our repo had a copy of. I needed an empty branch to make sure the PR would create a clean version without history from the newer branch. This helped a lot.Teleprinter
L
824

You can create a branch as an orphan:

git checkout --orphan <branchname>

This will create a new branch with no parents. Then, you can clear the working directory with:

git rm --cached -r .

and add the documentation files, commit them and push them up to github.

A pull or fetch will always update the local information about all the remote branches. If you only want to pull/fetch the information for a single remote branch, you need to specify it.

Ledesma answered 20/12, 2012 at 9:57 Comment(14)
Nice answer, but it's kind of annoying that the new branch starts out with all of the files (from the previous branch) staged.Marpet
I have one issue with this, you cannot cherry-pick into this new branch because you cannot cherry-pick into and empty HEAD.Surat
@Ray: If you need to cherry-pick, them branch out of the oldest common parent you need. Cherry picking into an empty repo sound like branching, honestly.Introvert
just for the reference: upon committing to the branch, one have to push it to the origin: git push origin <branchname>Hylozoism
When issueing git checkout --orphan <branch>; I don't see any listing of <branch> in git branch.Maffa
After git checkout --orphan one can use git reset --hard to delete left over files.Hafiz
The reason why you don't see the branch after git checkout --orphan <branch> is because it does not have any commits yet. After the first commit git branch prints the new branch.Masorete
Thanks @Krøllebølle, for explaining that. It's a very confusing "feature".Maddis
git clean -fd removes untracked files.Presidentelect
If youre trying to create a github PR with all your projects files in it, this wont work. Github wont compare two different historiesThanet
Correct @light24bulbs. But that wasn't the intention of the question nor the answer. For a PR just base a branch of master or whichever branch.Ledesma
git clean -f -i -d To remove literally every thingRocky
Is it possible to create an empty branch on github.com or in Github Desktop?Gallop
As of Git 2.23 git switch --orphan <new branch> is the preferred way to create orphan branches.Englishry
N
76

The correct answer is to create an orphan branch. I explain how to do this in detail on my blog.(Archived link)

...

Before starting, upgrade to the latest version of GIT. To make sure you’re running the latest version, run

which git

If it spits out an old version, you may need to augment your PATH with the folder containing the version you just installed.

Ok, we’re ready. After doing a cd into the folder containing your git checkout, create an orphan branch. For this example, I’ll name the branch “mybranch”.

git checkout --orphan mybranch

Delete everything in the orphan branch

git rm -rf .

Make some changes

vi README.txt

Add and commit the changes

git add README.txt
git commit -m "Adding readme file"

That’s it. If you run

git log

you’ll notice that the commit history starts from scratch. To switch back to your master branch, just run

git checkout master

You can return to the orphan branch by running

git checkout mybranch
Nemathelminth answered 30/9, 2013 at 0:15 Comment(2)
It's DANGEROUS to just remove everything in the working directory, because any non-trivial project will have untracked files (configuration, environment, cache, etc).Billen
git rm -rf . does NOT remove untracked files from the working directory - it only removes files in the index. The documentation recommends using /bin/rm to remove files from the working directory. See https://git-scm.com/docs/git-rm for more information.Unicuspid
E
34

2022 Update

As of Git 2.23, you can use git switch --orphan <new branch> to create an empty branch with no history. Unlike git checkout --orphan, all tracked files will be removed.

Once you actually have commits on this branch, it can be pushed to the remote repository:

git switch --orphan <new branch>
git commit --allow-empty -m "Initial commit on orphan branch"
git push -u origin <new branch>
Englishry answered 13/1, 2022 at 18:19 Comment(0)
M
16

Make an empty new branch like this:

true | git mktree | xargs git commit-tree | xargs git branch proj-doc

If your proj-doc files are already in a commit under a single subdir you can make the new branch this way:

git commit-tree thatcommit:path/to/dir | xargs git branch proj-doc

which might be more convenient than git branch --orphan if that would leave you with a lot of git rm and git mving to do.

Try

git branch --set-upstream proj-doc origin/proj-doc

and see if that helps with your fetching-too-much problem. Also if you really only want to fetch a single branch it's safest to just specify it on the commandline.

Millenary answered 20/12, 2012 at 23:25 Comment(6)
This answer is interesting because it allows you to start another branch/root with totally empty first commit.Kerby
Another way to make an empty branch if you don't mind switching to it is git checkout --orphan new-branch; git reset --hardMillenary
What's the use of the xargs?Kaylyn
@Kaylyn it's just another way of getting the tree id subbed in as a commit-tree arg.Millenary
I really like this approach, as this allows to create an empty new branch, without leaving your current branch, plus that fact there's an empty commit as a start point.Brigadier
By the way if the remote branch does not exists, instead of git branch --set-uptream-to you can push the empty branch on remote using git push --set-upstream origin proj-doc:projdoc (from you current branch).Brigadier
R
10

The best solution is to create a new branch with --orphan option as shown below

git checkout --orphan <branch name>

By this you will be able to create a new branch and directly checkout to the new branch. It will be a parentless branch.

By default the --orphan option doesn't remove the files in the working directory, so you can delete the working directory files by this:

git rm --cached -r

In details what the --orphan does:

--orphan <new_branch>
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.

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 <start_point> 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 <start_point>, 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.

Realtor answered 25/1, 2020 at 3:24 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Liaoning
A
9

If your git version does not have the --orphan option, this method should be used:

git symbolic-ref HEAD refs/heads/<newbranch> 
rm .git/index 
git clean -fdx 

After doing some work:

git add -A
git commit -m <message>
git push origin <newbranch>
Ameliaamelie answered 9/9, 2014 at 12:46 Comment(1)
Be careful, git clean can delete files you don't want to delete! Run git clean -ndx first to see what files it will delete before you run it for real with the -f option.Rowdyish
A
8

Let's say you have a master branch with files/directories:

> git branch  
master
> ls -la # (files and dirs which you may keep in master)
.git
directory1
directory2
file_1
..
file_n

Step by step how to make an empty branch:

  1. git checkout —orphan new_branch_name
  2. Make sure you are in the right directory before executing the following command:
    ls -la |awk '{print $9}' |grep -v git |xargs -I _ rm -rf ./_
  3. git rm -rf .
  4. touch new_file
  5. git add new_file
  6. git commit -m 'added first file in the new branch'
  7. git push origin new_branch_name

In step 2, we simply remove all the files locally to avoid confusion with the files on your new branch and those ones you keep in master branch. Then, we unlink all those files in step 3. Finally, step 4 and after are working with our new empty branch.

Once you're done, you can easily switch between your branches:

git checkout master 
git checkout new_branch
Avellaneda answered 10/10, 2016 at 10:21 Comment(0)
J
7

On base this answer from Hiery Nomus.

You can create a branch as an orphan:

git checkout --orphan <branchname>

This will create a new branch with no parents. Then, you can clear the working directory with:

git rm --cached -r .

And then you just commit branch with empty commit and then push

git commit -m <commit message> --allow-empty
git push origin <newbranch>
Jenniejennifer answered 19/2, 2020 at 12:42 Comment(0)
I
6

I found it here

If you use git 2.23 or above you may be used to git switch and git restore instead of git checkout

So if you need to have an empty branch try this:

git switch --orphan YourNewBranch

That's it :))

Incurve answered 6/9, 2021 at 18:0 Comment(0)
A
4

i found this help:

git checkout --orphan empty.branch.name
git rm --cached -r .
echo "init empty branch" > README.md
git add README.md
git commit -m "init empty branch"
Alrich answered 10/6, 2020 at 7:17 Comment(1)
I'm guessing we do this to create a "RELEASE branch" that can never be accidentally merged back into develop, right?Numismatology
D
2

Note that while git switch --orphan <new branch> remains the current best option, its description has changed with Git 2.44 (Q1 2024): the doc clarifies what an "unborn branch" means.

See commit d44b517, commit 49dc156 (24 Nov 2023) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 601b157, 02 Jan 2024)

orphan/unborn: add to the glossary and use them consistently

To orphan is a verb that denotes the act of getting on an unborn branch, and a few references to "orphan branch" in our documentation are misuses of the word.

They caused end-user confusion, which was made even worse because we did not have the term defined in the glossary document.

Add entries for "unborn" branch and "orphan" operation to the glossary, and adjust existing documentation accordingly.


git switch now includes in its man page:

Create a new unborn branch, named <new-branch>.

glossary-content now includes in its man page:

orphan

The act of getting on a branch that does not exist yet (i.e., an unborn branch).
After such an operation, the commit first created becomes a commit without a parent, starting a new history.

glossary-content now includes in its man page:

unborn

The HEAD can point at a branch that does not yet exist and that does not have any commit on it yet, and such a branch is called an unborn branch.

The most typical way users encounter an unborn branch is by creating a repository anew without cloning from elsewhere.
The HEAD would point at the 'main' (or 'master', depending on your configuration) branch that is yet to be born.
Also some operations can get you on an unborn branch with their orphan option.

Dion answered 4/1 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.