Git Github Together

How is it, everybody? I must say this one include the actual application of what we did in last two blogs “What is Git?” and “What is Github?“. Hope you remember them. This is going to be much interesting. So, just hold your horses and start reading.

You must be wondering why did I say that it is the actual application of what we did in last blogs. Well, in last two blogs we learned how we can do version control using Git and Github independently. This time we are going to use both of them in a collaborative manner and through this, we can actually increase our efficiency of work.

Github provides an extensive amount of features like opening issues which can be used as a bug tracker, network, gh-pages, reviews and the most important of all, it provides a platform for everyone to contribute in any project available. Though Github also gives us the option to do version control, it is always preferable to do version control using Git on your local machine and then upload it to the Github. This way the tasks we perform are done efficiently.

Let’s start then…

First, make a repo on your Github account, let’s say GithubLearning. You know how to make a repository, right? If not, kindly refer to this. Now, create a new file, give it a name( HelloWorld here) and write some text into it. For instance, let’s write

This is the first file in this repository.

Give it a suitable commit message, check on the Commit directly to the master branch and hit Commit New file. Now, you would be having two files in your repository. One would be and another would be the file we just created i.e. HelloWorld.

Now, you can create another file give it a suitable name and commit message with some content into it. Now, do branching create a new branch let’s say NewHelloWorld and create another file in this branch.

So, now you would be having 3 commits in your master branch and 4 commits in the NewHelloWorld branch.

This part we already knew, i.e. how to work on Github. Though you can create files and paste your code up there, there is no way to check if the changes in the code are right or wrong unless that updated code can be on our local machine with all the other files in the repository. Some of you would say that we can copy and paste the code in a file on our machine and then check it. This could be one of the options but that is not at all efficient. If you have a number of files on Github depending on each other, you won’t just copy paste the code, this will take a whole lot of time. This is where git comes into play.

The solution to this problem is to do cloning. We can do cloning through git and GitHub both, though we generally do it using git. Now, fire up your terminal and write:

git clone <YOUR REPO URL>

In my case, it is git clone and then press enter. Do take care that it requires the internet connection onto your machine. Once it is cloned on your machine. Open the repo and check the git status, you would see something like this:

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

It says the branch is up to date with origin master. Here origin refers to your remote URL i.e. your Github URL. At the time of cloning, the origin is set to the URL you give to the git clone command.

Now, do some changes on your machine in your repository. For now, let’s update the file HelloWorld and commit this file with the suitable commit message.

This is the first file in this repository.
It's name is HelloWorld

I have added a line in the HelloWorld file and then I followed the procedure i.e. first git add HelloWorld -> git commit -m “Update HelloWorld”. Now check git status. It would show something like this:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
 (use "git push" to publish your local commits)

nothing to commit, working tree clean

It says that this branch is ahead of origin by 1 commit. This means that git tracked that a commit has been done on the local machine which is not reflected back to the origin i.e. your repo on the Github. So unless you push the changes to the origin in the specified branch, it won’t be shown on the Github repository. It is always necessary to update your Github repo. So, to push changes to your Github repo. the command used is:

git push <repo_URL> <branch>

In my case, it is

git push origin master

Since origin is already set to my repo URL at the time of cloning and the branch we updated is master so, we have to push the changes to the same branch. As soon as you hit enter, git will ask you to input your username and password. Input the correct username and password and your changes will be pushed to your repo. You can see those changes in your Github account now. Now, there will be 4 commits in your master branch.

In collaboration, while doing work on our local machine, the changes always happens on the Github repo as many other people also do the same work and keep contributing to the project. So, it is always necessary to regularly update your repository on the local machine. To do that we pull the changes from origin.

Now, do some changes on the Github. Let’s create a file in NewHelloWorld branch(say ThirdFile) and write some content into it. For instance

This is the third file in the NewHelloWorld branch.

Since we have done this on our Github account. So unless we pull the changes from it, they won’t be shown on our local machine. When you’ll do git status in your NewHelloWorld branch, it will show the message like this:

On branch NewHelloWorld
Your branch is behind 'origin/NewHelloWorld' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

This time, it says that our branch on the local machine is behind origin by 1 commit. This means that we need to pull the changes from the origin(your repo URL). So to pull the changes we use the command:

git pull <repo_URL>

Here, it would be git pull origin or what you can do is git pull –all.

In open source contribution, we don’t only work with our repository. We contribute to other’s projects also. So, just like you cloned your own repo from your Github account, you can clone other’s repository also. Just make sure you give the right URL. However, you can clone someone’s repo and do work on your local machine, you cannot push the changes unless you have permission to do so. The simple solution to this dilemma is that first you fork other’s repository(project) and then you clone it from your Github account. As you would be doing the changes, you can simply push them to the origin(which is actually your repo. URL) and then make the pull request from your Github account to the actual project(repository) which you have forked.

Well, you won’t be the only one who would be doing the changes or contributing to the project. So as I said earlier you would need to keep your local repo updated. But now, it won’t be done as git push origin master. Any guesses why?

It’s because the changes are being done to the project from where you have forked it which is called upstream. Upstream basically is the root project from where other projects are forked and worked upon. In our case, the project which we have forked is upstream. So we have to pull the changes from upstream and not the origin.

Therefore, according to it, the command should be

git pull upstream

But before that, you need to make sure if you have added the upstream or not. You can check that using the following command:

git remote

This command tells the remote URL’s added to your configuration. For now, it would be showing origin only. So, to add the upstream you need to enter the command given below:

git remote add upstream <URL of the repository from where you have forked the project>

Well, you can give any name instead of upstream, though it is always preferable to go with conventions. You can check the names of your remote using git remote command and to check the corresponding URL’s use

git remote -v

Just as you added the upstream, you can also add the origin (if not set ).

That’s all for this blog post. Now, you can bring your or other’s project to your local machine, do changes required and push back to the Github. Like this cycle of the contribution goes on. You do a lot of commits, get reviewed and learn a lot. This is just starting of doing the collaboration. These were just a few basics, there is a lot more to it.

If you have any query or suggestion feel free to write in the comment section below.

Be curious, keep learning!



2 thoughts on “Git Github Together

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s