当前位置:  开发笔记 > 编程语言 > 正文

如何使用带有git命令的travis合并到另一个分支?

如何解决《如何使用带有git命令的travis合并到另一个分支?》经验,为你挑选了1个好方法。

我正在尝试向我的devstack添加一个功能,以便在travis测试通过名为travis的分支时添加自动部署.在此测试通过后,我想将此travis分支合并到主分支并推送到主分支.

到目前为止,当我推送travis分支时,travis运行测试并且一切都成功但我after_success在我的travis.yml文件中遇到了我的git命令问题.

travis.yml

- "npm i -g jasmine-node"
-after_success: 
  - "git fetch"
  - "git checkout master"
  - "git merge travis"
  - "git push origin master"
 branches:
   only:
     - travis

这是travis控制台上的输出:

error: pathspec 'master' did not match any file(s) known to git.
fatal: 'travis' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

非常感谢!



1> little-dude..:
TLDR

它不起作用,因为由于Travis克隆存储库的方式,分支在本地不存在.你需要先拉它们.

在我的travis构建脚本中,我调用此函数允许我拉出所有分支.根据您的需要进行调整.

function create_all_branches()
{
    # Keep track of where Travis put us.
    # We are on a detached head, and we need to be able to go back to it.
    local build_head=$(git rev-parse HEAD)

    # Fetch all the remote branches. Travis clones with `--depth`, which
    # implies `--single-branch`, so we need to overwrite remote.origin.fetch to
    # do that.
    git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
    git fetch
    # optionally, we can also fetch the tags
    git fetch --tags

    # create the tacking branches
    for branch in $(git branch -r|grep -v HEAD) ; do
        git checkout -qf ${branch#origin/}
    done

    # finally, go back to where we were at the beginning
    git checkout ${build_head}
}
说明

特拉维斯如何克隆存储库

我们可以在Travis日志中看到哪些命令在克隆存储库时运行.对于常规分支和拉取请求,它略有不同.

对于拉动请求:

# Clone the repository (note the --depth option) in ./user/repo
git clone --depth=50 https://github.com/user/repo.git user/repo

# Go the repository
cd user/repo

# Fetch the reference to the pull request 
git fetch origin +refs/pull/22/merge:

# Checkout the HEAD of the reference we just fetched. In other words,
# checkout the last commit of the PR. For details about FETCH_HEAD see
# /sf/ask/17360801/
git checkout -qf FETCH_HEAD

对于常规分支(mybranch在此示例中称为):

# Clone the repository (note the --depth option) in ./user/repo
# This time, we also have the --branch option
git clone --depth=50 branch=mybranch https://github.com/user/repo.git user/repo

# Go the repository
cd user/repo

# Checkout  the HEAD of the branch we just fetched
git checkout -qf 7f15290cc343249217a9b3669975705a3dc5bd44

在这两种情况下,--depth都会在克隆存储库时使用该选项,这意味着--single-branch.以下是git关于--single-branch:

仅克隆导致单个分支的提示的历史记录,由--branch选项或主分支远程的HEAD指向.进一步提取到生成的存储库只会更新分支的远程跟踪分支,此选项用于初始克隆.如果在创建单个分支克隆时远程处的HEAD未指向任何分支,则不会创建远程跟踪分支.

换句话说,只获取了一个远程分支.更糟糕的是,git fetch甚至不会取其他分支机构.

如何拉动所有远程分支

这个答案解释了如何重新开始git fetch工作:

git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

现在,git fetch应该获取所有远程分支,但我们仍然没有完成:我们希望创建远程跟踪分支.为此,我们可以git checkout为刚刚获取的每个分支执行操作:

for branch in $(git branch -r|grep -v HEAD) ; do
    git checkout ${branch#origin/}
done

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有