0

我正在使用 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' 检查 repo 时,我发现:

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)

合并后我是否遗漏了一些可以完成提交并解决此问题的内容?

4

1 回答 1

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 文档通常将此称为远程跟踪分支名称,但我认为这太误导了,因为它包含单词分支,即使它不是分支名称)。

当您签出远程跟踪名称、标签名称或任何不是分支名称的名称时,您实际上是在签出特定的提交,并且会得到一个分离的HEAD.

于 2019-06-07T16:37:25.030 回答