Skip to content

Instantly share code, notes, and snippets.

@sg-s
Last active March 3, 2024 10:07
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sg-s/2ddd0fe91f6037ffb1bce28be0e74d4e to your computer and use it in GitHub Desktop.
Save sg-s/2ddd0fe91f6037ffb1bce28be0e74d4e to your computer and use it in GitHub Desktop.
Automatic version numbers of your git repository using git hooks

Put this in a file called pre-commit in .git/hooks/

#!/bin/sh
# To enable this hook, rename this file to "pre-commit".

git rev-list --count master > build_number
git add build_number

Now there is a file called build_number in the root of your directory that counts the number of commits on your master branch (which is your build number).

Make sure the permissions on the file you created are the same as the samples in that directory.

@sajjad734
Copy link

Hi, I am new to github. I have been asked to maintain version number of my works. could you please help me to setup the version. I went through the above code but understanding it.
Regards,
S.

@ogrotten
Copy link

FYI for whoever:

This could also be done with the first line as

git rev-list --count master > build_number

Also note that ./git is probably hidden, but it is definitely there in the root of your project so cd into it anyway.

@sg-s
Copy link
Author

sg-s commented Aug 11, 2023

@ogrotten awesome thank you!

@ogrotten
Copy link

You didn't have to change it. 😄 Your way was less magic. I was just offering an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment