Git For Dummies (2/2)

Nicholas Loh
7 min readMay 14, 2021

--

In the Git For Dummies (1/2) we have learned about the basics of git. In part 2 we will be working on branches and remote repository — GitHub.

Why do we need branching?

Making changes on your main code base directly is not always a good idea especially when you are experimenting with new features, you will not want to mess up your working code base. Instead, we will prefer to separate the changes in other places and only merge in to the main code base after making sure everything is working as expected

Illustration of branching

Git Branch

A branch is a separate space where you can make changes without worrying messing up the working code, it is like having another save file !

We will be using the same folder from the previous part. First let us check what branches we have and which branches we are in.

git branch
output from Linux terminal

From the terminal we can see all the existing branch we had and the branch with the asterisk (*) symbol is our current branch. As you can see we are in the master branch, this is the first branch that will be created by git when we run git init .

To create a new branch we can use the following command

git branch <branch-name>

After that we can take a look at our branches with git branch .

Now we have successfully created a new branch. Now we need to change our current branch to the new branch we created just now. Using the git checkout command. This command can let us navigate through different branches and commits also.

git checkout <branch-name>

Now we are in our newly created branch! There’s a shorthand for creating and go in our new branch.

git checkout -b <branch-name>
shorthand for creating a branch

Now in our new branch we can nowmake any changes we want to index.js. We will add some new line in and make a commit.

added a new line to our file
add and commit our changes

Now we can switch back to our main branch and look out index.js file.

As you can see what we have added in our new branch is not appeared in or master branch. This is because branches does not interfere with each other.

After making all the changes in our branch and we have decided to update our master branch with the newest changes, we need to checkout to our master branch and merge it with the following command.

git merge <branch-name-to-merge>

Now we have a look at our index.js file again. And we can see that the changes we made in our branch is merged into our master branch.

Here is some useful command about git branch

Delete a branch

git branch -d <branch-name>   // delete merged branch 
git branch -D <branch-name> // forcefully delete branch

Rename a branch

git branch -M <branch-name> <new-branch-name>
git branch -M >new-branch-name> // change the name of the current branch

Remote Repository

After knowing about branches, it is time to work with a remote repository. In most cases you will not be working alone, you will be working with a team of developers. Sharing codes and changes between developers is very important.

Various online version control system platforms such as GitHub, BitBucket and GitLab offers solution to such problem. In this tutorial we will be using GitHub as it is one of the most used platform for open source projects.

Getting started with GitHub

We will need a GitHub account, go to GitHub and create an account if you haven’t. After creating an account we will create our first repository on GitHub.

As we had already initialized our local git repository, we will not need to tick the options below.

After creating a repository we can see the following page

Enter the following command into our terminal

git remote add origin https://github.com/NicholasLoh/Git-tutorial.git

The following command will let our local repository know which remote repository to point to.

To check that we had successfully added an origin run the following command.

git remote -v

Push changes to origin

After making all the changes in our local repository, we will need to push the changes to the remote so that other team member can have the newest code we wrote, to achieve this we need to run

git push origin master // master is the name of the branch

It may ask for your username and password when pushing to origin, one way to avoid this is is to connect to GitHub using ssh. But we will not be covering this in this article.

After finish pushing the code we can refresh our GitHub repository and see our changes there.

We will make some changes to the file directly from GitHub by clicking the file and edit

After finish editing we will directly commit into the master branch.

Pull new changes from remote repository

We had added a new line to our file directly from GitHub and we are going to pull the changes back to our local repository and have a look at the index.js file again.

git pull origin master

Now we had successfully pulled the newest changes from the remote repository to our local repository. This is how a team of developer work with each others.

Pull Request

In the previous example we are directly pushing our new code to the master branch. But it is not recommended when working with a team, usually team members will review each other code and make sure that the code has no issue and ready to be merge into the master branch (you will not want to work with a bug that is not written by you!).

Instead of pushing directly to the master we will push our branch to GitHub and create a “Pull Request” to let the team member review our code.

First we will create a new branch called “featureA”, make some changes to index.js and commit the changes.

removed lines and added new lines of code in

This time we will not be checking out to master and merge it we will directly push the commits to GitHub.

We will get an error saying The current branch featureA has no upstream branch. The current branch featureA has no upstream branch.An upstream branch is a branch on remote repository that tracks a branch on local repository.

To fix this we will need to use either one of the following command.

git push --set-upstream origin featureAgit push -u origin featureA

We can see that a message appeared in our GitHub repository. Clicking Compare & pull request will bring us to a new page where we can change the name of the Pull Request (PR), write a short description about the Pull Request and see all the file changes. After that just click on “Create Pull Request”

By now other team member can go to the Pull Request tab and look at all the Pull Requests made and review it.

Pull Request tab on GitHub

By clicking into each Pull Request you can see what has been changed in each file and able to leave comments about the changes. Changes and reviews will be request and done here. After the changes are confirmed click on “Merge pull request” to merge the changes to the master branch.

That’s all guys, thanks for reading the article. This is a basic introduction about to Git Branches and GitHub. Hope you all enjoy this small series — “Git for Dummies”. Happy Coding!

--

--

Nicholas Loh
Nicholas Loh

Written by Nicholas Loh

A Computing Science student who are interested in technologies and software development

No responses yet