Creating a new empty branch for a new project

前端 未结 8 2005
你的背包
你的背包 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:29

    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.

    0 讨论(0)
  • 2020-11-28 00:30

    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"
    
    0 讨论(0)
  • 2020-11-28 00:37

    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.

    0 讨论(0)
  • 2020-11-28 00:39

    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>
    
    0 讨论(0)
  • 2020-11-28 00:41

    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
    
    0 讨论(0)
  • 2020-11-28 00:46

    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
    
    0 讨论(0)
提交回复
热议问题