GitHub and Git for beginners — Part 2

Ishan Choudhary
Level Up Coding
Published in
6 min readJul 3, 2021

--

Photo by Yancy Min on Unsplash

Hey everyone, welcome to the second part of the GitHub and Git for beginners series. In today’s blog, we will be merging our code with git as well as exploring branches in more depth.

In the last blog, we committed all our changes. If you haven’t read it already, then check it out by clicking on this link. Now it is time to send these changes to our GitHub repository. So first login into GitHub with your account and you should see something like this:

Click on the ‘New’ button, then give your repository a name. Select private, so that only you and other people who have link for the repository can view it. It should looks something like this:

Click on create repository. You should land up in a page like this:

You can see, right on the front page, there are steps on how to create a new repository from the command line using git, how to create a new repository locally and import it into your GitHub, how to import an existing local repository, and how to import code from another repository. We are going to use the second one.

So all your need to do is that you need to open the folder that is your local GitHub repository. Then open a terminal and type out the commands given here:

For you the ‘git remote add origin’ will have a different link after that. Since your repository and account will have a different name. Also I highly suggest that you do not copy the commands but rather type it out, since sometimes errors do come in.

Once you have done this, just refresh your GitHub page, and you should see something like this:

Now let me explain the commands. The ‘git remote add origin’ basically connects your locally initialized repository to your GitHub repository. So that means now your repository is a remote repository and not a local repository. The ‘git branch -M main’ moves you to the main branch. If you remember, your repository is like a tree in which it has one default branch and you can make other branches. This default branch is called ‘main’, or ‘master’ in some cases.

Before going on to the last command, let us understand what is ‘pushing a commit’ is. By ‘committing’ you are basically saving a snapshot of your code at that point of time. By pushing, you are sending the commits or ‘snapshots’ to git hub. It will also send the contents of your local repository and update it in GitHub

Now the last command ‘git push -u origin main’ tells git to send the snapshot of your code along will the contents of the folder containing the code. So for example You have your folder containing the ‘README.md’ file. Now this file will also be sent to GitHub.

Now lets see how do we make new branches. So to make a new branch simply type:

git checkout -b new_branch

The branch name cannot contain spaces. So I just created a branch called new_branch, and when you create a branch you also shift into the branch. So if you are using git bash, you should see something similar to this:

The branch name is written in cyan. All your files will remain same, but now if you make a new file or add any new file, it will be restricted to only that branch. It won’t show up in any other branch. Lets see this in action. Firstly, I am going to open VsCode since it has git integration, so we can see the changes. I’ll create a new file called ‘test.txt’:

You can all see the contents of the file. I’ve created this file on the ‘new_branch’ branch. Now commit the changes, and then switch branches and see the results:

Their you have it! The file remains to the branch it was committed to which was the ‘new_branch’.

We have already seen how to push all changes to our GitHub repository. You simply go:

git push origin 'branch name'

And now if we check GitHub:

There it is! Our changes. If you switch branches in GitHub, which you can do by clicking on the drop down box on which the branch name is written. You will see that the main branch, will not have the test.txt file.

Branches are mainly used, for example when you are making a new feature in your project, but are worried that the changes can break your code, and also want to make sure that the original code without the new feature is preserved. Just make a new branch. Since all the files in the default branch will be copied out to the new branch, you can start making the changes. Once you are sure that everything is working. It is time to merge the default branch and your working branch. To merge the branches, just go to the default branch, and type:

git merge 'branch name'

You have to be on the branch, which needs to have another branch merged into it. So I have the test.txt file in the new_branch. Once you merge the branches, you will see that, the test.txt will appear in the main branch, and the new_branch will remain unchanged.

Now how do we pull changes that we have made through GitHub. So suppose I go to my repository, and make changes to the test.txt. You can make changes to the file in the repository on GitHub by clicking on the file you want to edit and click on the pencil button:

Then change the file and commit it:

Making Changes
Scroll down and find this button to commit changes

Write what you want your commit name. It is not necessary to put in a commit description, although you can if you want. Now click the green commit changes button. The changes are committed.

Now if you see your text.txt file in you laptop. The changes have yet not occurred. This is because this folder does not ‘know’ what changes have been made on the GitHub site. So we need to pull the changes to our folder.

Go to your terminal and change to the branch where you made the commit:

git checkout branch_name_here

You will get some message like this:

Now lets pull our commits. Type:

git pull

And then see your changed file. For me it was test.txt:

And that’s basically it. You have pulled the changes. Now one final thing is how to see all your commits. To do that, just go to your terminal and type:

git log

And you will get something like this:

That is it! You have learned how to use GitHub and Git. I haven’t shown you all the commands; But what I have shown is enough to start using GitHub for your projects. This is the end of the two part blog on how to use GitHub.

Anyway that is it for this one. Share it if you think others will find this useful and follow me for more such tutorials.

Thanks for reading!

--

--

I am a high school student and an aspiring software developer. In my free time, I post programming tutorials over here.