DEV Community

Cover image for Branches in Git
kulsoomzahra for eduAlgo

Posted on • Updated on

Branches in Git

Branches are no doubt, one the BEST features of Git. The Master Branch being the default of the repository is automatically created whenever you fork a new repo.
So usually when making commits, we've being committing only to the master branch.
This master branch is the stable version of your code and something you will release that's the reason why we don't really want to try out new code or features on this stable version (master branch) just in case it messes the whole code of your repo.
So we create an isolated space to experiment new code and features. If all goes good and they seem to work well, you can merge this isolated space (i.e branch) into the master branch thus eliminating the risk of messing up with the stable version of your repo.

This is why branches are a massive feature. Saving you right?

Alt Text

Working on a branch

Here's how you can work on branches:

1) Make a new branch
git checkout -b <branch name>

If you want to switch to an existing branch use
git checkout <branch name>

2) Open the editor of your choice and make changes

3) Add the files
git add .

4) Commit the changes and add a commit message
git commit -m "commit message"

5) Finally, push these changes to the origin
git push origin

If error occurs, it's probably because you've not set your remote properly . Try using
git remote set-url origin SSH of your repo
OR
git push --set-upstream origin <branch name>

Congrats! You've merged your branch into the master!

Renaming a branch

Rename a branch while currently not on the branch:
git branch -m oldName newName

Rename the current branch:
git branch -m newName

Deleting a branch

git branch -d <branch name>

Also delete branch from origin using

git push origin --delete <branch name>

Top comments (0)