When I create a new project in git it contains one and only one branch, the master branch:
~> cd test
~/test> echo "Prueba de git" > Readme.md
~/test> git init
Initialized empty Git repository in /home/jose/test/.git/
~/test> git add .
~/test> git commit -m "Prueba git"
Created initial commit 0babea8: Prueba git
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 Readme.md
~/test> git branch
* master
If I clone a project I find that it may have many remote branches:
$ git clone https://github.com/git/git
$ cd git
$ git branch -r
origin/HEAD -> origin/master
origin/maint
origin/master
origin/next
origin/pu
origin/todo
And that as a result of cloning there is only one local branch, the master branch:
$ git branch
* master
Is there something special about the master branch, is it different from the branches I create with git branch
? Is it mandatory? Will something bad happen if I delete it?
When you clone a repository you also clone its branches, not only the one
master
(the master one, which is created by default), but it saves them inside the "remote" repository and doesn't make it "local" until we usecheckout
.Using
git branch -a
you will be able to see all and usinggit checkout todo
you will be able to switch to the branchtodo
and see that the content of the repository changes, at which point you will also be able to see that branch as local usinggit branch -a
.Take a look at the documentation: https://git-scm.com/book/en/v1/Ramifications-on-Git-Remote-Branches
Answers:
Is there something special about the master branch?
It is the one created by default, and as you will see in the rest of my answers there is nothing special about it.
NOTE: Keep in mind that some applications might assume that branch should exist. Also, as I explain in the comments, if you delete the branch
master
you should delete any reference (even aHEAD
) that points to a commit of it.Is it different from the branches I create with git branch?
Nope.
Is it mandatory?
Nope.
Will something bad happen if I delete it?
No, it will be deleted like any other branch, but you will first have to unite
checkout
to a different branch (or create it if you don't have any other branches):Summarizing it can be said that it is the first to be created. Its use is usually intended to be the main branch of the project, where the latest stable and functional production version of the project is found or should always be found.
The rest of the branches are usually used to make modifications to the project, such as develop, hotfix, beta, etc... And once that version is tested and finalized, it is uploaded as the definitive version to the master branch.
On the official GIT page they explain exactly how the master branch works .
Branches are used to develop functionality isolated from each other. The master branch is the "default" branch when you create a repository. Create new branches during development and merge them into the main branch when you're done. Create a new branch called "feature_x" and switch to it using
go back to main branch
and delete the branch
A new branch will not be available to others unless you push the branch to your remote repository.
Source source: http://rogerdudler.github.io/git-guide/index.es.html