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 Check the Branches in a Project's Local Git Repository
git branch
The command above will display all the branches in your local Git repository.
Alternatively, you can also use:
git branch --list
Git will mark the current branch with an asterisk (*
).
How to Check the Branches in a Project's Remote Git Repository without Displaying Their Reference Details
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
.
The -r
flag is the shorthand notation for --remotes
.
How to Check the Branches in a Project's Remote Git Repository While Also Displaying Their Reference Details
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
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
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
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
.
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:
git branch new-branch-name 7b804hrw
The code above instructs Git to create new-branch-name
from the commit with the hash number 7b804hrw
.
When you use the git branch
command to create a new branch, Git will not automatically switch the HEAD pointer to the new branch. You need to use the git checkout
command to switch branches.
How to Rename the HEAD Branch
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
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
git checkout branch-name
The command above instructs Git to switch to branch-name
.
Alternatively, you can also switch branches like so:
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
.
Use the git reflog
command to see your HEAD
pointer's history.
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:
git switch -
How to create and switch immediately to a new branch
git checkout -b new-branch-name
The switch
command equivalence of the code above is:
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:
git branch new-branch-name
git checkout new-branch-name
While git switch -c new-branch-name
is a shorthand for:
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
.
git checkout -b new-branch-name existing-branch-name
Here is the switch
alternative:
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:
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:
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:
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:
git checkout z8d2f11 App.js
The code above tells Git to restore z8d2f11
's App.js
file only—not all z8d2f11
's commits.
Whenever you switch to a commit, the HEAD
pointer detaches from the branch it was previously on and moves to the commit history you switched to. In other words, whenever you checkout a commit, the project's HEAD
pointer switches to a "detached" state.
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:
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:
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
.
Suppose second-branch-name
is your HEAD branch (the branch you are currently working in). In that case, you can shorten your diff
command by specifying only the non-HEAD branch you wish to use for comparison.
Here's an example:
git diff first-branch-name
The snippet above tells Git to compare the first-branch-name
with the HEAD
branch.
Therefore, Git will display a result like the following image:
Let's discuss what the git diff
's output means.
1. Files in which Git found some differences
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 thepackage.json
of the first branch you specified in yourgit diff first-branch-name..second-branch-name
command. So, in this case, it referencesfirst-branch-name
.b/package.json
refers to thepackage.json
of the second branch you specified in yourgit diff first-branch-name..second-branch-name
command. So, in this case, it referencessecond-branch-name
.
You can add the --no-prefix
flag to the git diff
command if you wish to omit the a/
and b/
prefixes from the diff
's output.
For instance, suppose you entered the following command on your terminal:
git diff first-branch-name..second-branch-name --no-prefix
Git will remove the prefixes from its output like so:
diff --git package.json package.json
2. Information about the files in which Git found some differences
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 checksum44c842a
andffb1440
.
Git uses mode 100644
for normal files, 100755
for executable files, and 120000
for symbolic links.
3. File a's marker
--- 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.
Git uses a red color code to indicate the differences marked with the minus sign.
4. File b's marker
+++ 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.
Git uses a green color code to indicate the differences marked with the plus sign.
5. The chuck header
@@ -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 thefirst-branch-name
'spackage.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 thesecond-branch-name
'spackage.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.
- Git uses a blue color code to indicate the chuck header.
- A chuck header begins a chuck of extracted lines.
6. The context above line 27
"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.
Git uses a white color code to indicate the context around the differences.
7. File a's difference
- "react-player": "^2.12.0",
The output above shows the difference Git found in the first-branch-name
's package.json
file.
The minus (-
) symbol preceding the output above is file a's marker.
8. File b's difference
+ "react-player": "^2.11.2",
The output above shows the difference Git found in the second-branch-name
's package.json
file.
The plus (+
) symbol preceding the output above is file b's marker.
9. The context below line 27
"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.
- Git uses a white color code to indicate the context around the differences.
- Suppose nothing shows after running the
diff
command. In that case, it implies that the two branches have the same content.
To make each chunk less cumbersome, add the --color-words
flag to the git diff
command (that is, git diff --color-words
). By so doing, each chunk will include just the modified words and their context—not each modified line and its context.
How to Merge Git Branches
Git merging allows you to merge another branch's changes into the HEAD
(current) branch.
Before invoking the merge command, ensure the active branch is the branch you want to merge into.
In other words, switch to the branch you wish to update before running the merge
command.
Here's the merge syntax:
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:
git rebase name-of-branch-containing-your-changes
- Beginners should avoid the
rebase
command asgit rebase
re-writes a project's history whilegit merge
doesn't. - See Bitbucket's merging vs. rebasing article to learn the differences between
git merge
andgit rebase
.
How to Delete a Git Branch
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.
You can forcefully delete a branch with unmerged alterations by changing the lowercase -d
flag to uppercase like so:
git branch -D branch-to-delete
Overview
This article discussed the main ways to use and manage Git branches.
Get the CodeSweetly free Git PDF cheat sheet.
Use it to easily remember the Git commands you need for your projects.