简体   繁体   English

如何在特定的 git 提交上运行 shell 脚本?

[英]How to run shell script on specific git commit?

I want to run run.sh to check the results of my codes, which takes about 30 hours.我想运行run.sh来检查我的代码的结果,这大约需要 30 个小时。 But I can't wait to add other features to my codes.但我迫不及待地想在我的代码中添加其他功能。 However, I found there are some potential dangers:但是,我发现有一些潜在的危险:

In my case, I want run.sh running with all files are fixed while I keep editing more features on python files, which may have bugs.在我的例子中,我希望run.sh运行时所有文件都是固定的,同时我继续编辑 python 文件的更多功能,这些文件可能有错误。

For example, here is my git log例如,这是我的 git 日志

* commit 6d1931c0f5f6f7030f1a93d4f7496527c3711c1f (HEAD -> master)
| Author: deeperlearner
| Date:   Fri Aug 13 10:37:21 2021 +0800
|
|     Version to be run by shell script
|

How can I force run.sh run on commit 6d1931 while I keep doing commits?我如何在继续提交的同时强制run.sh在提交6d1931上运行?

* commit 059706b752f4702bc5d0c830c87e812a7bbaae27 (HEAD -> master)
| Author: deeperlearner
| Date:   Fri Aug 13 10:38:21 2021 +0800
|
|     Buggy commit that may destroy `run.sh`
|
* commit 6d1931c0f5f6f7030f1a93d4f7496527c3711c1f
| Author: deeperlearner
| Date:   Fri Aug 13 10:37:21 2021 +0800
|
|     Version to be run by shell script
|

I currently have solutions: use docker or just copy the whole codes to other directory.我目前有解决方案:使用docker或将整个代码复制到其他目录。

But I wonder if there is any method that can force shell script to execute on specific commit hash ID?但我想知道是否有任何方法可以强制 shell 脚本在特定提交 hash ID 上执行?

This is one use case for which git worktree is well suited.这是git worktree非常适合的一个用例。 You basically create a new branch at the desired commit and make a copy of the working directory at that branch.您基本上是在所需的提交处创建一个新分支,并在该分支处复制工作目录。 For example, if you're at the top-level of your working tree and want to run the script on the current HEAD, just do:例如,如果您在工作树的顶层并希望在当前 HEAD 上运行脚本,只需执行以下操作:

$ git worktree add ../bar
Preparing worktree (new branch 'bar')
HEAD is now at b2ceba9 Add d
$ cd ../bar
$ # Run long running script here
$ cd ../foo
$ # Edit files, create new commits, profit

(In this example, foo is the name of the top level directory of the repo.) There are options to worktree to work with a particular commit rather than HEAD. (在这个例子中, foo是 repo 的顶级目录的名称。)worktree 有一些选项可以处理特定的提交而不是 HEAD。 Read the fine manual for details.详细阅读精美手册。

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

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