开发者

Uniquely reference a commit in Git when history changes

开发者 https://www.devze.com 2023-01-25 23:47 出处:网络
I ha开发者_运维问答ve a problem, i like to refer to other commits from within commit messages, which i do with the SHA ID, but i also like to change a previous commit from time to time. When i change

I ha开发者_运维问答ve a problem, i like to refer to other commits from within commit messages, which i do with the SHA ID, but i also like to change a previous commit from time to time. When i change a commit, the SHA ID changes, any ideas? is there another way to uniquely refer to a commit? thanks


There's no automatic way to address this. What you probably want to do is finalize commits at some point (i.e. when merged to master and pushed) and at that point confirm that all SHA1s are pointing to the right place.

One way you could check yourself would be to grep commit messages for SHA1s (e.g. git log --grep='[0-9a-f]{7}), and check whether the given SHA1 represents a commit which is an ancestor of the commit whose message mentions it. You could trigger that check from a hook, too. Exactly which hook depends on your workflow; I could see pre-commit or post-merge being possibilities. For single-commit verification (like pre-commit), you wouldn't actually have to grep the logs, just the current commit message. For a merge, you'd want to grep the logs of the commits being merged.


I think the only certain way is to use tags. But it's quite laborious to create a tag whenever you want to reference a commit.

Maybe using relative references (like ~5) could work, but this would break if you deleted, split or squashed commits.

One other possibility would be to reference the SHA1 of the tree, not the commit itself. That way, if you changed commits, but not the trees behind them, the reference would still work. The problem with this is that the tools don't support this, I think.

0

精彩评论

暂无评论...
验证码 换一张
取 消