Skip to content

Git Branch Explained – How to Use and Manage Git Branches

Git branch provides a helpful way to create a subsection of your main work.

In other words, Git branch allows you to try new experiments on a secondary line of development without affecting your main work.

Below are the main ways developers use and manage Git branches.

How to Clone a Git Repository and All Its Branches

Git Cloning is mainly about getting (downloading) a copy of a .git repository.

For instance, you may need a copy of a project you intend to modify. In such a case, getting a clone of the project’s .git directory puts in your possession all the file versions the project’s contributors have committed to the .git repository.

To clone a repository, run the following:

Terminal
git clone <theGitRepoURL> <state the place to put the cloned git folder>

By so doing, Git will download a copy of the specified .git repository into the place you’ve identified.

How to Clone a Single Remote Branch While Tracking All the Other Ones

Terminal
git clone -b <remote-branch-name> <remote-repo-url>

The command above tells Git to download only the specified remote branch while tracking the others.

How to Clone a Single Remote Branch without Tracking All the Other Ones

Terminal
git clone -b <remote-branch-name> --single-branch <remote-repo-url>

The command above tells Git to download only the specified remote branch without tracking the others.

How to Check the Branches in a Project’s Local Git Repository

Terminal
git branch

The command above will display all the branches in your local Git repository.

Alternatively, you can also use:

Terminal
git branch --list

How to Check the Branches in a Project’s Remote Git Repository without Displaying Their Reference Details

Terminal
git branch -r

The command above will display all the branches in your remote Git repository. But it will omit the branches’ reference details.

Git prefixes a remote branch with its repo’s URL name to distinguish it from your local branch. For instance, origin/main indicates that the main branch lives in a remote repository whose URL’s name is origin.

How to Check the Branches in a Project’s Remote Git Repository While Also Displaying Their Reference Details

Terminal
git ls-remote

The command above will display all the branches in your remote Git repository and their reference details (including commit hashes).

How to Check the Branches in a Project’s Remote Git Repository While Also Displaying Their Hashes

Terminal
git ls-remote --heads

The command above will display all the branches in your remote Git repository and their commit hashes.

How to Check All the Branches in a Project’s Local and Remote Git Repository

Terminal
git branch -a

The command above will display all the branches in your project’s local and remote repos.

How to Create a New Git Branch

Terminal
git branch new-branch-name

The code above will create a new branch called new-branch-name.

By default, Git creates a new branch from the HEAD point. However, you can specify the exact branch from which you want to create your new branch.

For instance, the command below tells Git to create new-branch-name from existing-branch-name.

Terminal
git branch new-branch-name existing-branch-name

Keep in mind that you can also create a new branch from a specific commit like so:

Terminal
git branch new-branch-name 7b804hrw

The code above instructs Git to create new-branch-name from the commit with the hash number 7b804hrw.

CodeSweetly ads

Express Your Love for Coding!

Explore CodeSweetly's Shop for an array of stylish products to enhance your coding experience.
Shop now

How to Rename the HEAD Branch

Terminal
git branch -m branch-new-name

The code above will rename the HEAD (active) branch to branch-new-name.

How to Rename a Non-HEAD Branch

Terminal
git branch -m branch-old-name branch-new-name

The code above will rename branch-old-name to branch-new-name.

How to Switch between Git Branches

You can use the checkout or switch command to switch between your project’s branches.

Git checkout vs. switch: What’s the difference?

git checkout is a versatile command for doing many things, such as switching between branches, restoring files, and switching between commits.

However, we use git switch only to switch your project’s HEAD from one branch to another.

Below are popular ways of using git checkout and git switch to switch between branches.

How to switch from one branch to another

Terminal
git checkout branch-name

The command above instructs Git to switch to branch-name.

Alternatively, you can also switch branches like so:

Terminal
git switch branch-name

After the switch, branch-name will become the HEAD (active) branch. In other words, the HEAD pointer will move to branch-name.

Suppose you switched from branch A to branch B. In that case, you can use the previous branch shorthand (-) to switch back to branch A:

Terminal
git switch -

How to create and switch immediately to a new branch

Terminal
git checkout -b new-branch-name

The switch command equivalence of the code above is:

Terminal
git switch -c new-branch-name

Note that the -b and -c flags tell Git to create a branch before switching to it. In other words, git checkout -b new-branch-name is a shorthand for:

Terminal
git branch new-branch-name
git checkout new-branch-name

While git switch -c new-branch-name is a shorthand for:

Terminal
git branch new-branch-name
git switch new-branch-name

By default, Git creates a new branch from the HEAD. However, you can specify the exact branch from which you want to create your new branch.

For instance, the command below tells Git to create new-branch-name from existing-branch-name.

Terminal
git checkout -b new-branch-name existing-branch-name

Here is the switch alternative:

Terminal
git switch -c new-branch-name existing-branch-name

Keep in mind that you can also create a new branch from a specific commit like so:

Terminal
git checkout -b new-branch-name 7b804hrw

The code above instructs Git to create new-branch-name from the commit with the hash number 7b804hrw.

Here’s the switch equivalence:

Terminal
git switch -c new-branch-name 7b804hrw

So, now that you know how to switch between Git branches, we can discuss switching between commits.

