7. Creating A New Branch Using git branch
Lets say we want to add something new to our project or repo but without affecting the current code or lets say we want to copy the current project and make change to the copied version without affecting the original one.
Then this is where branching feature of git comes handy. No matter how big or small your feature is its always better to create a new branch. By doing this whatever the changes we are making will not hamper the original code base until we merge these new branch to the original branch.
Before creating a branch lets first check our available branches in the repo using git branch --list
$ git branch --list
* master
(END)
By default while we initialize a git in our project it sets a master branch. And this branch is the one that we use in the production environment i.e when our project will be live to public master branch will be the active branch. So, its better practice to create a develop branch first and add other features branch to it. The * in master sign denotes that the active branch right now is master.
Lets create a new branch using the command git branch "branch name"
$ git branch "develop"
$ git branch --list
develop
* master
(END)
So now we have two branch develop and a master branch. So lets make our develop branch as active so that we only will make changes in the develop branch without affecting the master branch by using git checkout branch_name
$ git checkout develop
Switched to branch 'develop'
Now develop branch is the active branch, you can make sure by using git branch –list again.
Note: If you want to create a new branch and switch to it with one line code you can use git checkout -b branch_name
Lets make some changes to out code base now. Open up your text-editor and edit the index.html file
index.html
<html>
<head></head>
<body>
<h1>This is the first change</h1>
<h2>I am adding an awesome feature to this project</h2>
</body>
</html>
Lets go ahead and add a readme file as well which basically is used to describe about your project and will also be seen in github site. Create a new file with name README.md and right some description about our project.
README.md
## This will be shown in the github site :)
## I am learning git
After doing this we need to check our project status using git status
, it shows that two files are changed. The index.html and the new README.md file.
In order to know what changes were made we can use the command git diff
We are sure about the changes so we will add and commit them like we did above by using git add and git commit command.
I hope you added and committed the changes. If we run this index.html we will see that the output in the browser has a header text “I am adding an awesome feature to this project”.
Lets switch to the master branch git checkout master
. Now if we reload the browser we will not be seeing that header text “I am adding an awesome feature to this project”. This is because we had added this feature to our develop branch and right now we are in the master branch.