I assume that you have some idea about the version controlling System which is used to store the source code with its history and keep the release process for that particular software.

I am going to explain “Git Fork Vs Git Clone” using the famous version controlling system GitHub.

What is the difference between Git Fork and Git Clone?

Git Fork means you just create a copy of the main repository of a project source code to your own GitHub profile. Here you can experiment whatever you like without affecting the main source of that project.

The fork is mostly used to indicate or propose any changes to the source project or create a new idea using that project source as a starting point.

Then make your changes and create a Pull Request to the main repository branch. If the Main Repository owners like your changes, they will merge it to the main repository.

How can you modify your changes?

This is when Git clone comes into the action. Here is what you need to do in order to duplicate the code (fork) into your local repository, modify it with your implementation and push it back to your fork to create a Pull Request to the main repository.

Why we need Fork?

  1. You might not have write permissions to work directly on the main repository. (Mostly this model is used for open source projects).
  2. If everyone clone and directly work on that main project repository/branch, then it’ll be very hard to manage.

How can we fork and clone a repository to work on an open source project ?(For an example, let’s take https://github.com/apache/hadoop)

First, what you need to do is go to the above link and click the Fork button of that project.

You can get your own copy of this project and there you can notice that the project repository name will change as {your-profile-name}/hadoop.

Here you can do whatever changes you like and can create any number of git branches on it. That means it’s your own copy of Hadoop project.

Then you can clone it to your local by issuing the following command. Before you do that, just click the “Clone” or “Download” button which is in green color and click the clipboard.

Open a git bash into your local computer and type the following command in a folder specifically for this project.

git clone https://github.com/{profle-username}/hadoop.git


It will automatically download your fork to the local computer. Then you can do the changes to that fork (You can even create a branch on your fork to add the modification). Once changes are done, just push it to your fork repository branch.

git add {modified file name}
git commit -m “Comments”
git push origin master

 

Then from the UI of your own copy of Hadoop, click Pull request as below.

Afterwards, you would go to the following screen where you can create a Pull Request to any of a branch of anyone’s fork from your own fork’s any of the branch.

 

Questions and use cases

Why fork is good, compared to work directly on a repository?

For example, we know that the master branch of a repository is always secured and blocked to push directly with the changes because this is where we keep the release tag/ the actual product.

Mostly we do the development using the develop branch. There also we have some limitations, that only the people with write access can commit directly or do the modifications directly.

If you have to create a feature with a team of 5 people, how can you do it?

Do all 5 people need to create their own and give the PR to develop branch separately?

No, It’s not needed and hard to review. Then, how can we achieve that?

you can simply create a branch (epic) using the develop branch, and those 5 developers can create their own branch (feature) using that newly created epic branch. Then they can implement their own modification on their own branches and give the PR to the epic branch. Here we give all 5 developers write access to the epic branch, which means we trust them on the changes merged to the epic branch.

git checkout -b epic develop
git push origin epic develop

 

The developers can create their feature branch using the epic branch as below;

Developer i:
for (int i; i<6;i++) {
git checkout -b feature/i epic
git push origin feature/i epic
}


How can we avoid mistakes because we give all the developers to write access in the above model?

For that, we can ask the developers to fork their own copy of the product to do their implementation and raise a PR to the Lead Developer’s fork. Then the Lead developer will merge the other changes on his own fork and create a PR to the main repository.

How can I merge my git fork with someone else’s fork of a Project in git?

Let’s take below example.

https://github.com/apache/hadoop.git — The origin project
https://github.com/hariss/hadoop.git — My Fork of the origin project
https://github.com/prashath/hadoop.git — Someone’s Fork of the origin project

So you will have your local clone of the fork in your PC (local). If you need to work with the origin project or with another fork, you have to add the corresponding remotes to your local clone as below.

git remote add upstream https://github.com/apache/hadoop.git
git remote add fork https://github.com/prashath/hadoop.git

 

so now you can get the remote information by fetching from those forks.

git fetch upstream
git fetch fork

 

Finally, you can merge someone’s fork to your one as below.

git merge fork/{branch 1- branch you want to merge}


How can we merge if someone else will create a fork and modify to our own fork?

First add the person’s fork repository as a remote branch to the local clone of the project.

git remote add repo <repo-url>

 

Fetch the changes next.

git fetch repo

 

If you need to merge in a new local branch (new branch) to that branch.

git checkout <new branch>

 

Merge the changes to your new branch.

git merge repo/master (Assume that you need to merge to other person’s master branch)

 

Afterward, you can do the following actions based on the output.

add/ commit / resolve conflicts/ push / Create PR/ merge with develop or Master.

References: https://help.github.com/en/articles/fork-a-repo

Author: Hariprasath Thanarajah is a Salesforce Engineer at CMS – Salesforce Certified Developer. You could follow him @tharis63 “https://medium.com/@tharis63

 

Author : Admin
Published Date June 25, 2019
Share Post
[ssba-buttons]