Python 与pygit2合并时从原始/主文件中删除的头

Python 与pygit2合并时从原始/主文件中删除的头,python,git,pygit2,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 = snak

我正在使用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
据我所知,我做的每件事都很正常,所以我不明白为什么头会脱落。根据我的理解,当您签出一个特定的提交时,头部是分离的,而不是我没有执行的分支:

# 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)

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

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,即使它不是分支名称)


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

,感谢您的解释,这是有意义的。我的分支没有一个显示在/refs/heads/中,即使我签出它们时也是如此(方法与以前相同)。如何将所有分支在本地克隆到refs/heads?我使用此处找到的代码成功地将其克隆到/refs/heads:这并没有解决“您仍在合并”的问题,但是现在分离的头已经解决了,我想多看看文档,希望能够解决无法提交的问题。我不确定pygit2在每种情况下都做了什么。一般来说,在Git中,
Git checkout X
(对于任何X)将首先查看
X
是否是一个分支名称(存在为
refs/heads/X
),如果是,则切换到该提交+分支。如果不是,那么它将查看
X
是否是其他现有名称,并且它是否可以解析为一个远程跟踪名称,例如
origin/X
,实际上将运行
git checkout-bx origin/X
,创建指向与
origin/X
相同的提交散列ID的X,然后签出新创建的X。
if name.startswith('refs/heads/'):
    print('{} is a branch name'.format(name))
else
    print('{} is not a branch name'.format(name))