Forking the Git

We have gone through all the mandatory steps needed, if a complete project is handled by a single user. What if there are 20 different people making their contributions and want to merge them into an existing remote branch. All that’s needed to be done is cloning the repository, which makes all of the repository content available at the contributor’s end. It might be done as:

git clone repo_url

For example:

git clone [email protected]:rtCamp/rtpanel.git

We can now say that the contributor has now successfully forked the repo. Forking the repo is nothing but cloning the repo. Also the shortname origin is automatically assigned to the forked repo. After the contributor finishes with making the changes for the day, all he needs to do is push back the changes to the remote as:

git push origin master

Fine, now weeks later, the same contributor plans to add some further validations to the code. He now needs to pull the updated code on the remote master. Git pull command fetches the updated code on the remote branch you previously cloned from, to the local branch and merge the changes. To do so, open the terminal, move to the respective project directory and type the following:

git pull

Git pull is used as a magic command that combines git fetch and git merge, though it’s preferred to use them separately instead of using the single git pull command as you can check what you have fetched before merging the changes.

When we clone or fork a repository, the command automatically adds that remote repository under the name ‘origin‘. So, alternatively we can use:

git fetch origin

Git fetch doesn’t alter the local working tree until merged. The first command fetches any new work that has been pushed to that server since last cloned or last fetched. Making sure that we are working on master, we can take a look at the differences between the local branch and the remote one by:

git checkout master
git diff master origin

So looking at the changes, we can now merge it as:

git merge origin

I hope this was quite a helpful and simpler approach to getting familiar with Git.