Pravar Agrawal Technology & Travel

Add Remote Repository

While working with more than one teams in a project I’ve always faced few generic issues. How do I contribute to the upstream project? Is creating a new branch in the main repository the preferred way? If not then what to do otherwisw? And like always, git to the rescue. In this post we’ll take a look at some of the similar issues and how to resolve them.

I’ve been using Git for quite some time now both at work and during my own fiddling. And such issues are very frequent. Best approach which I follow in such cases are to fork the original repository which we want to contribute to into ours. And then clone our fork onto our machine and push the changes from there. Also, we want our local to gather recent updates from the upstream repository which our fork misses out. For that we can add the upstream repository with Pull access into our local. Following commands will make it more clear,

git remote add <shortname> <url>

For example, I want to add remote url of https://github.com/pravarag/screen-manager then,

git remote add sm https://github.com/pravarag/screen-manager

git remote -v

Here remote -v will show all the urls which Git has stored for our shortname to be used while reading and writing to the remote. Now, we’ll update our local branch with our newely set remote. Follow,

git checkout <local-branch>

git pull --rebase origin sm

And now, our local is updated with the latest changes from the upstream repository. Then, we can see that there are new changes ready to be pushed to our cloned repository from local. To do that, we simply push those changes to our repository,

git push -u origin master

And now comes the best part. We’ve updated our cloned repository with latest as well as with changes which we have newely made. Now we want our changes to be a part of upstream project. So, we need to raise a merge request for that. With merge request, we actually ask for an approval by reviewers from the upstream project that whether or not, our changes are acceptable or not. For submitting a merge request if, we are using Github then we need to first go to the upstream project’s repository. We can see a tab as Pull requests on repository’s main page. Once on Pull requests, we need to select New pull request option button. We are presented with an option of compare across forks which we are going to use in our case. Once we click on that, we can select our forked repository as head repository and it’s branch we want to compare with base repository as upstream repository.

Everything set, and we have just created a pull request for our recent changes. Now, our pull request is pending for review by any of the reviewers from our upstream project. Also in some repository, once pull request is created a series of automated tests starts executing as part of Continuous Integration checks. Once our changes are approved and the approver can progress with merging our changes.