Creating a new empty branch for a new project

前端 未结 8 2072
你的背包
你的背包 2020-11-28 00:10

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 docu

8条回答
  •  无人及你
    2020-11-28 00:37

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

    git checkout --orphan 
    

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

提交回复
热议问题