This is Part 2 of our GitHub Basics series that will give answers to questions I had while learning about the version control platform GitHub. To read the Part 1 on initializing a repository and pushing to GitHub, see here! I'm going to break down the concept of branches, using the branches to manage your repository, and using pull requests to merge changes into your production branch.
Utilizing branches in your project is a safe way to try new things that won’t impact the branch used for production. In most projects you will typically have at least two branches: Main and Master. These can be renamed to whatever suits you, but these are standard branch names. For example, you can create branch Main for development purposes and then create a pull request to merge those changes into the Master branch. It is best practice to not directly edit the Master branch, but to make changes in another branch and merge to the Master branch when the code has been tested and confirmed as stable.
This will allow you to have a reliable record of changes that are production-ready, so just in case an issue arises you can roll back to the Master/production branch's previous version. Lets get started. :)
Command to check branch you are currently on:
git branch
It will show all branches on your local repository and the branch you are currently on will be starred.
Command to create another branch based on the branch you're already on and automatically move over to that branch:
git checkout -b testbranch1
Command to create branch while remaining on the current branch:
git branch testbranch1
When you have modified your code in the testbranch1 and want to add it to your production code on the Master branch, use the following commands:
git add filename.js
git commit -m "Commit message here for context"
git push origin testbranch1
You will want to go back to your repository in GitHub and select the Compare & pull request button seen below.
In my repository, I was working on the Main branch and will be merging the changes into the Master branch.
Doing a pull request can be useful if other people need to review what changes have been made before they are approved. To compare the changes, select the Pull Requests (1) Tab and select the name of your pull request; in my case, we would select the link that says "Collections template added."
After you have finished reviewing the changes, you can finish by merging the pull request.
Once you have finished merging and closing the pull request, GitHub will let you know you can safely remove the development branch if you want.
You don't have to delete the branch, but it is an option. You can also delete a branch through the terminal if you choose to. If you are needing to delete testbranch1, for example, you will need to move to the Main, Master, or another branch. Use these commands to change branches and delete testbranch1:
git checkout Main
git branch -d testbranch1
I hope this helped clarify some points that had me confused when I first joined GitHub. Hit me up in the comments :)
Top comments (0)