How to commit files modified by pre-commit hook in Git

8,036

You can do something like this:

#!/bin/sh
#

perl -i -pe 's/var app_version = "\d+\.\d+\.\K(\d+)/ $1+1 /e' ./version.js

git add ./version.js

What it does: It changes the string var app_version = "0.1.1"; to var app_version = "0.1.2"; and adds the file back to the git commit process. When you commit the latest change will be included.

Share:
8,036

Related videos on Youtube

dimovnike
Author by

dimovnike

I work with: C/C++, Java, PHP, Mysql, bash. Linux, networking. Microcontrollers (AVR, PIC) Embedded software. Also interested in: Cryptography, math, quantum Computing, photography.

Updated on September 18, 2022

Comments

  • dimovnike
    dimovnike over 1 year

    Im struggling for a couple of hours to make git store metadata (permissions/flags/etc) of the files using metastore and I use hooks/pre-commit for this.

    The script is invoked correctly, and the file "metadata" is modified but not added into commit.

    I tried calling:

    git add ./metadata 
    

    and

    git add -u ./metadata
    

    from the pre-commit script, but there is no effect. The file ends up uncommited and marked as modified or staged BUT not commited, ever.

    Is there any way to make this work? Basically, I want it to update and commit the file "metadata" on each commit (preferably, as the same commit, so I can restore all permissions after checkout)

    Thanks!

    P.S. If you are wondering why I need this, the answer is I store OS files for an embedded device which needs permisions/suid flags etc.

    • Arkadiusz Drabczyk
      Arkadiusz Drabczyk almost 10 years
      It should work, maybe you are doing something wrong. Pre-commit hook is executed automatically when you do "git commit". It must have an executable bit set. So try to add some files to the staging are manually and then do "git commit". After that do "git show" to see what changes have been introduced in this commit and see if metadata has been committed.
  • frhd
    frhd over 9 years
    I got the same problem, but having other staged files changed doesn't solve the issue. Git doesnt pick up changes made by pre-commit in my case..
  • dimovnike
    dimovnike over 9 years
    make sure u do git add for the canges made by pre-commit script (at the end of the script).
  • dimovnike
    dimovnike over 9 years
    wrong question?
  • Martin Gerhardy
    Martin Gerhardy almost 7 years
    The problem here is that you will commit all changes in that particular file. Not only the previously staged changes. Might work for files that only contain the version though.