Sort Git Branches by Date

GitGitBeginner
Practice Now

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

Introduction

When working with Git, it's common to have multiple branches in a repository. However, it can be difficult to keep track of all the branches and their last commit dates. In this lab, you will learn how to use Git to sort branches by date, making it easier to manage your repository.


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-12760{{"`Sort Git Branches by Date`"}} end

Sort Git Branches by Date

You have a Git repository with multiple branches, and you want to sort them by date. This will allow you to see which branches have been updated recently and which ones have not. Sorting branches by date can also help you identify branches that may need attention or merging.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground.

  1. Clone the repository to your local machine:
git clone https://github.com/labex-labs/git-playground
  1. Navigate to the repository directory and configure your GitHub identity:
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Create a branch called one, modify the code and commit it:
git checkout -b one
touch hello.txt
git add .
git commit -m "hello.txt"
  1. Switch to the branch named master and create a branch named two:
git checkout master
git checkout -b two
  1. Now, to sort the branches by date, use the following command:
git branch --sort=-committerdate

This will display a list of all local branches and sort them based on the date of their last commit. You can use the arrow keys to navigate the list, and press Q to exit.

This is the finished result:

Summary

Sorting Git branches by date can be a useful tool for managing your repository. By using the git branch --sort=-committerdate command, you can easily see which branches have been updated recently and which ones may need attention. This lab has provided you with the knowledge and skills to sort Git branches by date, making it easier to manage your repository.

Other Git Tutorials you may like