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:
With Git:
new-design
to work on the design without affecting the main branch.small-error-fix
.small-error-fix
into the main branch.new-design
branch and complete the design work.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!
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:
[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:
[user@localhost] $ |
git branch |
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.
[user@localhost] $ |
git checkout 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.
<!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:
[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:
We need to add both files to the staging environment for this branch.
[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:
[user@localhost] $ |
git status |
We’re satisfied with our changes, so we’ll commit them to the branch.
[user@localhost] $ |
git commit -m “Added image to Hello World” |
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. |
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:
[user@localhost] $ |
ls |
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.
[user@localhost] $ |
git checkout master |
The new image is not part of this branch. List the files in the current directory again:
[user@localhost] $ |
ls |
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.
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.
[user@localhost] $ |
git checkout -b 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.
<!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:
[user@localhost] $ |
git status |
Stage the file and commit the changes:
[user@localhost] $ |
git add index.html |