你如何将主分支与GitPython合并成一个功能分支? [英] How do you merge the master branch into a feature branch with GitPython?

查看:303
本文介绍了你如何将主分支与GitPython合并成一个功能分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自动化我的一些标准工作流程,而我发现自己经常做的一件事是将远程主分支的更改合并到我自己的本地分支中,并推送结果。



所以步骤如下:


  1. 切换到master

  2. 来自远程的更改
  3. 切换到原始功能分支

  4. 从主合并到功能分支

  5. 推送功能分支到远程

我一直在尝试编写一个简短的python脚本来为我做一个单独的调用,但我被卡住了在第4步。我无法理解文档以了解如何要做到这一点。



使用git.exe本身,我只需要这样做: git.exe merge master



这是否可以使用GitPython模块,如果是这样的话,应该怎么做?

解决方案< /没有一个非常有说服力的理由,我会建议使用 git 二进制文件来执行你的任务。但是,如果您想使用GitPython来执行此操作,请查看 Advanced Repo usage 部分,其中包括一个示例合并操作。



例如,假设我有一个名为 master 功能。我目前在功能分支上,并且想要在从主变更中合并。



初始化回购对象:

 >>> import git 
>>> repo = git.Repo('。')

现在我需要对当前分支的引用;我可以这样做:

 >>> current = repo.active_branch 
>>>当前
< git.Headrefs / heads / feature>

或者我可以通过名称获取分支:

 >>> current = repo.branches ['feature'] 
>>>当前
< git.Headrefs / heads / feature>

我还需要引用 master 分行:

 >>> master = repo.branches ['master'] 
>>> master
< git.Headrefs / heads / master>

现在我需要找到这两个分支的合并基础(即


$ $ p $ $ code>>>> base = repo.merge_base(current,master)
>>> base
[< git.Commit9007141b5daa35c39afda2d6baf670438d7424a7>]



<现在我们开始一个合并操作:

$ $ p $ $ $ $ $ $ $ $>>> repo.index.merge_tree(master,base = base )
< git.index.base.IndexFile对象在0x7fa8bb6a9f70>

并提交它,提供两个父提交的链接:

 >>> repo.index.commit ',
... parent_commits =(current.commit,master.commit))
< git.Commitfb7051d7be36b7998a8f6c480120f84f3f9d9f73>
>>>

此时,我们成功合并了这两个分支,但是
not 修改了工作目录。如果我们返回shell
提示符, git status file显示 file1 已被修改(因为b

  $ git status 
在分支特征$上
不再与存储库中的内容匹配)

b $ b更改没有为commit提交:
(使用git add< file> ...更新将提交的内容)
(使用git checkout - < file> .. 。放弃工作目录中的更改)

修改:file1

我们需要执行新的提交结账:

 >>> current.checkout(force = True)
< git.Headrefs / heads / feature>

现在:

  $ git status 
关于分支功能
没有提交,工作目录干净

上述过程很脆弱;如果存在合并冲突,那么只有
会爆炸,这就是为什么你可能更适合使用CLI的


I'm trying to automate some of my standard workflow and one thing I find myself doing often is to merge changes to a remote master branch into my own local branch and push the result.

So the steps are as follows:

  1. Switch to master
  2. Pull changes from remote
  3. Switch to original feature branch
  4. Merge from master into feature branch
  5. Push feature branch to remote

I've been trying to write a short python script to do this for me with a single call but I'm stuck on step 4. I can't make sense of the docs to work out how to do this either.

Using git.exe itself I would simply do this: git.exe merge master

Is this possible using the GitPython module and if so, how should one do so?

解决方案

Absent a very compelling reason, I would suggest just using the git binary to perform your tasks. However, if you want to do this using GitPython, take a look at the Advanced Repo usage section of the documentation, which includes an example merge operation.

For example, let's say I have a repository with two branches named master and feature. I'm currently on the feature branch, and I want to merge in changes from master.

I start by initializing a Repo object:

>>> import git
>>> repo = git.Repo('.')

Now I need a reference to my current branch; I can do this:

>>> current = repo.active_branch
>>> current
<git.Head "refs/heads/feature">

Or I can get the branch by name:

>>> current = repo.branches['feature']
>>> current
<git.Head "refs/heads/feature">

I also need a reference to the master branch:

>>> master = repo.branches['master']
>>> master
<git.Head "refs/heads/master">

Now I need to find the merge base of these two branches (that is, the point at which they diverge:

>>> base = repo.merge_base(current, master)
>>> base
[<git.Commit "9007141b5daa35c39afda2d6baf670438d7424a7">]

Now we stage a merge operation:

>>> repo.index.merge_tree(master, base=base)
<git.index.base.IndexFile object at 0x7fa8bb6a9f70>

And commit it, providing links to the two parent commits:

>>> repo.index.commit('Merge master into feature',
... parent_commits=(current.commit, master.commit))
<git.Commit "fb7051d7be36b7998a8f6c480120f84f3f9d9f73">
>>> 

At this point, we have successfully merged the two branches but we have not modified the working directory. If we return to the shell prompt, git status file show that file1 has been modified (because it no longer matches what is in the repository):

$ git status
On branch feature
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   file1

We need to perform a checkout of the new commit:

>>> current.checkout(force=True)
<git.Head "refs/heads/feature">

And now:

$ git status
On branch feature
nothing to commit, working directory clean

The above process is fragile; if there are merge conflicts, it's just going to blow up, which is why you are probably better off sticking to the CLI.

这篇关于你如何将主分支与GitPython合并成一个功能分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