Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

git - 如何获得最近提交的Git分支列表?(How can I get a list of Git branches, ordered by most recent commit?)

I want to get a list of all the branches in a Git repository with the "freshest" branches at the top, where the "freshest" branch is the one that's been committed to most recently (and is, therefore, more likely to be one I want to pay attention to).

(我想得到一个Git存储库中所有分支的列表,顶部有“最新鲜”的分支,其中“最新鲜”的分支是最近提交的分支(因此,更可能是一个分支)我想要注意)。)

Is there a way I can use Git to either (a) sort the list of branches by latest commit, or (b) get a list of branches together with each one's last-commit date, in some kind of machine-readable format?

(有没有办法可以使用Git(a)通过最新提交对分支列表进行排序,或者(b)以某种机器可读格式获取分支列表以及每个分支的最后提交日期?)

Worst case, I could always run git branch to get a list of all the branches, parse its output, and then git log -n 1 branchname --format=format:%ci for each one, to get each branch's commit date.

(最糟糕的情况是,我总是可以运行git branch来获取所有分支的列表,解析其输出,然后git log -n 1 branchname --format=format:%ci为每个分支,以获得每个分支的提交日期。)

But this will run on a Windows box, where spinning up a new process is relatively expensive, so launching the Git executable once per branch could get slow if there are a lot of branches.

(但是这将在Windows机器上运行,其中启动新进程相对昂贵,因此如果有很多分支,每个分支启动Git可执行文件可能会变慢。)

Is there a way to do all this with a single command?

(有没有办法用一个命令完成所有这些?)

  ask by Joe White translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the --sort=-committerdate option of git for-each-ref ;

(使用git for-each-ref--sort=-committerdate选项;)

Also available since Git 2.7.0 for git branch :

(自Git 2.7.0开始git branch也可用:)

Basic Usage: (基本用法:)

git for-each-ref --sort=-committerdate refs/heads/

# Or using git branch (since version 2.7.0)
git branch --sort=-committerdate  # DESC
git branch --sort=committerdate  # ASC

Result: (结果:)

结果

Advanced Usage: (高级用法:)

git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'

Result: (结果:)

结果


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...