programing tip

Git 커밋 수는 어떻게 얻습니까?

itbloger 2020. 9. 30. 08:51
반응형

Git 커밋 수는 어떻게 얻습니까?


SVN 개정 번호와 같은 Git 저장소의 커밋 수를 얻고 싶습니다.

목표는 고유하고 증가하는 빌드 번호로 사용하는 것입니다.

저는 현재 Unix / Cygwin / msysGit에서 그렇게합니다.

git log --pretty=format:'' | wc -l

그러나 나는 그것이 약간의 해킹이라고 생각합니다.

더 좋은 방법이 있습니까? 실제로 필요하지 wc않거나 심지어 Git이 필요하지 않으면 멋질 것이므로 베어 Windows에서 작동 할 수 있습니다. 파일이나 디렉토리 구조를 읽으십시오 ...


개정에 대한 커밋 수를 얻으려면 ( HEAD,, master커밋 해시) :

git rev-list --count <revision>

모든 분기에서 커밋 수를 얻으려면 :

git rev-list --all --count

빌드 식별자에 이것을 사용하지 않는 것이 좋지만, 필요한 경우 빌드중인 브랜치의 개수를 사용하는 것이 가장 좋습니다. 이렇게하면 동일한 개정판이 항상 동일한 번호를 갖게됩니다. 모든 지점에 대해 개수를 사용하면 다른 지점의 활동으로 인해 숫자가 변경 될 수 있습니다.


git shortlog 한 가지 방법입니다.


git rev-list HEAD --count

git rev-list

git rev-list <commit>: 주어진 커밋의 상위 링크를 따라 도달 할 수있는 커밋을 나열합니다 (이 경우 HEAD ).

--count : 나열되었을 수있는 커밋 수를 나타내는 숫자를 인쇄하고 다른 모든 출력을 억제합니다.


이 명령은 커미터별로 그룹화 된 커밋 수를 반환합니다.

git shortlog -s

산출:

14 John lennon
9  Janis Joplin

-s인수가의 축소 형식 임을 알고 싶을 수 있습니다 --summary.


커밋에 대한 고유하고 여전히 읽기 쉬운 식별자를 찾고 있다면 git describe 가 적합 할 수 있습니다.


Git에서 "개정 번호"에 대해 생각하는 첫 번째 사람 wc은 아니지만 커밋을 지우거나 짓누르고 기록을 다시 확인할 수 있기 때문에 ' '는 매우 위험합니다.

"개정 번호"는 병합의 경우 필요 했기 때문에 Subversion에 특히 중요했습니다 (SVN1.5 및 1.6이 전면에서 개선되었습니다).

올바른 번호를 결정하기 위해 분기 모든 내역을 조회 하지 않는 알고리즘으로 주석에 개정 번호를 포함하는 사전 커밋 후크로 끝날 수 있습니다.

Bazaar는 실제로 그러한 알고리즘을 내 놓았고 , 여러분이하고 싶은 일에 대한 좋은 출발점이 될 수 있습니다.

( Bombe의 답변이 지적했듯이 Git에는 실제로 최신 태그, 커밋 수 및 약간의 SHA-1 키를 기반으로 한 자체 알고리즘이 있습니다). 그것이 당신에게 효과가 있다면 그의 대답을보고 찬성해야합니다.


Aaron의 아이디어 를 설명 하기 위해 애플리케이션과 함께 배포 하는 애플리케이션의 "info"파일에 Git 커밋 해시를 추가 할 수도 있습니다 .

이렇게하면 about 상자가 다음과 같이 표시됩니다.

상자에 대해

The applicative number is part of the commit, but the 'application’s "info" file' is generated during the packaging process, effectively linking an applicative build number to a technical revision id.


U can just use :

git shortlog -s -n

Result :

 827  user one
    15  user two
     2  Gest 

To get it into a variable, the easiest way is:

export GIT_REV_COUNT=`git rev-list --all --count`

A simple way is:

 git log --oneline | wc -l

oneline ensures that.


Git shortlog is one way to get the commit details:

git shortlog -s -n

This will give the number of commits followed by the author name. The -s option removes all the commit messages for each commit that the author made. Remove the same option if you would like to see the commit messages also. The -n option is used for sorting the entire list. Hope this helps.


git rev-parse --short HEAD


There's a nice helper script that the Git folks use to help generate a useful version number based on Git describe. I show the script and explain it in my answer to How would you include the current commit id in a Git project's files?.


If you're just using one branch, such as master, I think this would work great:

git rev-list --full-history --all | wc -l

This will only output a number. You can alias it to something like

git revno

to make things really convenient. To do so, edit your .git/config file and add this in:

[alias]
    revno = "!git rev-list --full-history --all | wc -l"

This will not work on Windows. I do not know the equivalent of "wc" for that OS, but writing a Python script to do the counting for you would be a multi-platform solution.


Generate a number during the build and write it to a file. Whenever you make a release, commit that file with the comment "Build 147" (or whatever the build number currently is). Don't commit the file during normal development. This way, you can easily map between build numbers and versions in Git.


In our company, we moved from SVN to Git. Lack of revision numbers was a big problem!

Do git svn clone, and then tag the last SVN commit by its SVN revision number:

export hr=`git svn find-rev HEAD`
git tag "$hr" -f HEAD

Then you can get the revision number with help of

git describe --tags --long

This command gives something like:

7603-3-g7f4610d

Means: The last tag is 7603 - it's the SVN revision. 3 - is count of commits from it. We need to add them.

So, the revision number can be counted by this script:

expr $(git describe --tags --long | cut -d '-' -f 1) + $(git describe --tags --long | cut -d '-' -f 2)

The one I used to use was:

git log | grep "^commit" | wc -l

Simple but it worked.


Using Bash syntax,

$(git rev-list --count HEAD)

looks fine for purely linear history. If you also want to sometimes have “numbers” from branches (based off master), consider:

$(git rev-list --count $(git merge-base master HEAD)).$(git rev-list --count ^master HEAD)

When run from a checkout of master, you get simply 1234.0 or the like. When run from a checkout of a branch you will get something like 1234.13, if there have been 13 commits made on that branch. Obviously this is useful only insofar as you are basing at most one branch off a given master revision.

--first-parent could be added to the micro number to suppress some commits arising only from merging other branches, though it is probably unnecessary.


You can try

git log --oneline | wc -l

or to list all the commits done by the people contributing in the repository

git shortlog -s

git config --global alias.count 'rev-list --all --count'

If you add this to your config, you can just reference the command;

git count


Use git shortlog just like this

git shortlog -sn

Or create an alias (for ZSH based terminal)

# show contributors by commits alias gcall="git shortlog -sn"

참고 URL : https://stackoverflow.com/questions/677436/how-do-i-get-the-git-commit-count

반응형