Curriculum
Course: Git
Login
Text lesson

Git Branch

Working with Git Branches

In Git, a branch is an independent version of the main repository, allowing you to work on separate tasks or features without affecting the main branch.

Without Git:

  1. Make copies of all relevant files to prevent impacting the live version.
  2. Work on the design and discover dependencies that require changes in other files.
  3. Make additional copies of dependent files, ensuring all references are correct.
  4. Encounter an unrelated error elsewhere that needs immediate attention.
  5. Save your work and note the filenames of the copies you were using.
  6. Fix the unrelated error, then return to complete the design work.
  7. Copy or rename files to update the live version with the new design.
  8. Later, realize that the unrelated error fix was not applied to the new design because the files were copied before the fix.

With Git:

  1. Create a new branch called new-design to work on the design without affecting the main branch.
  2. Encounter an unrelated error needing urgent attention.
  3. Create a new branch from the main branch called small-error-fix.
  4. Fix the unrelated error and merge small-error-fix into the main branch.
  5. Return to the new-design branch and complete the design work.
  6. Merge the new-design branch into the main branch, incorporating the unrelated error fix.

Branches enable you to work on different aspects of a project independently, and you can easily switch between branches without interference. Git’s branching is efficient and quick!

New Git Branch

Let’s add some new features to our index.html page.

Since we’re working in our local repository and want to avoid disrupting or potentially damaging the main project, we’ll create a new branch:

Example

[user@localhost] $

git branch hello-world-images

We have now created a new branch called “hello-world-images.”

Let’s verify that the new branch has been created:

Example

[user@localhost] $

git branch
 hello-world-images
* master

We can see the new branch “hello-world-images,” but the * next to master indicates that we are currently on the master branch.

The checkout command is used to switch branches, moving us from the current branch to the one specified at the end of the command.

Example

[user@localhost] $

git checkout hello-world-images
Switched to branch 'hello-world-images'

We’ve now switched our workspace from the master branch to the new branch.

Open your preferred editor and make some changes. For this example, we’ve added an image (img_hello_world.jpg) to the working folder and inserted a line of code into the index.html file.

Example

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel=”stylesheet” href=”bluestyle.css”>
</head>
<body>

<h1>Hello world!</h1>
<div><img src=”img_hello_world.jpg” alt=”Hello World from Space”
style=”width:100%;max-width:960px”
>
</div>
<p>This is the first file in my new Git Repo.</p>
<p>A new line in our file!</p>

</body>
</html>

We’ve made changes to a file and added a new file in the working directory (which is the same directory as the main branch).

Now, check the status of the current branch:

Example

[user@localhost] $

git status

On branch hello-world-images

Changes not staged for commit:

  (use “git add …” to update what will be committed)

  (use “git restore …” to discard changes in working directory)

           modified: index.html

Untracked files:

    (use “git add …” to include in what will be committed)

             img_hello_world.jpg

no changes added to commit (use “git add” and/or “git commit -a”)

Here’s what’s happening:

  • Changes have been made to index.html, but the file is not yet staged for commit.
  • The file img_hello_world.jpg is not being tracked.

We need to add both files to the staging environment for this branch.

Example

[user@localhost] $

git add –all

Using --all instead of specifying individual filenames will stage all changes (new, modified, and deleted) files.

Now, check the status of the branch:

Example

[user@localhost] $

git status
On branch hello-world-images
Changes to be committed:
  (use “git restore –staged …” to unstage)     new
file: img_hello_world.jpg     modified: index.html

We’re satisfied with our changes, so we’ll commit them to the branch.

Example

[user@localhost] $

git commit -m “Added image to Hello World”
[hello-world-images 0312c55] Added image to Hello World
2 files changed, 1 insertion(+)
create mode 100644 img_hello_world.jpg

Now we have a new branch that differs from the master branch.

Note: Using the -b option with checkout will create a new branch and switch to it if it doesn’t already exist.

Switching Between Branches

Let’s see how quick and easy it is to work with different branches and how well it functions.

We are currently on the hello-world-images branch. Since we’ve added an image to this branch, let’s list the files in the current directory:

Example

[user@localhost] $

ls
README.md  bluestyle.css  img_hello_world.jpg  index.html

We can see the new file img_hello_world.jpg, and if we open the HTML file, we can confirm that the code has been altered as expected.

Now, let’s observe what happens when we switch to the master branch.

Example

[user@localhost] $

git checkout master
Switched to branch 'master'

The new image is not part of this branch. List the files in the current directory again:

Example

[user@localhost] $

ls
README.md  bluestyle.css  index.html

img_hello_world.jpg is no longer present, and the HTML file has reverted to its previous state.

Isn’t it great how easy it is to work with branches? This approach allows you to manage different tasks and changes seamlessly.

Emergency Branch

Now, imagine that we’re not finished with hello-world-images, but we need to fix an error on master.

To avoid disrupting the master branch directly or interfering with the ongoing work on hello-world-images, we’ll create a new branch to address the emergency.

Example

[user@localhost] $

git checkout -b emergency-fix
Switched to a new branch 'emergency-fix'

We’ve now created and switched to a new branch from master. We can safely fix the error without affecting the other branches.

Let’s go ahead and fix our hypothetical error.

Example

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel=”stylesheet” href=”bluestyle.css”>
</head>
<body>

<h1>Hello world!</h1>
<p>This is the first file in my new Git Repo.</p>
<p>This line is here to show how merging works.</p>

</body>
</html>

We’ve made changes to this file, and we need to get those changes onto the master branch.

First, check the status:

Example

[user@localhost] $

git status
On branch emergency-fix
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

Stage the file and commit the changes:

Example

[user@localhost] $

git add index.html
git commit -m "updated index.html with emergency fix"
[emergency-fix dfa79db] updated index.html with emergency fix
1 file changed, 1 insertion(+), 1 deletion(-)