Scenario

If you working in a big team, chances are you have a lot of branches in you code respository. And more likely you will need to swith between branches in order to be able to working on different tasks.

Here comes a scenario you may encounter one day: you are working on one branch, and there’s something else urgent coming in and you have to working on it first. So you commit you changes and switch to another branch. After you finish the urgent task and you want to come back to the unfinished business.But which branch I was working on before?

I know if you have a good memory or you have a system/tool to track all these, it should be very easy to find out the answer. But in some cases, you might not be able to do it. How Git can help you with this?

Solution

The solution is to use sort 1option to list all the branches by commit date.

git branch --sort=-committerdate #descending order

Go Further

Besides the committerdate, the sort options can use all the fields which are available in git for-each-ref 2command.

Footnotes

  1. https://git-scm.com/docs/git-branch

  2. https://git-scm.com/docs/git-for-each-ref