你如何使用git命令合并到另一个分支中使用travis? [英] How do you merge into another branch using travis with git commands?
问题描述
我试图在我的devstack中添加一项功能,以便在Travis测试通过名为 travis 的分支时添加自动部署。在这个测试通过之后,我想将这个travis分支合并到master分支并推送到master分支。
到目前为止,当我推送到 travis 分支时,travis运行测试并且一切都成功了,但是我在
after_success
在我的travis.yml文件中。 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'与git已知的任何文件都不匹配。
致命:'travis'似乎不是git存储库
致命:无法从远程存储库读取。
请确保您具有正确的访问权限
并存在存储库。
非常感谢!
TLDR
它不工作,因为由于Travis克隆存储库的方式,
分支不在本地存在。你需要先把它们拉出来。
在我的travis构建脚本中,我调用了这个函数,它允许我拉出所有
分支。
$ b
function create_all_branches()
{
#跟踪Travis给我们的位置。
#我们是一个独立的头脑,我们需要能够回到它。
本地build_head = $(git rev-parse HEAD)
#获取所有远程分支。带有`--depth`的特拉维斯克隆,
#隐含`--single-branch`,所以我们需要覆盖remote.origin.fetch到
#。
git config --replace-all remote.origin.fetch + refs / heads / *:refs / remotes / origin / *
git fetch
#可选地,我们也可以获取标签
git fetch --tags
#在$(git branch -r | grep -v HEAD)中创建分支
;做
git checkout -qf $ {branch#origin /}
完成
#最后回到我们开始的地方
git checkout $ {build_head }
}
说明
特拉维斯克隆库如何
我们可以在Travis日志中看到在克隆库时运行哪些命令。这对于常规分支和拉取请求略有不同。
对于合并请求:
#在./user/repo
克隆存储库(注意--depth选项)git clone --depth = 50 https://github.com/ user / repo.git user / repo
#进入存储库
cd user / repo
#获取对请求的引用
git fetch origin + refs / pull / 22 / merge:
#检出我们刚刚提取的引用的HEAD。换句话说,
#检出PR的最后一次提交。有关FETCH_HEAD的详细信息,请参阅
#https://stackoverflow.com/a/9237511/1836144
git checkout -qf FETCH_HEAD
对于常规分支(在本例中称为 mybranch
):
#在./user/repo
中克隆存储库(注意--depth选项)这次我们也有--branch选项
git clone --depth = 50 branch = mybranch https://github.com/user/repo.git user / repo
#进入存储库
cd user / repo
#签出我们刚取得的分支的头
git checkout -qf 7f15290cc343249217a9b3669975705a3dc5bd44
在这两种情况下,克隆存储库时都会使用 - depth
选项,这意味着 - 单分支
。以下是 git
关于 - 单分支
的说法:
仅克隆通向 - 分支选项或主分支远程的HEAD指向的单个分支尖端的历史记录。进一步提取到生成的存储库中将只会更新分支的远程跟踪分支,此选项用于初始克隆。如果在创建单分支克隆时远程的HEAD未指向任何分支,则不会创建远程跟踪分支。
换句话说,只有一个远程分支被提取。更糟糕的是, git fetch
不会获得
甚至获取其他分支。
如何将所有远程分支机构
这个答案解释了如何使 git fetch
再次工作:
$ b
git config - 替换所有remote.origin.fetch + refs / heads / *:refs / remotes / origin / *
现在, git fetch
应该获取所有的远程分支,但我们还没有完成:我们想要创建远程跟踪分支。为此,我们可以为我们刚才提取的每个分支执行 git checkout
:
$ b
;做
git结帐$ {branch#origin /}
完成
I am trying to add a feature to my devstack to add auto deploy when a travis test passes on a branch called travis. After this test passes, I want to merge this travis branch into master branch and push to the master branch.
So far, when I push to travis branch, travis runs the test and everything succeeds but I am having problems with my git commands in after_success
in my travis.yml file.
travis.yml
- "npm i -g jasmine-node"
-after_success:
- "git fetch"
- "git checkout master"
- "git merge travis"
- "git push origin master"
branches:
only:
- travis
This is the output on travis console:
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.
Thank you so much!
TLDR
It is not working because due to the way Travis clones repositories, the branches don't exist locally. You need to pull them first.
In my travis build script, I call this function that allows me to pull all the branches. Adapt it according to your need.
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}
}
Explanation
How Travis clones repositories
We can see in Travis logs which commands are run when it clones the repositories. It is slightly different for regular branches and for pull requests.
For a Pull Request:
# 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
For a regular branch (called mybranch
in this example):
# 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
In both case the --depth
option is used when the repository is cloned, which implies --single-branch
. Here is what git
says about --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.
In other words, only one remote branch was fetched. Worse, git fetch
won't
even fetch the other branches.
How to pull all the remote branches
This answer explains how to make git fetch
work again:
git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
Now, git fetch
should fetch all the remote branches, but we are still not done: we want remote-tracking branches to be created. For this, we can do git checkout
for each branch we just fetched:
for branch in $(git branch -r|grep -v HEAD) ; do
git checkout ${branch#origin/}
done
这篇关于你如何使用git命令合并到另一个分支中使用travis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!