Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change git branch output order

Tags:

git

git-branch

When I type git branch, I receive a list of branches that appear to be sorted alphabetically instead of being sorted by their creation time.

Is there a way to make the output of git branch sorted by date?

like image 871
WinWin Avatar asked Jul 08 '11 20:07

WinWin


People also ask

How do I switch to master branch?

In order to switch to the master branch, on this specific commit, we are going to execute the “git checkout” command and specify the “master” branch as well as the commit SHA. In order to check that you are correctly on a specific commit, you can use the “git log” command again.

How can I get a list of git branches ordered by most recent commit?

Use git branch --sort=-committerdate to display a list of all local branches and sort them based on the date of their last commit. Use arrow keys to navigate, press Q to exit.

How do you change branch in VS code?

Access the "Source Control" tab on the left side of VSCode. Click on the "three small dots" next to the refresh button. Click on the "Checkout to..." option. Choose the branch you want to switch to.

How to switch branches in Git checkout?

How To Switch Branch on Git 1 Switch Branch using git checkout. The easiest way to switch branch on Git is to use the “ git checkout ” command and specify the name of the branch you ... 2 Switch branch using git switch. ... 3 Checkout Remote Branch on Git. ... 4 Checkout New Branch from Specific Commit. ... 5 Conclusion. ...

How do I merge two branches in Git?

When creating a new branch, set up branch.<name>.remote and branch.<name>.merge configuration entries to mark the start-point branch as "upstream" from the new branch. This configuration will tell git to show the relationship between the two branches in git status and git branch -v.

How does git pull work with remote tracking branches?

When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the branch.<name>.remote and branch.<name>.merge configuration entries) so that git pull will appropriately merge from the remote-tracking branch.

How do I change the Order of commits in Git?

Using Git on the console is also fairly easy. Start by executing the command git rebase -i HEAD~<#> where <#> is the number of commits involved in the reorder: An editor will open (probably vim) with text similar to this:


2 Answers

Stujo's answer is my favorite, but I wanted to go one step further and make sort by committer date my default git branch behavior. Here's how:

git config --global branch.sort -committerdate

Remove the - before committerdate to sort the other way.

Now git branch will always be sorted by date!

like image 172
electrovir Avatar answered Sep 28 '22 00:09

electrovir


Edit: Since Git version 2.19 (late 2018), Git obeys a branch.sort configuration. Since Git version 2.7.0 (early 2016), git branch itself allows sorting, so that you do not have to use git for-each-ref directly.

Edit

Alas there are apparent problems with the sorting options taken by git-for-each-ref. Since that command is (obviously) explicitely aimed at showing refs and accepts the --sort option, I'm thinking of this as a likely bug[1].

Here is the best options I can further come up with, but the output is quite far estranged from the original format (because they rely on decorating revisions after the fact to refer to branches). Ah well, maybe it is of use for you:


[1] if this were git-rev-list or git-log I would think the problem would be that we're not actually walking a revision tree; we're actively trying to show only tips of trees, without walking them.

Temporary alternative

git log --no-walk --date-order --oneline --decorate \
       $(git rev-list --branches --no-walk)

This would give you a list similar to

4934e92 (HEAD, origin/testing, origin/HEAD, testing) reviewed INSTALL file as per #1331
6215be7 (origin/maint, maint) reviewed INSTALL file as per #1331
1e5e121 (origin/emmanuel, emmanuel) buffers: adjust the size in zfsfuse_stat
e96783e (origin/compress, compress) buffers: adjust the size in zfsfuse_stat
f6e2c6c (origin/unstable, unstable) revert the fatal condition again
dd52720 (origin/master-lucid, master-lucid) lucid
3b32fa7 (tag: 0.7.0, origin/master, master) panic revocation of 0.7.0-0 package necessitates an update
6eaa64f (origin/maint-0.6.9, maint-0.6.9) Replace remount by drop_caches (on rollback)

_As you can see, the result can be a bit overwhelming in the presence of many remote (tracking) branches, which effectively alias the same revisions. However, the result is properly ordered by (descending) date.

The correct (unfortunately broken?) approach...

No, but you should be able to do

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

(use --sort='-*authordate' for author date ordering)

On my test repo, this yields:

compress
emmanuel
maint
maint-0.6.9
master
master-lucid
testing
unstable

Alias

you can create a git alias to do this: append the following lines to .git/config

[alias]
branch2 = git for-each-ref --sort='-*committerdate' --format="%(refname:short)" refs/heads/

From then on, you could just say git branch2

like image 39
sehe Avatar answered Sep 27 '22 23:09

sehe