Lets say you want to add some new files and code to your project to test if it works better. But you dont want to mess up your current work. So what do you do? You can make a new 'branch' of your project with git and test your code in that branch. If it works well you can also merge it with your main branch, i.e. , master or if it did not you can delete it.
Use this command to make a new branch:
git branch branchname
For example, if you want to make a new branch named "test",
git branch test
To switch and work with a branch, use this command:
git checkout branchname
For example, to switch to the previously made branch - "test",
git checkout test
Now you can add files, change code, or do anything. All these changes will only be recorded on your new branch. Also make sure, you use the add and commit commands to save changes to your branch.
So after working on your branch, you can merge it with your main branch - master. To merge, you have to first
checkout your master branch:
git checkout master
and then merge by using:
git merge branchname
This will merge your branch with your main branch - master. Now you can delete the merged branch by using:
git branch -d branchname
and your branch is deleted.
But if you try out the delete command without merging, it might show an error. To forcefully delete a branch
try this command:
git branch -D branchname