Using Git Branches – A Guide to Managing Code Versions

Why Use Git Branches?

Git branches allow developers to work on different features, fixes, or experiments without affecting the main project. This enables:

Parallel Development – Multiple team members can work on different features simultaneously.
Safe Experimentation – Test new ideas without modifying the main codebase.
Better Code Organization – Keep work isolated until it's ready to be merged.

This guide will show you how to create, switch, and delete branches effectively.


Viewing Existing Branches

To see all branches in your repository:

git branch

The current branch will have an * next to it.

To see remote branches:

git branch -r

To see both local and remote branches:

git branch -a

Creating a New Branch

To create a new branch without switching to it:

git branch <branch-name>

Example:

git branch feature-login

This creates a new branch without switching to it.

To create a branch and switch to it immediately:

git checkout -b <branch-name>

or using the newer command:

git switch -c <branch-name>

Switching Between Branches

To switch to an existing branch:

git checkout <branch-name>

or using the newer command:

git switch <branch-name>

Example:

git switch feature-login

If you try switching and have uncommitted changes, Git may warn you. To switch while keeping changes:

git stash

Then switch branches and retrieve the stashed changes:

git stash pop

Switching to a Remote Branch and Setting It to Track

If the branch exists on the remote but not locally, use:

git switch --track origin/<branch-name>

Example:

git switch --track origin/feature-login

This creates a local branch named feature-login that tracks the remote branch of the same name, allowing you to pull and push without specifying the remote every time.

You can also do the same with the older checkout command:

git checkout --track origin/<branch-name>

I recommend always using 'tracked' when switching to any branch from origin.


Pushing a New Branch to Remote Repository

If you've created a new branch locally and want to share it with your team:

git push --set-upstream origin <branch-name>

or simply:

git push -u origin <branch-name>

Example:

git push -u origin feature-login

️ Deleting Branches

Delete a Local Branch

To delete a branch that is no longer needed:

git branch -d <branch-name>

If the branch has unmerged changes and you want to force delete:

git branch -D <branch-name>

Delete a Remote Branch

To remove a branch from the remote repository:

git push origin --delete <branch-name>

Next Steps

Now that you understand Git branches, explore more:

Merging Branches – Learn how to merge branches efficiently.
Handling Merge Conflicts – Fix conflicts when merging branches.
Collaborating with GitHub – Work with teams using GitHub repositories.
Collaborating with GitLab – Manage projects and workflows using GitLab's DevOps tools. – Work with teams using remote branches.

Need help? Check the official Git documentation or ask in the comments!