Git add revision number to file on commit

Hey I’m wondering if there is a way in git to make a copy of a specific file and rename it with a revision number on commit (if there has been a change made).

For example if I had a file called ‘code.js’, when I make a change to it and commit, I want to be able to make it ‘code-1.0.0.js’ while keeping the original (or last copy).

I know this isn’t a big deal to do manually, just wondering if there is a better way using git.

  • How to track config files of submodules in Git?
  • Git, SVN, and within-repository links
  • Complex Build/Project Management
  • How to add old zipped sources at the beggining of origin trunk?
  • Linking git repository to multiple TFS projects
  • Moving from Private GitHub Repo to Public and Private
  • Git - How to close commit editor?
  • What are the best tools for Sql Server version control
  • 2 Solutions collect form web for “Git add revision number to file on commit”

    You can automate the generation of that file by a script that you can version and register in a .gitattributes file.

    That would mean that, on git checkout, code-1.0.0.js would automatically be generated (as a private file, not versioned in the git repo), for the local repo owner to use.

    content filter

    The version can be computed from a git describe.

    git describe --abbrev=4 HEAD

    You could use git hooks. Create a file named pre-commit in the folder .git/hooks (below the root of your repository) with this content

    cp -f code.js code-1.0.0.js
    git add code-1.0.0.js

    Then, when you commit, the contents of code.js will be copied to code-1.0.0.js and that file will also be committed.

    This pre-commit script is in bash, so you can improve it to work the way you want. For instance, you could increase the version number in the file name.

    However, the concept of having version numbers embedded in file names does feel like it is going against the grain of Git. If you want file versions for the sake of HTTP caching, you would probably be better off using some functionality for that built into your web development environment or web server.

    Git Baby is a git and github fan, let's start git clone.