简体   繁体   English

Git在提交时向文件添加修订号

[英]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). 嘿,我想知道是否有一种方法可以在git中制作特定文件的副本,并在提交时使用修订号重命名(如果已经进行了更改)。

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). 例如,如果我有一个名为'code.js'的文件,当我对其进行更改并提交时,我希望能够在保留原始(或最后一个副本)的同时使其成为'code-1.0.0.js' 。

I know this isn't a big deal to do manually, just wondering if there is a better way using git. 我知道手动做这不是什么大不了的事,只是想知道是否有更好的方法使用git。

You can automate the generation of that file by a script that you can version and register in a .gitattributes file. 您可以通过可以在.gitattributes文件中进行版本和注册的脚本自动生成该文件。

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. 这意味着,在git checkoutcode-1.0.0.js将自动生成(作为私有文件,而不是在git repo中版本化),供本地repo所有者使用。

内容过滤器

The version can be computed from a git describe . 该版本可以git describe计算

git describe --abbrev=4 HEAD

You could use git hooks . 你可以使用git hooks Create a file named pre-commit in the folder .git/hooks (below the root of your repository) with this content 使用此内容在文件夹.git/hooks (在存储库的根目录下)创建名为pre-commit的文件

#!/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. 然后,当您提交时, code.js的内容将被复制到code-1.0.0.js并且该文件也将被提交。

This pre-commit script is in bash, so you can improve it to work the way you want. 这个pre-commit脚本是bash,所以你可以改进它以你想要的方式工作。 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. 然而,在文件名中嵌入版本号的概念确实感觉它违背了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. 如果您希望为HTTP缓存提供文件版本,那么最好使用内置于Web开发环境或Web服务器中的某些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM