标签: gitpython

GitPython相当于"git remote show origin"?

我正在尝试更新一个Python脚本,该脚本检查一些本地存储库的状态,以防止从使用进程到使用GitPython的远程控制器.什么是等效的命令GitPythongit remote show origin,或者有什么更好的方法来检查本地回购是快速转发外的日期(等)?

$ git remote show origin
* remote origin
  Fetch URL: <url>
  Push  URL: <url>
  HEAD branch: master
  Remote branches:
    XYZ    tracked
    master tracked
  Local branches configured for 'git pull':
    XYZ    merges with remote XYZ
    master merges with remote master
  Local refs configured for 'git push':
    XYZ    pushes to XYZ    (up to date)
    master pushes to master (up to date)
Run Code Online (Sandbox Code Playgroud)

最后两行是我主要关注的问题.它看起来像这可能是可能的GitPython通过迭代git.Repo.heads …

python git gitpython

7
推荐指数
1
解决办法
3234
查看次数

使用gitpython编辑提交

让我们说你在终端,目前坐在git repo的根源.

如果你已经安装了GitPython,你可以这样做以获得最后一次提交的提交,或者更确切地说,是HEAD指向的提交:

>>> import git
>>> r = git.Repo('.')
>>> c = r.head.commit
>>> c
<git.Commit "62e71e8210d0d0275b1f5845bf3033a7bfa3ed73">
>>> c.message
u'old message'
>>> c.message = "new message"
>>> c.message
'new message'
Run Code Online (Sandbox Code Playgroud)

但是如果退出python并进入git log此处,您将看到提交消息保持不变.如何编辑此库的提交?

python git gitpython

7
推荐指数
1
解决办法
1401
查看次数

GitPython - 如何将'git stash'更改为GitPython存储库?

我有一个通过GitPython库创建的repo,它有一些未提交的更改.我想隐藏这些变化.我该怎么做?

在GitPython 文档中搜索"stash" 没有返回任何结果.

python git gitpython

7
推荐指数
1
解决办法
1904
查看次数

GitPython创建和推送标签

在python脚本中,我尝试在git存储库上创建并将标记推送到origin.我使用gitpython-1.0.2.

我能够检查一个现有的标签,但无法找到如何将新标签推送到远程.

非常感谢

python git gitpython

7
推荐指数
2
解决办法
4211
查看次数

GitPython列出了受某个提交影响的所有文件

我使用这个for循环遍历所有提交:

repo = Repo("C:/Users/shiro/Desktop/lucene-solr/")
for commit in list(repo.iter_commits()):
    print commit.files_list  # how to do that ?
Run Code Online (Sandbox Code Playgroud)

如何获取包含受此特定提交影响的文件的列表?

python gitpython

7
推荐指数
1
解决办法
2278
查看次数

GitPython 更新挂钩 SHA 无法解析

我正在使用 python3.5 和 GitPython 模块编写更新挂钩。

对于初学者来说,我只是想迭代已添加/删除/修改的文件并将其保存到文件中。这是钩子:

import git
import sys

branch, oldref, newref = sys.argv[1:]
print('oldref: {0}'.format(oldref))
print('newref: {0}'.format(newref))

repo = git.Repo('/srv/test')
oldcommit = repo.commit(oldref)
newcommit = repo.commit(newref)

with open(r'/tmp/hook.stdout', 'w') as outfile:
    for diff in oldcommit.diff(newcommit):
        outfile.write('{0}\n'.format(diff.b_path))
Run Code Online (Sandbox Code Playgroud)

当我尝试推送存储库时,出现以下错误:

remote: oldref: a38d324b84327e428127182b2d36b03afa73c11c
remote: newref: 1fbc70042891ecddef104f1938259b30f5f1e912
remote: Traceback (most recent call last):
remote:   File "hooks/update", line 10, in <module>
remote:     oldcommit = repo.commit(oldref)
remote:   File "/usr/local/lib/python3.5/dist-packages/git/repo/base.py", line 433, in commit
remote:     return self.rev_parse(text_type(rev) + "^0")
remote:   File "/usr/local/lib/python3.5/dist-packages/git/repo/fun.py", line 193, in …
Run Code Online (Sandbox Code Playgroud)

python git githooks gitpython

7
推荐指数
1
解决办法
6385
查看次数

查明更改是否已推送到 gitpython 中的原点

在 shell 中,我输入“git status”并获取输出:

Your branch is up-to-date with 'origin/master'.
Run Code Online (Sandbox Code Playgroud)

或者

Your branch is ahead of 'origin/master' by 3 commits.
Run Code Online (Sandbox Code Playgroud)

在 GitPython 中,我怎样才能知道更改是否尚未推送到远程?

gitpython

7
推荐指数
1
解决办法
739
查看次数

如何在 GitPython 中使用 GitHub 访问令牌?

我正在尝试编写一个 python 脚本,该脚本在运行时会将文件推送到我的 GitHub 存储库之一。我正在使用包 GitPython。我想使用访问令牌登录我的 GitHub 帐户(而不是输入我的用户名和密码),因为我有两步验证。我已经创建了令牌,但我不知道如何将它添加到我的 GitPython 代码中。

到目前为止,这是我的代码:

def push(repo_dir):
    import os
    from git import Repo

    # set working directory
    os.chdir("XXX")

    #repo_dir = 'Pantone'
    repo = Repo(repo_dir)
    file_list = [
        "index.html",
        "Data/colors.csv"
    ]
    commit_message = 'Adding new color'
    repo.index.add(file_list)
    repo.index.commit(commit_message)
    origin = repo.remote('origin')
    origin.push()
Run Code Online (Sandbox Code Playgroud)

python github gitpython

7
推荐指数
0
解决办法
2824
查看次数

使用 gitpython,如何检出某个 Git 提交 ID?

检出分支很有效,但我还需要检出给定 Git 存储库中的某个提交 ID。

我的意思是相当于

git clone --no-checkout my-repo-url my-target-path
cd my-target-path
git checkout my-commit-id
Run Code Online (Sandbox Code Playgroud)

我怎样才能用 gitpython 做到这一点?

python-3.x gitpython

7
推荐指数
1
解决办法
2704
查看次数

GitPython:git push - 设置上游

我使用 GitPython 克隆主分支并签出功能分支,我进行本地更新、提交并推送回 git。代码片段如下所示,

注意:我的分支名称是 feature/pythontest

def git_clone():
    repo = Repo.clone_from(<git-repo>, <local-repo>)
    repo.git.checkout("-b", "feature/pythontest")
    # I have done with file updates 
    repo.git.add(update=True)
    repo.index.commit("commit")
    origin = repo.remote(name="origin")
    origin.push()
Run Code Online (Sandbox Code Playgroud)

当我执行脚本时,出现以下错误,

To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/pythontest
Run Code Online (Sandbox Code Playgroud)

python gitpython gitlab

7
推荐指数
1
解决办法
3982
查看次数

标签 统计

gitpython ×10

python ×8

git ×5

githooks ×1

github ×1

gitlab ×1

python-3.x ×1