0

I want to fetch the current git commit id of the repository either before user pushes to GitHub of after user clones from GitHub. This commit id should then be written onto a file, which is also in the repository. Is it possible to do this using any Git hook?

I have a c++ function that displays the current git id. If a user downloads (.zip) of my repository it should display the commit id that was present GitHub when they downloaded it. If they clone the repo, later commit to it, git id should get updated. Any way to do this?

leo valdez
  • 259
  • 1
  • 15
  • I'm not clear on why you're asking for this. It sounds like you're asking for a git hook to write a file in the git repo that records the git commit hashes of that repository. Have you tried `git log`? – Ian MacDonald Jul 06 '18 at 16:06
  • BTW, why are you trying to do this? Since git is a branching based approach, this could have some side effects that you are not anticipating. For instance, will this file containing the commit id be named the same for all users? Do you realize that if you capture the current commit and push to remote that that will not be the commit id of HEAD after your merge to your main branch? – Chad Moore Jul 06 '18 at 16:26
  • `Do you realize that if you capture the current commit and push to remote that that will not be the commit id of HEAD after your merge to your main branch?` Yes, it needs to be same. Id should be updated. So is there any way to achieve this? Have a file that always stores the current git commit id? – leo valdez Jul 06 '18 at 16:30
  • Might as well ask how to dribble a baseball. What's the point, here? What's this in aid of? – jthill Jul 06 '18 at 18:24
  • I'm not able to figure out what more I should add to the question. What exactly is missing? What is the additional info required? – leo valdez Jul 07 '18 at 05:25

1 Answers1

0

You can certainly do this, not sure why, but it is possible.

Note, git uses branches, so you have to know what branch is your main branch, let's assume master.

  1. Use the git log command with some options to get the commit id from the remote HEAD on your main branch.

    $ git log origin/master -n 1 --pretty=format:"%H"

  2. Output this to a file.

    $ git log origin/master -n 1 --pretty=format:"%H" > top_commit.txt

  3. Put this in a shell script so you can reuse.

  4. Add a githook to the desired event, which I assume will be pre-commit. For more on that, see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks.

There are some limits to what you can do. For instance, I know you can do this for pre-commit, but not sure about the need to do so when cloning. Also, client side githooks will have to be installed. You'll need to write a script to install those for your users.

UPDATE

Something akin to what you are asking is possible, but not exactly; I do not think it possible to grab the commit being made and shove it in a file, then include that file in the same commit.

Furthermore, I'm not sure why you want to do this as git log has a full history of what occurred.

There are valid reasons to want to know and use the commit id at HEAD, but we'd need to know why, when, where, how, etc., to help you further.

Chad Moore
  • 734
  • 4
  • 15
  • You'll still want to use the `git log` command with options above, when you do it is up to you based on what you're wanting to do. I'll update based on what I thinking you're trying to do now. – Chad Moore Jul 06 '18 at 16:23
  • commit id is not generated pre-commit. If i use post-commit, change made to the file is not committed, so it is not pushed to GitHub. Is there any other way? – leo valdez Jul 06 '18 at 16:24
  • No. I misunderstood what you were trying to do. I thought you were attempting to get the commit id of HEAD from your origin repository rather than capture the commit id of your current commit. Please address the purpose, or why, behind this request in your question above it will help tremendously to answer this. There may be another way to achieve your purpose, because currently I can't understand why you want to do this. – Chad Moore Jul 06 '18 at 16:28