What is the difference between cloning and branching?

I am just learning how to use Github and SourceTree and I am really confused as to how it all works.

When I cloned my repo, a duplicate copy of the repo appeared in my file structure. After that, that pink branching line appeared. Is cloning essentially creating an alternate universe file history that begins at the same point as the original but then branches off in to its own thing? If so, then what is the point of “branching”? When I create a branch, it doesn’t change what appears in my file structure hierarchy at all. So what is the difference?

3 Likes

Hi @darienspringer654,

Welcome to Github Community Forum!

They are completely different. I will try to explain with an example.

Consider a history of a repository:

                 master ->  A—B---C—D 

A, B, C, D are commits of this repository made on the master branch. Consider that this repository is stored in a server (like github), normaly the main repository are called origin. We want to develop some new things on this project so we clone this repository on our local pc:

              origin /master ->  A—B---C—D 

                                                      II   clone

              local /master ->  A—B---C—D

After some changes and commits you will have this history:

              origin /master ->  A—B---C—D 

                                                      II  

              local /master ->  A—B---C—D---E—F---G

And then you will push this changes to origin repository to update it:

              origin /master ->  A—B---C—D---E—F---G

                                                      II   push origin

              local /master ->  A—B---C—D---E—F---G

This works perfectly if you are the only one that are make changes in this repository and master branch, probably when you develop something you need to work with some other people and changes the same branch will cause conflicts:

              local_one/master ->  A—B---C—D---E—F---G

                                                      II   push origin

              origin/master ->  A—B---C—D

                                                      II   push origin

              local_two/master ->  A—B---C—D---X—Y---Z

In this case the rule is: who comes first rule them all and the others are no longer accepted to push their changes in the master branch. Branches are the way to prevents conflicts, instead of doing all of the changes in a single branch you made that in a new different branch. Like this:

              local_one/new.branch.one ->  A—B---C—D---E—F---G

                                                      II   push origin new.branch.one

                    origin/master ->  A—B---C—D

                                                      II   push origin new.branch.two

              local_two/new.branch.two ->  A—B---C—D---X—Y---Z

This will create a different history in the origin repository:

                      new.branch.one ->  E—F---G

                                                       /

              master ->  A—B---C—D

                                                       <br>

                     new.branch.two ->  X—Y---Z

Now you can try to merge this branch into master with Pull Request on github or with merge command, if everything goes ok in the end you get a new history of the master branch:

              master ->  A—B---C—D---X—Y---Z—E---F—G

or:

            master ->  A—B---C—D---E—F---G—X---Y—Z

The order is determined by the first branch merged.

The branching is used also when a repository are made by only one developer, normally one branch correspond to a new functionality or a new update (also called patch) of the software this can help to organize the project. it also provide a better way to test new functionality of the software without touch the master code and if this functionality are no more requested you can simply delete the branch where are stored without too many efforts.

Branching and cloning are completly different one to another. I already explain what branches are used for, the clone is simply a copy and paste of a project. You are not going to use more than one clone for a project, but you will use a thousand of branches for a project. This is an example of a repository history with many branches:

2019-03-23_541x267.png

The difference from copy a directory and clone a repository is simpy that the second will copy the history of the repository and create all metadata to use git protocol inside that folder.

I hope I have solved your doubts, otherwise ask for more :grin: 

-Gabriele-

Mark helpful posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!

9 Likes

Yes, you were extremely helpful. Thank you! I definitely have a better understanding of the distinction between branching and cloning now.

My question now is: since a branch is for testing things out that may or may not be merged in to the master, is it possible to have entire files in a branch that won’t be included in the master (unless the branch merges)? Because in Sourcetree, it doesn’t seem like I can add a branch exclusive file. I can only commit a file to the master.

1 Like

Yes, you can do whatever you want in a branch like: delete, create and transform that are files or folders. I dont know how it works sourcetree, but I think this tool it is made to manage git flow and not to edit and create new things. So you need a file manager to create a new files or directories and an editor to edit files.

-Gabriele-

Mark helpfull posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!

1 Like