Tech Notes

Git & GitHub for Beginners

Git Branches

How to use branches while working on your project. This includes creating branches, switching branches, and merging branches.

Branches allow you to work on, or add special features, without interfering with your current project. When you start a repository in Git, it is automatically assigned to the role of 'main' branch.

I often make another branch to work on the layout while I am building my site. I call it layout. Once I am pleased with my work I merge it with the 'main' branch.

To start in your terminal type in:

git branch

This will give you a list of current branches in your project. The branch you are currently working on will be highlighted. To make a new branch:

git branch name-of-new-branch

Then to switch over to your new branch type in:

git checkout name-of-new-branch

Now you are working on your new branch. Once you have made some changes you go through the same process of:

git add filename

git commit filename -m "message about changes"

git push

You can also combine the commands:

git add filename && git commit -m "message about changes" && git push

If it is your first time pushing this branch to your remote repository then use:

git push origin name-of-new-branch

Once you are finished pushing your changes to GitHub, then you can switch back to main by typing in:

git checkout main

Once you have switched to the main branch, you can then merge your other branch to main by typing in:

git merge name-of-new-branch

Now you have merged your changes from your other branch in with the main.

back to top