Chuck Liu
Chuck Liu

Reputation: 117

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. 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.

For example, here is my git log

* 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?

* 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.

But I wonder if there is any method that can force shell script to execute on specific commit hash ID?

Upvotes: 5

Views: 639

Answers (1)

William Pursell
William Pursell

Reputation: 212654

This is one use case for which git worktree is well suited. 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:

$ 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. Read the fine manual for details.

Upvotes: 7

Related Questions