Git & GitHub made simple - Branching and PR
Tags: git, github, beginners
Published: 2020-10-31 13:58:48

You know how to setup a project with Git and connect it to GitHub. You understand how Git's staging step works and commits don't scare you anymore ([do they](https://dev.to/didof/github-made-simple-two-workflows-to-project-setup-4e3c)?).

It's time to learn how to do things the way big children do them 🤠.

***

## Create a feature branch 🌱
When you start working on a project linked to a remote repository you are provided with a default branch called **master** (recently renamed **main**).
You can touch by hand by typing in the terminal (be careful to have navigated to the exact folder):
`git branch`.
![git_branch](https://dev-to-uploads.s3.amazonaws.com/i/heomkv9kli4vks1rhx6g.PNG)
At the moment there is only one branch and consequently it is selected.

Now you want to implement a new feature in your project.
Rather than going to dirty the master branch (which is the one that carries the safe, tested code, possibly addressed to production), you decide to work on a specific branch for the new feature.
Create a new branch with the command:
`git checkout -b [feature_branch_name]`.
![git_new_branch](https://dev-to-uploads.s3.amazonaws.com/i/8hsujimrqsnydm9r11iy.PNG)

> Here, for simplicity's sake, you've created a branch for inserting an HTML file. Obviously, it's overkill in this case, but I'm sure you will go further and understand the potential of what you are about to learn.

As soon as you create the new branch you are moved to it. Use again `git branch` to find out:
![git_two_branches](https://dev-to-uploads.s3.amazonaws.com/i/r0t7y2a74pdk87orcpa5.PNG)

> Do you want to jump from one branch to another? Just `git checkout [feature_branch_name]` and optionally check with `git branch`.

At this stage, there is absolutely no difference between the master content and the new branch one. The reason behind the branching is that any changes you commit will be constrained into that and only that specific branch. The master branch itself is not affected in any way (at least until you decide otherwise).
You may be wondering "why make life so complicated 😵?". 
Well, you're actually simplifying it: you can finally work with peace of mind on features that could break code or leave them pending while you complete something else. 
Furthermore - and that's sufficient - the concept of branching is crucial to *team working*.

So proceeding with the example, you make sense of existing to the feature-branch by creating an HTML file. Add it, commit it, push it.
> Since this is the first time you are pushing this branch, you need to set the upstream with `git push -u origin [feature_branch_name]`.

![git_add_feature_1](https://dev-to-uploads.s3.amazonaws.com/i/3dikmkq30999j9pi6ocx.PNG)
![git_add_feature_2](https://dev-to-uploads.s3.amazonaws.com/i/8wzvrpnq4zi8tsubkn9t.PNG)

Obviously, the branch function is fulfilled when the feature is merged into the master code. However, I would like to familiarize you with checkout before proceeding. Try to switch to the *master* branch with `git checkout master`.
Pop! Look at your folder structure: the HTML file is gone. Go back into the feature-branch and... there it is.

## Merge branches 👯
You could just checkout to the *master* branch and use the command `git merge [feature_branch_name]` and git would merge the two branches into a single shiny one.
But there is a more professional way of closing the circle. And this is called doing a **PR** (*pull request*). As the name implies, it is a request to pull code into another branch.
> Feature_branch ---> Master_branch

From the moment you pushed the feature branch, this appeared in the repository on GitHub:
![git_pull_request_disclaimer](https://dev-to-uploads.s3.amazonaws.com/i/i97c2qzckx9rmffobflk.PNG)
> If you are an attentive and curious child you will have noticed that when you did the push you were given a url address
![git_pull_request_detail](https://dev-to-uploads.s3.amazonaws.com/i/pr7jbmnvxbzvlkp3wnaf.PNG)
Clicking here is basically the same as clicking the green button.

You create the pull request and you are sent to this page. It is well specified that the feature branch is injected into the master branch.
![git_merge_display](https://dev-to-uploads.s3.amazonaws.com/i/ya30beoxs735gbmo9dt5.PNG)

Below you are asked to give a title to the pull request (usually by specifying the GitHub issue that generated it). In the body it is a good idea to give detailed explanations, perhaps with a nice list, explaining the reasons, obstacles and solutions adopted.
![git_pull_request_commit](https://dev-to-uploads.s3.amazonaws.com/i/4mmtry9zlsudzsoew2q8.PNG)

Finally, you hit that inviting green button and you feel like a slightly better programmer.
On the new screen you *and others* can:
* add comments to
 * ask for information
 * request additions or changes
 * share knowledge
* trace the history of the commits
 * in fact, you can safely continue to push changes on the requested branch pull
* examine the files and add specific comments to the line of code
 * hover on the line, you will see a blue *+* button

> Since it is your pull request you have permission to resolve conversations.

When it all adds up, you can finally proceed with the merge.
![git_merge_button](https://dev-to-uploads.s3.amazonaws.com/i/6vycr5jrn5b2631ik22v.PNG)
**CLICK!**
![git_merge_done](https://dev-to-uploads.s3.amazonaws.com/i/rkdesr06zonqcok6g5k2.PNG)

A pull request is born, throws a tantrum, eventually matures, and finally, alas, dies. Don't cry, it's the cycle of life...
Anyway you can always bring it back to life 🧟
![git_branch_restore](https://dev-to-uploads.s3.amazonaws.com/i/wz4u55aqw61tgxd2bc3c.PNG)


Seriously, let's take a look at the remote repository.
It is actually the result of the merge between the two branches. Yet locally despite being in the master branch you do not see any `index.html` file, why? You need to pull locally the updated remote repository with the command
`git pull origin master`
![git_pull](https://dev-to-uploads.s3.amazonaws.com/i/cmvmecns8lqtk4uzgr10.PNG)
And here everything is as it should be.

Finally, delete 🪓 the branch with the command 
`git branch -d [feature_branch_name]`
![git_delete](https://dev-to-uploads.s3.amazonaws.com/i/3rpo6v6arkxu6w22bc94.PNG)

***

This is the basis and it's not always that simple. Often you will run into **merge conflicts** ⚔. If you like, **[give it a look](https://dev.to/didof/git-github-made-simple-resolving-merge-conflicts-5ha7)**.

Otherwise, continue with the fork [VERY_VERY_SOON].

🐤 twitter: https://twitter.com/did0f