微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在不丢失主题更改的情况下自动切换到 git branch 创建测试仓库工作树函数用例清理

如何解决在不丢失主题更改的情况下自动切换到 git branch 创建测试仓库工作树函数用例清理

我有一个 cronjob 在休息期间对我的主分支运行测试。首先,它检查主人。假设我在进行一些未提交的更改时正在结帐到 topic。我想确保当我回来时,检查已完成,主题分支已检出,就像我离开时一样。

[programmatically check if you need to stash anything and do so if needed]
git checkout master 
[do your thing]
git checkout - [and nicely unstash changes if there were any. Do not blindly unstash old stashes otherwise.]

解决方法

在我看来:

  • 最好的方法是提交更改,
  • 接下来最好的事情是 issue(Atlassian 的指南)。

如果您还没有准备好在分支 cardano-wallet 上提交更改,请创建一个新分支 (a = done)。它很快,这意味着您永远不会失去这些更改。您可以推送更改并在另一台机器上继续或与其他人分享未完成的工作。

Stash 也可以,但仅限本地。

,

回复里有很好的建议。我喜欢 worktree 解决方案,这是我编写的函数,然后是一个用例,如果它对任何人都有用:

创建测试仓库

mkdir repoparent
cd repoparent
mkdir repo
cd repo
git init
touch README; git add .; git commit -m "first commit"
git checkout -b topic
touch NEWS
git add .
git branch
git status

工作树函数

# usage: testbranch [TESTDIR-BASENAME-IN-PARENT] [BRANCH]
function testbranch(){    
    ## directory where I want the repo to be cloned and linked
    #TEST_DIR="../crontest"
    TEST_DIR=$1
    ## remove TEST_DIR if it exists for any reason
    rm -rf $(.git/worktrees/$TEST_DIR)
    ## create TEST_DIR relative path which is in parent dir
    TEST_DIR_REL=../$1 
    printf "\nTEST DIR: $TEST_DIR_REL\n"
    ## the branch I'd like to checkout in the cloned repo
    #BRANCH='master'
    BRANCH=$2
    printf "\nBRANCH: $BRANCH\n"

    ## clone and link the repo in $TEST_DIR and checkout $BRANCH
    ## adding --force forces even if $BRANCH is checked out in another worktree.
    ## Might require care.
    rm -rf $TEST_DIR_REL
    git worktree add $TEST_DIR_REL $BRANCH --force
    ## cd to the cloned repo
    cd $TEST_DIR_REL
    # [do stuff & save outside the cloned repo which will be deleted],e.g.:
    printf "\nWe are now on worktree and checked into: "
    printf "$(git rev-parse --abbrev-ref HEAD)\n"
    ## cd to the original repo
    cd - ## assumes no cd has occurred in the main code
    ## remove the cloned repo
    rm -rf $TEST_DIR_REL
    ## IMPORTANT: keep git posted that you have removed it
    git worktree prune
    printf "\ntest successfully concluded\n"
}

用例

testbranch foo master

清理

## clean up
cd ...
rm -rf repoparent

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。