bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Git/Git Tutorial
Git•Git Tutorial

Git Branch

Concept visual

Git Branch

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Change Platform:

GitHub

Bitbucket

GitLab

What is a Git Branch?

In Git, a branch is like a separate workspace where you can make changes and try new ideas without affecting the main project. Think of it as a "parallel universe" for your code.

Why Use Branches?

Branches let you work on different parts of a project, like new features or bug fixes, without interfering with the main branch.

Common Reasons to Create a Branch

Developing a new feature

Fixing a bug

Experimenting with ideas

Example: With and Without Git

Let's say you have a large project, and you need to update the design on it. How would that work without and with Git:

Without Git:

Make copies of all the relevant files to avoid impacting the live version Start working with the design and find that code depend on code in other files, that also need to be changed! Make copies of the dependant files as well. Making sure that every file dependency references the correct file name

EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!

Save all your files, making a note of the names of the copies you were working on Work on the unrelated error and update the code to fix it Go back to the design, and finish the work there Copy the code or rename the files, so the updated design is on the live version (2 weeks later, you realize that the unrelated error was not fixed in the new design version because you copied the files before the fix)

With Git:

Formula

With a new branch called new - design, edit the code directly without impacting the main branch
EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!

Formula

Create a new branch from the main project called small - error - fix
Fix the unrelated error and merge the small - error - fix branch with the main branch
You go back to the new - design branch, and finish the work there
Merge the new - design branch with main (getting alerted to the small error fix that you were missing)

Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. You can even switch between branches and work on different projects without them interfering with each other. Branching in Git is very lightweight and fast!

Creating a New Branch

Let's say you want to add a new feature. You can create a new branch for it. Let add some new features to our index.html page. We are working in our local repository, and we do not want to disturb or possibly wreck the main project.

So we create a new branch

Example git branch hello-world-images

Now we created a new branch called "

Formula

hello - world - images

"

Listing All Branches

Let's confirm that we have created a new branch. To see all branches in your repository, use:

Example git branch hello-world-images

  • master

Formula

We can see the new branch with the name "hello - world - images", but the
  • beside master specifies that we are currently on that branch. Switching Between Branches checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:

Example git checkout hello-world-images

Switched to branch 'hello-world-images'

Now you can work in your new branch without affecting the main branch.

Working in a Branch

Now we have moved our current workspace from the master branch, to the new branch Open your favourite editor and make some changes. For this example, we added an image (img_hello_world.jpg) to the working folder and a line of code in the index.html file:

Example

<!DOCTYPE html> <html> <head>

Formula

< title > Hello World!</title >

<link rel="stylesheet" href="bluestyle.css"> </head> <body>

Formula

< h1 > Hello world!</h1 >

<div><img src="img_hello_world.jpg" alt="Hello World from Space"

style="width:100%;max-width:960px"></div>

Formula

< p > This is the first file in my new Git Repo.</p >
< p > A new line in our file!</p >

</body> </html> We have made changes to a file and added a new file in the working directory (same directory as the main branch ). Now check the status of the current branch

Example git status

On branch hello-world-images

Previous

Git Help

Next

Git Branch Merge