Creating a new empty branch for a new project

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

提交回复
热议问题