Find Branches Containing a Commit

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Git, branches are used to develop features or isolate changes made to a project. Sometimes, you may need to find out which branches contain a specific commit. This lab will test your ability to use Git commands to find branches containing a commit.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12703{{"`Find Branches Containing a Commit`"}} end

Find Branches Containing a Commit

You have been given a Git repository named https://github.com/labex-labs/git-playground. Your task is to find all branches that contain a hash with the commit message "Added file2.txt".

  1. Change into the repository directory:
cd git-playground
  1. Use the git branch --contains command to find all branches that contain a hash with the commit message "Added file2.txt":
git branch --contains d22f46b

The output should be:

* master
new-branch
new-branch-1
new-branch-2

Summary

In this lab, you learned how to find all the branches containing a specific commit using the git branch --contains command. This is a useful skill to have when working with Git repositories that have multiple branches.

Other Git Tutorials you may like