Vishal Sharma
Vishal Sharma

Reputation: 83

How is a branch different from a folder, in a GitHub repository?

I'm new to GitHub, and I don't exactly understand what a branch is. I got a feeling it is like a folder. Please explain: what exactly is the difference between folder and a branch?

Upvotes: 2

Views: 6508

Answers (4)

Kartick Vaddadi
Kartick Vaddadi

Reputation: 4998

As Melebius said, they both are ways to keep two different versions of your code.

A folder is a feature of the filesystem.

A branch is a feature of version control systems, which means that Git tracks at which point in the version history you created it.

Git lets you merge a branch back into mainline, merging them. You might create a branch to work on something that may or may not work out, making several commits till you get it to a working state, at which point you merge.

Or move individual commits from mainline to the branch or vice-versa. You might use this when you made a bug fix to the branch that you later realise also applies to mainline.

All these benefits disappear with folders.


On the other hand, folders have their own benefits like:

  • You already know how to use them.
  • If you want to diff two folders, recursive diff tools exist. But to diff two Git branches, you need a tool that's made for Git, which yours may not be.
  • You can open projects in two folders in two windows of your IDE and manually compare them, or apply changes one by one from one folder to another.
  • With branches, I sometimes accidentally make commits to the wrong branch. With folders, I've learnt to check which folder I've committed to.
  • You can use Spotlight on the Mac, or grep on the command line, to search for something, like a class name, and it will find across all folders, but not across all branches.
  • Branches require you to follow a Git-specific procedure to track a remote branch, or to create a remote branch when you push a commit that creates a new local branch.
  • If your repo has gotten too big, it's easy to see which folders are taking up space.

Upvotes: 1

David Deutsch
David Deutsch

Reputation: 19025

Good answers so far; I'll add my two cents. You already know what a folder is, so next we have to think about what a commit is. A commit is a snapshot of a folder - specifically, the root folder of your repository at some point in time. A branch is simply a pointer to a commit. Take a look at the following image from the excellent Git book:

enter image description here

In this picture, the gray boxes are commits. You can see that each one has its own snapshot of the main folder. The orange boxes are branches - note how they are just pointers to a commit. In this case, there are two branches (v1.0 and master), and they both happen to point to the same commit. HEAD points to the branch you are currently on. So if you were to make a new commit in the above case, master would point to that new commit, whose parent would be f30ab. v1.0 would still point to f30ab.

So while branches can somewhat act like folders in the sense that different branches can "contain" different (versions of) files, in reality it is different commits that contain these different (versions of) files, and a branch simply points to a commit.

BTW, as others have mentioned, you should really read the Git book. Working with Git is so much more pleasurable when you know the fundamentals.

Upvotes: 5

Melebius
Melebius

Reputation: 6695

In a way, they are both sets of files. However, their concepts come from different environments and the usage is very different.

A folder is a feature of file system. I hope I don’t have to explain more since it’s a basic computing concept. You can nest folders and they have no special features regarding version control systems (mainly history).

A branch is a feature of version control systems. It allows you to manage more development tasks at a time. They contain history and you can check at any time from which commit they diverted. However, you cannot nest them in Git.

I would start to learn how to use branches in Git on https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging.

Upvotes: 2

Junaid
Junaid

Reputation: 1004

read the article to understand how github works.

A branch is like a work in progress, you make a copy of your main code (or part of it) make changes (add new feature or fix a bug) and when you have tested it that it works perfectly fine you merge it back to main branch. So whole idea is basically have a development branch and a stable working branch which you can use if something goes wrong during changes.

Upvotes: 0

Related Questions