Git Fork Vs Git Clone

Git Fork Vs Git Clone

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 our 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?

Here the Git clone comes into the action. Here what you need to do is you need to duplicate the code(fork) into your local and 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 may don?t have the write permission 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 https://github.com/apache/hadoop)

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

Image for post

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 you like and can create any number of git branches on that means it?s your own copy of Hadoop project.

Image for post

Then you can clone it to your local by the following command. Before that Just click the Clone or download button which is in green color and click the clipboard.

Image for post

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).

and just push it to your fork repository branch.

(How to push?

> 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,

Image for post

Afterwards, you can come 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.

Image for post

Question or the use cases

  1. 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 limitation that only the people have the write access can commit directly or can do the modification directly.

But you have to create a feature with 5 peoples. How can you do it?

Do 5 peoples need to create their part and give the PR to develop branch separately?

No, It?s not needed and hard to review.

Then?

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 branch(epic). Then they can implement their own modification on their own branches and give the PR to the epic branch. Here we give all the 5 developers to write access to the epic branch which means we believed in them(They can make mistake and merge to the epic).

> git checkout -b epic develop

> git push origin epic develop

The developers can create their feature branch using the epic branch,

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?

So for that, we can ask the 5 developers to fork their own copy of the product to do their implementation and can give a PR to the Lead Developer?s fork(1 of 5 can be the Team lead). Then the Lead developer will merge the other changes on his own fork and can create a PR to the main repo.

Use case

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

I have the following repositories

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 have your local clone of the fork in your local PC,

If you need to work with the origin project/ with another fork you have to add the corresponding remotes to your local clone,

> 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 by follows,

> 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?

Add the person?s fork repo as a remote branch to the local clone of that project

> git remote add repo <repo-url>

get the changes,

> git fetch repo

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

> git checkout <new branch>

merge their changes to your new branch

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

Afterward, you can do the following according to the output,

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

References:

Fork a repo – GitHub Help

For more information on open source, specifically how to create and grow an open source project, we’ve created Open?

help.github.com

Git: Getting someone elses project without forking

Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share?

stackoverflow.com

Git merge from someone else’s fork

Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share?

stackoverflow.com

https://github.com/apache/hadoop

11

No Responses

Write a response