与pygit2合并时,HEAD从原点/主节点发送

时间:2019-06-07 16:27:18

标签: python git pygit2

我正在使用pygit2合并项目的某些分支,但是每当合并它们时,我最终都会得到:

def avoid_walls(directions, board, snake):
<<<<<<< HEAD
return directions
=======
z = 1
moves = []
for direction in directions:
    new_x = snake[0][0] + dirs[direction][0]
    new_y = snake[0][1] + dirs[direction][1]
    if new_x >= 0 and new_x < len(board[0]) and new_y >= 0 and new_y < len(board):
        moves.append(direction)
return moves
>>>>>>> 357f58b6d1682760b2fa9bf7b2418da347ca353c

在我的代码中。当我用“ git status”检查仓库时,会发现:

HEAD detached from origin/master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

据我所知,我一切都做得很好,所以我无法弄清为什么HEAD脱落了。据我了解,当您检出特定的提交而不是分支时,HEAD是分离的,而我没有这样做:

# clone repo to local directory
repo = pygit2.clone_repository(my_repo, my_dir)

# checkout master branch (checked out by default, but just to be safe)
repo.checkout('refs/remotes/origin/master')

# merge with other branches
repo.merge(repo.branches['origin/branch1'].target)

# commit changes
index = repo.index
index.add_all()
index.write()
author = pygit2.Signature("ME", "me@domain.com")
commiter = pygit2.Signature("ME", "me@domain.com")
tree = index.write_tree()
oid = repo.create_commit('refs/heads/master', author, commiter, "init commit", tree, [repo.head.target, repo.branches['origin/branch1'].target])

#some tests i tried to fix the issue
repo.head.set_target(oid)
repo.apply(oid)

合并后我是否缺少某些内容,可以完成提交并解决此问题?

1 个答案:

答案 0 :(得分:1)

refs/remotes/origin/master不是分支。所有分支均以refs/heads/开头:

if name.startswith('refs/heads/'):
    print('{} is a branch name'.format(name))
else
    print('{} is not a branch name'.format(name))

在这种情况下,因为它以refs/remotes/开头,所以它是一个远程跟踪名称(Git文档通常将其称为远程跟踪分支名称,但是我认为这太让人误解了,因为其中包含 branch 这个词,即使它不是 branch 名称。

当您签出远程跟踪名称,标签名称或任何不是分支名称的名称时,您实际上是在签出特定的提交,并且将获得分离的{{ 1}}。