Fetch Current Branch and Latest Commit_ID in Rails Application

Sometimes while running project on a staging server, we can are required to check the current branch and latest commit for the code. Simply create a new Initializer file git_info.rb

git_info.rb
1
2
GIT_BRANCH = `git rev-parse --abbrev-ref HEAD` || 'none'
GIT_COMMIT = `git log | sed -n 1p`.gsub(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/,'').split.last || '0'   

and now you can use these GIT_BRANCH and GIT_COMMIT anywhere in your views and as per requirement.

git

Comments