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

标签 git github continuous-integration travis-ci

我正在尝试向我的 devstack 添加一个功能,以便在 travis 测试通过名为 travis 的分支时添加自动部署。这个测试通过之后,我想把这个travis分支 merge 到master分支,然后push到master分支。

到目前为止,当我推送到 travis 分支时,travis 运行测试并且一切都成功但是我在 travis.yml 的 after_success 中的 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.

非常感谢!

最佳答案

长篇小说

它不起作用,因为由于 Travis 克隆存储库的方式, 本地不存在分支机构。您需要先 pull 它们。

在我的 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 如何克隆存储库

我们可以在 Travis 日志中看到克隆存储库时运行了哪些命令。常规分支和 pull 请求略有不同。

对于 pull 请求:

# 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
# https://stackoverflow.com/a/9237511/1836144
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 的说法:

Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at. Further fetches into the resulting repository will only update the remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branch clone was made, no remote-tracking branch is created.

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

如何 pull 所有远程分支

This answer解释如何使 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

关于git - 如何使用带有 git 命令的 travis merge 到另一个分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405047/

相关文章:

git - 忽略提交给 git 的文件并将它们从历史记录中删除

git - 防止在 git 存储库中的 master 分支上直接提交并仅接受 merge ?

github - 关于 Github 问题的 Atom/RSS 提要?

github - 如何为 GitHub 开放软件存储库做出贡献?

c# - MSBUILD 与 VS2008 构建之间的差异

cruisecontrol.net - CCTray授权问题

git - 如何忽略 git status 中的 .gitignore 文件?

在 heroku 之外部署 Git

git - 如何让 Panic Coda 使用 Github 进行源代码控制?

c++ - Jenkins 中的 Visual Studio 代码分析