Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between repository and branch in git?

Tags:

git

Someone who is well versed in git could help me to understand the difference between repo and branch. I am recently introduced to git and having a bit hard time to understand them. I was told to clone a remote repo (e.g. foo) to my local box. Then create a local branch out of it. Work (update/create/delete files) on the branch and add/commit/push to the remote server (e.g. bitbucket). After 2nd set of eyes review the branch and says ok. Then it gets merged to development or master branch.

Then what role, a repository plays in this picture? To me all the operations I ran is against branch...

like image 567
DaeYoung Avatar asked Dec 30 '16 15:12

DaeYoung


People also ask

What is a repository in a Git?

A Git repository tracks and saves the history of all changes made to the files in a Git project. It saves this data in a directory called . git , also known as the repository folder. Git uses a version control system to track all changes made to the project and save them in the repository.

How many branches are there in a repository?

Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

What are the three types of branching in Git?

The two primary branches in Git flow are main and develop. There are three types of supporting branches with different intended purposes: feature, release, and hotfix.

Is repo the same as repository?

A repo (short for repository) is a storage for all the changes and files from a project, enabling developers to “version control” the project's assets throughout its development stage.


2 Answers

A repository is your whole project (directories and files) that you clone on your computer. A branch is a version of your repository, or in other words, an independent line of development.

A repository can contain multiple branches, which means there are multiple versions of the repository. The purpose of versioning your code after all, is that you can work on multiple aspects of your project at the same time - each of those evolving in different branches. Git uses the expression "working tree" (representing your workbench) alongside "branches".

Related: If you want to know more about the local and remote branches.


Concerning the way of dealing with branches in your initial question:

Clone a remote repo (e.g. foo) to my local box. Then create a local branch out of it. Work (update/create/delete files) on the branch and add/commit/push to the remote server

This is a nice and clean way to work with git. I suggest you always proceed like this :

----A---B---C--     (REMOTE, master)

            |
            |       (Pull to local : `git pull origin master`)
            v

----A---B---C--     (LOCAL, master)

Then create a branch from your local master (git checkout -b branch1), work on it, commit your changes and push to remote :

----A---B---C------     (REMOTE, master)

----A---B---C---D--     (REMOTE, branch1)

               ^
               |
               |        (Push to remote : `git push origin branch1`)

----A---B---C------     (LOCAL, master)
             \
              D----     (LOCAL, branch1)

Then, when you're happy with your feature/fix/whatever, you can merge branch1 into master.

like image 198
Val Berthe Avatar answered Oct 22 '22 17:10

Val Berthe


A repository contains all the information about your project, including a history of all the changes. Each change is entered into the repository in the form of a "commit".

In order to show the difference between branches and repositories I will describe the process you mentioned and point out along the way the branches and the repositories being used. This is not intended to be a git tutorial, just an example so we can talk about branches and repos.

Typically there exists a remote repository which users can get a copy of, and to which they can submit changes. No actual development work is done directly in the remote repo.

A user gets their first copy of a remote repository by cloning it. This will create a local repository on the users machine. The local repo is a complete copy of the remote repo.

A branch is simply a pointer to one of the commits in the repository. In the simplest case, there is the default branch called "master", and master points to the most recent commit. Each commit is identified by a number, which is a hash of the repo at that moment. For example in this case master might point to commit:

2d2cd6cf6f634e88ad19fa454fdcd2e1c26009c7

A developer can clone the remote repo, and checkout branch master. Then create and checkout development branch, (e.g. featureX-dev).

git checkout -b featureX-Dev

At this point both branches (master and featureX-Dev) point to the same commit. Make changes to your files. Commit the changes to the local copy of branch featureX-dev. Now, in your local repository, branch featureX-Dev points to a newer commit than master does. Push branch featueX-dev to the remote repo so it can be reviewed.

git push -u origin featureX-dev

The -u is used the first time the branch is pushed to the remote to tell git you want to track this branch.

Other developers (who have already cloned the remote repo) can get branch featureX-dev from the remote repository by performing a pull.

After they review it and tell you it's ok, then you can merge branch featureX-dev with your copy of master in your local repo, and push master.

But wait! What if some other developer has already pushed their changes to the remote master?

You checkout master in your local repo

git checkout master

Then pull master from the remote

git pull origin master

You have the new changes that someone else made, and master points to their last commit. Now you can merge your dev branch into the local copy of master

git merge featureX-dev

If no one has changed master there is no harm done. The merge will just add your commits to the master branch. Resolve conflicts if any are created, and then push master back to the remote.

like image 33
Randy Leberknight Avatar answered Oct 22 '22 17:10

Randy Leberknight