Thursday, 1 August 2013

Sync your fork and Original Resipository

I just started working for a organisation whose code is on Github. I cloned their repository and after making changes I tried to push changes to their repository But I couldn't. The reason was obvious. I cloned their repository. I don't have the permission to make changes in their repository.

Thats why you fork a repository. You can make any kind of changes in it. So I did that. But after I cloned it, I realised that the repository was too old. I hadn't merge the changes made in the original repository into my repository. I used to think that my fork automatically gets updated to the latest changes made in the original repository. Stupid thought? I know :P

Now you have to manually sync both of the repositories i.e. Your fork and the original one. I am writing this from this blog.

I assume you have a local copy of your repository. Now issue the following commands

Now you need to add a remote branch to your repository which points to the original repository.

Issue the following command to do that

$ git remote add --track master mleung git://

Now here are 3 parameters i.e. master, mleung and git://

First one is the branch from which you want to merge. Most of the times it is master. In my case it was develop so I replaced it with develop. The next parameter is just a label. You can write whatever you want the name to be like repository_remote. And now the third parameter which is the link to the original repository. This command will add a remote branch to your repository. As you have noticed this command fetches nothing from the Internet. Now the next command is to do the same. Now before doing that you can also check if a remote branch was added from your above command using the following command

$ git remote

Now we can fetch the remote branch using

$ git fetch mleung

Now that you have fetched the changes and you have it in your repository. You can merge it which is the easiest thing to do. Now here specify the name of the remote branch that you have used above

$ get merge mleung/master

Now you successfully synced your local repository with the latest changes. Now you can push that changes in your remote repository with pride :P

$ git push

Happie Gitting :P