3

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.

KGambit
  • 192
  • 3
  • 10

2 Answers2

2

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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

#!/bin/sh
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.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158