How to Get List of Git Branches, Ordered By Most Recent Commit?

Git is a widely used version control system that helps manage code changes across different branches. Sometimes, you may need to see a list of branches sorted by the most recent commit. This can be useful for understanding the current state of development and prioritizing which branches need attention. This article will guide you through the steps to achieve this using Git commands.

Table of Content

  • Using Git Commands
  • Using Git Aliases
  • Using Git GUI Tools

Using Git Commands

The most straightforward way to list Git branches ordered by the most recent commit is by using a combination of Git commands. Here’s a step-by-step guide as:

1. Using for-each-ref and sort

Git’s for-each-ref command allows you to iterate over and display refs (branches, tags, etc.) in a customized format. Combining this with sorting, you can easily list branches by their latest commit date.

Open your terminal and navigate to your repository. Run the following command:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:iso8601) %(refname:short)'

Explanation:

  • –sort=-committerdate: Sorts the branches by the committer date in descending order.
  • refs/heads/: Limits the output to branches (not tags or other refs).
  • –format=’%(committerdate:iso8601) %(refname:short)’: Formats the output to show the committer date and branch name.

2. Using branch and log

Alternatively, you can use the git branch and git log commands in combination with some shell scripting:

for branch in $(git branch --format='%(refname:short)'); do
echo "$(git log -1 --format="%ci" $branch) $branch"
done | sort -r

Explanation:

  • git branch –format=’%(refname:short)’: Lists all branch names.
  • git log -1 –format=”%ci” $branch: Gets the commit date of the most recent commit in each branch.
  • sort -r: Sorts the output in reverse order, so the most recent commit dates come first.

Using Git Aliases

For convenience, you can create a Git alias to avoid typing long commands repeatedly. Here’s how you can add an alias to your Git configuration:

Open your .gitconfig file and add the following lines under the [alias] section:

[alias]
recent-branches = "!git for-each-ref --sort=-committerdate refs/heads/
--format='%(committerdate:iso8601) %(refname:short)'"

Now, you can simply run git recent-branches to get the list of branches ordered by the most recent commit.

Using Git GUI Tools

Several Git GUI tools can also display branches sorted by recent activity. Here are a couple of popular options:

1. SourceTree

SourceTree, a free Git GUI client by Atlassian, provides a visual representation of your repository, including branch management. Although it does not sort branches by the latest commit out of the box, you can visually inspect branch activity and sort by commit dates manually.

2. GitKraken

GitKraken is another powerful Git GUI that offers more advanced branch management features. It automatically sorts branches by recent activity in its interface, making it easy to see the most active branches at a glance.

Additional Tips

  • Customize the Date Format: The date format in the --format option can be customized. For example, %(committerdate:iso8601) will display the date in ISO 8601 format.
  • Filter Specific Branches: You can filter specific branches by adjusting the refs/heads/ part. For example, refs/heads/feature/ will list only branches under the feature/ prefix.
  • Alias for Convenience: To simplify this command, you can create a Git alias. Add the following line to your ~/.gitconfig file:

Conclusion:

Listing Git branches ordered by the most recent commit is a powerful way to manage and prioritize your work in a repository. By using the git for-each-ref command, you can easily get an overview of the activity across branches. This can help you stay organized and ensure that you’re focusing on the most active parts of your project.


Contact Us