Gyasi Sutton, MD, MPH Physician in Training, Coder at heart. Python, R, Node, and Rust.

Git Branching 101: A Cheatsheet for Effective Collaboration

  Reading Time:

Git branches are a powerful feature that allows developers to work on multiple versions of a project simultaneously. They allow teams to work on different features or bug fixes in parallel without interfering with each other's work. In this article, we'll go over the various Git branch commands that are commonly used in development workflow, and explain how to use them effectively.

The first command we'll cover is git branch. This command lists all of the local branches in the repository, with the current branch indicated by an asterisk (*). This command is useful for quickly checking which branch you're currently on and which branches are available to switch to.

$ git branch
* master
  development
  feature-x

Another useful command is git branch -a, which lists all branches, including remote branches. This command is particularly useful for teams working on a remote repository, as it allows developers to see all branches, both local and remote.

$ git branch -a
* master
  development
  feature-x
  remotes/origin/master
  remotes/origin/development

git branch -r command lists only remote branches. This command is useful when you want to check out a remote branch or when you want to see the branches that are present in the remote repository.

$ git branch -r
  remotes/origin/master
  remotes/origin/development

git branch -v command lists all branches with the last commit on each branch beside the branch name. This command is useful for checking the activity of each branch and identifying which branches are up-to-date.

$ git branch -v
* master     eaedf12 Updated README
  development  bf5c9a6 Fixed bug in login function
  feature-x    3f2d8c1 Added new feature

git branch --list <pattern> command lists branches that match the specified pattern. This command is useful for searching for a specific branch when you have a large number of branches in your repository.

$ git branch --list "feature*"
  feature-x

git for-each-ref --format='%(refname:short)' refs/heads command shows all branches in a more compact format. This command is useful for showing all branches in a simple and concise format.

$ git for-each-ref --format='%(refname:short)' refs/heads
master
development
feature-x

Now let's move on to commands that are used for creating and managing branches. git branch <branch_name> command creates a new branch with the specified name. This command is useful for creating a new branch for a new feature or bug fix.

$ git branch feature-y

git checkout <branch_name> command switches to the specified branch. This command is useful for switching between branches, for example, when you want to switch from the development branch to the master branch.

$ git checkout feature-y
Switched to branch 'feature-y'

git merge <branch_name> command merges the specified branch into the current branch. This command is used to integrate changes from one branch into another. It's always a good practice to make sure that you are on the branch that you want to merge into before running the merge command.

$ git merge feature-y
Updating e2ef3c4..7f4f8c9
Fast-forward
 file1.txt | 2 ++
 file2.txt | 3 +++
 2 files changed, 5 insertions(+)
 create mode 100644 file1.txt
 create mode 100644 file2.txt

git branch -d <branch_name> command deletes the specified branch. This command is useful for deleting a branch that is no longer needed. It's important to note that you cannot delete a branch that has not been fully merged into another branch.

$ git branch -d feature-y
Deleted branch feature-y (was 7f4f8c9).

In addition to the commands outlined in this article, it's important to also consider permissions when working with Git branches. Depending on the collaboration model of your team, certain team members may only have permission to push to certain branches or may need approval before merging changes into certain branches. For example, in a typical development workflow, developers may only have permission to push to development branches, while release managers are responsible for merging changes into the production branch. It's important to define roles and permissions within your team clearly and to have a system in place for reviewing and approving changes to important branches. Additionally, it's a best practice to have a separate branch for production that is protected, meaning it cannot be deleted or force-pushed. This will ensure that the production branch remains stable and that changes are only made through a proper review and approval process.

Git Branch Commands List
Command Description
git branch Lists all branches in the local repository
git branch 'name' Creates a new branch with the specified name
git checkout Switches to the specified branch
git merge Merges the specified branch into the current branch
git branch -d Deletes the specified branch
git branch -m Renames the specified branch
git branch -a Lists all branches in the local repository and remote repository
git push -u Pushes the specified branch to the remote repository and sets it as the upstream branch
git pull Pulls changes from the specified branch in the remote repository
git stash Stashes any changes in the current branch
git stash list Lists all stashes
git stash apply Applies changes from the specified stash to the current branch
git stash drop Deletes the specified stash

In conclusion, Git branches are a powerful feature that allows teams to work on multiple project versions simultaneously. By understanding and effectively using the Git branch commands outlined in this article, teams can streamline their development workflow, improve collaboration, and make it easier to manage and maintain their codebase.

Errors, Tips? Say it in the comments!

Streamlining EHRs with NLP: Balancing documentation and patient care"

Editor's note The assumption in this article is that the focus is on US doctors, however, the conclusion also highlights the potential for a universal benefit...

Docker Compose vs Docker CLI:

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application services, networks and volumes, making...