Friday, July 28, 2017

Git for Beginners - Part 8 - Remote Repositories


So far we have discussed how to work with your own repository, which is located locally. There is no conflict if you are the only one who is doing a project. But if you are working on a group project and you need to keep track of the changes of the project which are done by your group members (Consider a large project and share the specification of the project among them). But practically they can't work in your local repository. For that kind of situation, remote repositories are really helpful to you. 

Github, Bitbucket is examples of remote repositories. The basic idea is, they keep your project source code and the group members can get a copy of your project to their own profile and download into their local machines. If one of your group members do some changes to their downloaded source code, they can upload it to their own copy of the project to the remote repository (forked repository) and can make a pull request. So other group members also can see what you have changed. But this is a series of processes. We will discuss in this tutorial how to work with remote repositories.

First of all, you need to create a new account in GitHub, BitBucket or etc. Here I will create a GitHub account. To do this go to this link. GitHub will provide you only unlimited public repositories. But from BitBucket you can use private repositories with some limitations. Once you created an account you can follow the below steps.

There are two ways to work with a remote repository.

          1. Contribute to a project that is started by someone
          2. Create your own project and let others contribute to it.

In this tutorial, we will discuss the first method.

             The best example of this category is, contributing to an Open Source Project. To do this follow the steps given below.

1. First, choose an Open Source Project which will fit with your skills and abilities. As an example, if you are good in Java, you can search 'Java projects' in the search bar or select a project from GSoC Organizations page.

2. Once you have selected a project, go to their main page. In the upper right corner of the page, you will see option buttons.

      1. Watch:  If you watch the repository, you will get all the notifications for any pull requests and issues of that project (even if you are not mentioned). If not, only mentioned notifications will arrive for you.
       2. Start: Starting the repository allows you to keep track of that project even if you are not involved with that project.
       3. Fork:  Once you fork the repository, it will create a copy of a repository and save it in your profile.


Forking a repository is the key thing in this tutorial. Because this will allow you to change whatever you want without affecting the original repository. As an example, if you fix a bug in that project, you can request from the owner of the repository to apply your change. If the project owner likes it, they will add your own feature into their original repository.

3. Now you have the project you like most. But still, you cannot experiment with it. To do that you need another copy of the project onto your local machine. So go ahead and navigate to your fork (in your GitHub profile) of the repository.

      Under the repository name, click Clone or Download.

     If you want to Clone the project, click on the clipboard icon to copy the clone URL for the repository.

          Then open up your terminal and type $git clone https://github.com/YOUR-USERNAME/Project Name and press Enter. Then your local clone of the project will be created.



4. Now you have the project on your local machine! But you, are not the only one who contributes to that project. The changes to the project will happen at any time. Therefore you need to keep your fork and local clone up-to-date. To do this you have to sync your both copies with the original project.

    1. First, go inside your local project directory and type $git remote -v and press Enter and you will notice the current configured remote repository for your fork. It will be considered as the origin.


   2. Then go to the location of the original project repository on GitHub. There you will see the Clone or Download button. Click it and copy the URL. Then open up your terminal and type $git remote add upstream, and then paste the URL you copied and press Enter. (You can use any name for that. Upstream is the only recommended name)


      3. Now again type $git remote -v to verify the new upstream repository you have specified for your fork. Therefore the origin should be your fork and upstream should be the original repository.


Now you can keep your fork synced with the upstream repository with the below Git commands.

       1. Open up the terminal and change the current working directory to your local project.

    2. Fetch all the branches and their respective commits from the original (upstream) repository by using $git fetch upstream. 

       

       3. Then change your fork's current branch to the master branch using $git checkout master.  Then all the changes will download to your local repository.

           

       4. Now you have to merge all the changes from the local repository to your working directory. Go ahead and type $git merge upstream/master. This brings your fork's master branch into sync with the original repository. Note that this will not lose your local changes.


     By doing these steps you can update your local project. When you want to fix some bug, I recommend you create a new branch and do the changes. Therefore you can sync your master branch with the original remote project. After you fix the bug correctly, you can merge it with the master branch. There are several ways to do this and consider that this is only one method that I recommend you.

5. Let's say you fix some bug in the project and you want to say to the project owner that you fix the bug. First of all, you have to push your changes to your fork. To do this use the $git push command.
Before all of these, you have to commit your changes first.


Once you push the changes to your fork, Git will ask for your username and the password of the GitHub account.

Then go to your account in GitHub and you will notice that your push command updated your fork. Now is the time to make a request to the owner of the project about your work. So click on New Pull Request. Then add a relevant title for the Pull Request and leave a comment on what you have done in short. Then click on Create Pull Request. If your work satisfied the owner, he will merge your work to his original project.


---------------------------------------------------------End---------------------------------------------------------











0 comments:

Post a Comment

Copyright © iTecTricks | Powered By Blogger

Design by Shehan Vanderputt