How to Switch between Git Commits

You can use the git checkout command to switch from one commit history to another.

By default, Git will only allow you to switch to another commit if you have committed all your changes.

Here’s an example:

Terminal
git checkout z8d2f115010634ea4ae0a2670p7aec61b394c306

The code above tells Git to switch to the commit having a hash string of z8d2f115010634ea4ae0a2670p7aec61b394c306.

Suppose you only wish to restore an old version of a specific commit’s file. In such a case, indicate the file after the commit’s hash like so:

Terminal
git checkout z8d2f11 App.js

The code above tells Git to restore z8d2f11’s App.js file only—not all z8d2f11’s commits.

How to Exit the Detached State of a Project’s HEAD Pointer

You can exit the detached state by switching to any of your project’s branches.

Here’s an example:

Terminal
git checkout main

The code above tells Git to switch to the main branch.

How to Compare the Differences between Two Git Branches

You can compare the difference between two branches like so:

Terminal
git diff first-branch-name..second-branch-name

The code above tells Git to show the differences between first-branch-name and second-branch-name.

Therefore, Git will display a result like the following image:

Example of a git diff command's
output

The git diff command returns the difference between two package.json files.

Let’s discuss what the git diff’s output means.

1. Files in which Git found some differences

Terminal
diff --git a/package.json b/package.json

Git used the output above to indicate the files in which it found some differences.

In other words, Git found some differences in the first-branch-name and the second-branch-name’s package.json files.

Note the following:

  • a/package.json refers to the package.json of the first branch you specified in your git diff first-branch-name..second-branch-name command. So, in this case, it references first-branch-name.
  • b/package.json refers to the package.json of the second branch you specified in your git diff first-branch-name..second-branch-name command. So, in this case, it references second-branch-name.
CodeSweetly ads

Design in Figma, launch in Webflow

Bring your static designs to life with IX2, wire up content using our powerful CMS, and one-click publish onto the fastest hosting infrastructure.
Find out more

2. Information about the files in which Git found some differences

Terminal
index 44c842a..ffb1440 100644

Here is what the above index information means:

  • 44c842a..ffb1440 indicates the SHA-1 checksum of the two files where Git found some differences.
  • 100644 refers to the mode of the files represented by the SHA-1 checksum 44c842a and ffb1440.

3. File a’s marker

Terminal
--- a/package.json

The line above indicates the maker Git used to reference the differences in the first-branch-name’s package.json file.

In other words, Git used --- (minus signs) to mark the differences in the first-branch-name’s package.json file.

4. File b’s marker

Terminal
+++ b/package.json

The line above indicates the maker Git used to reference the differences in the second-branch-name’s package.json file.

In other words, Git used +++ (plus signs) to mark the differences in the second-branch-name’s package.json file.

5. The chuck header

Terminal
@@ -27,7 +27,7 @@

Here’s what the chuck header above means:

  • -27 in -27,7 indicates that Git started the chuck’s extraction from the 27th line of the first-branch-name’s package.json file.
  • 7 in -27,7 implies that Git extracted a chuck of 7 lines. In other words, Git extracted lines 27, 28, 29, 30, 31, 32, and 33 from file a.
  • +27 in +27,7 signifies that Git started the chuck’s extraction from the 27th line of the second-branch-name’s package.json file.
  • 7 in +27,7 implies that Git extracted a chuck of 7 lines. In other words, Git extracted lines 27, 28, 29, 30, 31, 32, and 33 from file b.
  • The @@symbols mark the beginning and end of the chuck header.

6. The context above line 27

Terminal
"prism-react-renderer": "^1.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",

The output above is the three lines of context above the difference Git found.

In other words, Git displayed some context above the line where it found a difference.

7. File a’s difference

Terminal
- "react-player": "^2.12.0",

The output above shows the difference Git found in the first-branch-name’s package.json file.

CodeSweetly ads

Express Your Love for Coding!

Explore CodeSweetly's Shop for an array of stylish products to enhance your coding experience.
Shop now

8. File b’s difference

Terminal
+ "react-player": "^2.11.2",

The output above shows the difference Git found in the second-branch-name’s package.json file.

9. The context below line 27

Terminal
"uniqid": "^5.4.0",
},
"browserslist": {

The output above is the three lines of context below the difference Git found.

In other words, Git displayed some context below the line where it found a difference.

How to Merge Git Branches

Git merging allows you to merge another branch’s changes into the HEAD (current) branch.

Here’s the merge syntax:

Terminal
git merge name-of-branch-containing-your-changes

The code above instructs Git to merge name-of-branch-containing-your-changes with the HEAD (current) branch.

Git may prompt you to enter a commit message on running the merge command. You can either provide a new message or accept Git’s default.

Afterward, close the editor’s window to save the commit message.

An alternate way to merge changes from one branch into the HEAD branch is to use the rebase command like so:

Terminal
git rebase name-of-branch-containing-your-changes

How to Delete a Git Branch

Terminal
git branch -d branch-to-delete

The code above will delete the branch called branch-to-delete.

Note that you cannot delete the branch that is currently the HEAD branch.

Likewise, Git does not permit deleting a branch containing unmerged changes.