GitHub Basics Pt. 2: Branches and Pull Requests
Tags: github, versioncontrol, beginners
Published: 2021-12-20 23:58:25

###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!](https://dev.to/ceblakely/starting-with-github-made-easy-3g8) 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.
![Git Branch Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly1ukolbwhipwccbcuxl.png)

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. 
![Creating Pull Request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v93ciwkbxqsbpia6xuth.png)
 In my repository, I was working on the Main branch and will be merging the changes into the Master branch.
![Confirming Pull Request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwizw5cay54nnqcsdwaq.png)
 
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."
![Overview of Pull Requests Tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3x418fhjslnfg6lacmwt.png)
![Comparison](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aep0qbhgh6aphrnklma.png)
 After you have finished reviewing the changes, you can finish by merging the pull request. 
 ![Merge Pull Request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvx37bs4v7irk58ljnn4.png)
 
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.
![Delete Branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q2pe5vav859il35110s.png)

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 :)