When to create a new empty branch?
Sometimes, it can be very useful to create a new empty branch on git
. For example, imagine the following scenario: you are working on a website project but you also want to keep a good documentation aside, in the same repository. So, in order to keep things minimally separated, you can create a brand new branch called docs
and keep your website code at another branch called develop
.
That way, you can always easily change from one workspace to another (changing branches) and also keep everything inside your repository.
How to create it?
It's simple: let's execute the command git checkout
, in order to change to another branch. Also, we're going to use the --orphan
flag, which is going to be responsible for creating a branch with empty commits on it (you can git log
in order to verify this, after creating the branch).
Also, we must give the branch a name right after the --orphan
flag.
git checkout --orphan <branch_name>
Then, your project files from the previous branch you were working at will still be there. But they will be there just as untracked
files and they can be easily removed by executing the command:
git reset --hard
When using the --hard
flag, git reset
will be responsible for destroying any changes you've made in your branch files, compared to the last HEAD commit (last commit you've made). Since our branch is empty and has no commits on it, all the files are going to be instantly removed.
Switching back to another branch:
After creating a commit in your new branch, it will be visible in your branch list, which you can check by using the command:
git branch
Then, you can switch between any branches by using:
git checkout <branch_name>
Enjoyed it?
Please, like this post and follow me on social media!
Top comments (0)