7 min read

Mastering Git Branches Tutorial

Table of Contents

Git Branches Tutorial

Branches are a fundamental concept in Git, essential for productive and efficient version control. This guide will walk you through the core aspects of working with branches, from creation to deletion, merging, and rebasing.

Core Concepts

Before diving into the specifics, let’s clarify two core concepts:

Head Branch

The head branch is the currently active or checked-out branch. You can have many branches in your repository, but only one is active at a time. When you use commands like git status, you’ll see the name of the head branch, which indicates where new commits will be created.

Local vs. Remote Branches

When working with branches, you’re primarily working with local branches in your local Git repository. Remote branches, typically on platforms like GitHub, GitLab, or Bitbucket, are used for synchronizing changes with others. The majority of your work happens on local branches, while remote branches facilitate collaboration.

Creating Branches

To create a new branch, use the git branch command:

git branch <branch_name>

This creates a new branch based on the currently checked-out revision.

To create a branch based on a specific revision, use the revision hash:

git branch <branch_name> <revision_hash>

Remember that you can only create branches in your local repository. Creating branches on a remote repository involves publishing an existing local branch.

Switching Branches

Switching branches allows you to change the context of your work. Use the git checkout command followed by the branch name:

git checkout <branch_name>

Alternatively, use the git switch command, which is specifically designed for switching branches and is less ambiguous than git checkout:

git switch <branch_name>

The currently checked-out branch is where new commits will be created.

Renaming Branches

You can easily rename branches:

Renaming the Head Branch

To rename the currently checked-out branch, use:

git branch -m <new_branch_name>

Renaming a Non-Head Branch

To rename a branch that is not currently checked out, use:

git branch -m <old_branch_name> <new_branch_name>

Renaming remote branches is more complex and involves deleting the old branch and pushing the renamed branch.

Publishing Branches

You can’t directly create a new branch on a remote repository, but you can publish an existing local branch to a remote repository. Use the git push command with the -u flag to establish a tracking connection:

git push -u <remote_name> <branch_name>

The -u flag sets up a tracking connection, simplifying future push and pull operations.

Tracking Branches

Tracking connections establish a relationship between local and remote branches. This simplifies push and pull operations by eliminating the need to specify the remote and branch names each time.

Tracking a Remote Branch

To track a remote branch, you can use either git branch --track or git checkout --track:

git branch --track <local_branch_name> <remote_name>/<remote_branch_name>

or

git checkout --track <remote_name>/<remote_branch_name>

Tracking a remote branch downloads the remote branch to your local repository and sets up a tracking connection.

Pulling and Pushing Branches

Once a tracking connection is established, you can use the simple git pull and git push commands to sync your branches:

git pull
git push

Git will automatically pull from or push to the tracked remote branch.

Git also provides information about the divergence of local and remote branches. The git branch -v command shows how many commits a local branch is ahead or behind its remote counterpart.

Deleting Branches

Branches are not meant to live forever, and it’s important to clean up obsolete branches.

Deleting a Local Branch

To delete a local branch, you must first switch to a different branch:

git switch <another_branch>

Then, delete the local branch:

git branch -d <branch_name>

If a branch contains unmerged changes, use the -D flag to force deletion (be cautious with this option):

git branch -D <branch_name>

Deleting a Remote Branch

To delete a remote branch, use:

git push <remote_name> --delete <branch_name>

When deleting a branch, remember to check if its counterpart branch on the other end should also be deleted.

Merging Branches

Merging integrates changes from one branch into the currently active (head) branch.

  1. Switch to the target branch:

    git switch <target_branch>
    
  2. Merge the source branch:

    git merge <source_branch>
    

This creates a merge commit that combines changes from both branches.

Rebasing Branches

Rebasing is an alternative way to integrate changes. It rewrites the commit history of the target branch to appear as if it were branched off the source branch later, without creating a merge commit.

  1. Switch to the target branch:

    git switch <target_branch>
    
  2. Rebase onto the source branch:

    git rebase <source_branch>
    

Rebasing results in a linear history without the visual branching seen with merges.

Comparing Branches

Comparing branches is useful for understanding the differences between them.

Comparing Local Branches

To compare two local branches, use the git log command:

git log <branch_name1>..<branch_name2>

This shows the commits present in <branch_name2> but not in <branch_name1>.

Comparing Local and Remote Branches

To compare a local branch with its remote counterpart:

git log <remote_name>/<branch_name>..<branch_name>

This shows the commits present in the local branch but not in the remote one.

Summary of Git Branch Commands

CommandDescription
git branch <branch_name>Creates a new branch based on the current HEAD.
git branch <branch_name> <revision_hash>Creates a new branch based on specific commit.
git checkout <branch_name>Switches to the specified branch.
git switch <branch_name>Switches to the specified branch (more explicit than checkout).
git branch -m <new_name>Renames the current branch.
git branch -m <old_name> <new_name>Renames a branch that is not currently checked out.
git push -u <remote> <branch_name>Publishes a local branch to a remote and sets up tracking.
git branch --track <local_name> <remote>/<remote_name>Tracks a remote branch by creating a local branch.
git checkout --track <remote>/<remote_name>Tracks a remote branch using checkout.
git pullPulls changes from the tracked remote branch.
git pushPushes changes to the tracked remote branch.
git branch -d <branch_name>Deletes a local branch.
git branch -D <branch_name>Force deletes a local branch.
git push <remote> --delete <branch_name>Deletes a remote branch.
git merge <branch_name>Merges the specified branch into the current branch.
git rebase <branch_name>Rebases the current branch onto the specified branch.
git log <branch_name1>..<branch_name2>Compares branches showing commits in branch2 but not in branch1.

This comprehensive guide provides a solid foundation for working with branches in Git. Remember to use the appropriate commands for creating, switching, renaming, publishing, tracking, pulling, pushing, deleting, merging, rebasing, and comparing branches. Happy coding!