GitPython

How to do a git reset --hard using gitPython?

早过忘川 提交于 2019-12-05 16:29:00
问题 Well the title is self explanatory. What will be the python code equivalent to running git reset --hard (on terminal) using GitPython module? 回答1: I searched for reset in the documentation and found this: class git.refs.head.HEAD(repo, path='HEAD') reset(commit='HEAD', index=True, working_tree=False, paths=None, **kwargs) Reset our HEAD to the given commit optionally synchronizing the index and working tree. The reference we refer to will be set to commit as well. 回答2: You can use: repo = git

Check merge for conflicts using GitPython

不羁的心 提交于 2019-12-05 02:23:11
问题 I am performing a merge using GitPython: repo.merge_tree(branch1, branch2) After the merge, I would like to see whether or not there were any merge conflicts. How do I do so? 回答1: Nota buena: I wasn't quite able to get this answer to work when I tried it in my own project. I'm not sure if it's because the information I present in this answer is incorrect, or if it's because there was another issue in my code. At any rate, the information in this answer is difficult to find, and I believe it

gitpython and git diff

左心房为你撑大大i 提交于 2019-12-05 00:53:12
I am looking to get only the diff of a file changed from a git repo. Right now, I am using gitpython to actually get the commit objects and the files of git changes, but I want to do a dependency analysis on only the parts of the file changed. Is there any way to get the git diff from git python? Or am I going to have to compare each of the files by reading line by line? You can use GitPython with the git command "diff", just need to use the "tree" object of each commit or the branch for that you want to see the diffs, for example: repo = Repo('/git/repository') t = repo.head.commit.tree repo

Pushing local branch to remote branch - gitpython

和自甴很熟 提交于 2019-12-04 10:23:30
I created new repository in my Github. Using the gitpython library i'm able to get this repository. Then I create new branch, add new file, commit and try to push to the new branch. Please check be code below: import git import random import os repo_name = 'test' branch_name = 'feature4' remote_repo_addr_git = 'git@repo:DevOps/z_sandbox1.git' no = random.randint(0,1000) repo = git.Repo.clone_from(remote_repo_addr_git, repo_name) new_branch = repo.create_head(branch_name) repo.head.set_reference(new_branch) os.chdir(repo_name) open("parasol" + str(no), "w+").write(str(no)) # this is added print

Checkout or list remote branches in GitPython

泪湿孤枕 提交于 2019-12-04 02:59:27
问题 I don't see an option to checkout or list remote/local branches in this module: https://gitpython.readthedocs.io/en/stable/ 回答1: After you’ve done from git import Git g = Git() (and possibly some other command to init g to the repository you care about) all attribute requests on g are more or less transformed into a call of git attr *args . Therefore: g.checkout("mybranch") should do what you want. g.branch() will list the branches. However, note that these are very low level commands and

GitPython get tree and blob object by sha

不羁的心 提交于 2019-12-04 01:15:01
问题 I'm using GitPython with a bare repository and I'm trying to get specific git object by its SHA. If I used git directly, I would just do this git ls-tree sha_of_tree git show sha_of_blob Since I'm using GitPython and I want to get a specific tree, I do the following: repo = Repo("path_to_my_repo") repo.tree("b466a6098a0287ac568ef0ad783ae2c35d86362b") And get this back <git.Tree "b466a6098a0287ac568ef0ad783ae2c35d86362b"> Now I have a tree object, but I cannot access its attributes like path,

Use GitPython to Checkout a new branch and push to remote

坚强是说给别人听的谎言 提交于 2019-12-03 16:34:08
Given a repo(GitPython) - how can i create a new local branch, add some files, and push it to remote using GitPython? repo example: from git import * curr_dir = os.path.dirname(os.path.realpath(__file__)) repo = Repo(curr_dir) for now i'm just using subprocess: def publish_changes_to_git(commit_msg): curr_time = time.time() ts = datetime.datetime.fromtimestamp(curr_time).strftime('%Y-%m-%d-%H-%M-%S') branch_name = "auto-commit-{ts}".format(ts=ts) subprocess.check_output(["git", "checkout", "-b", branch_name]) subprocess.check_output(["git", "add", SOME_PATH]) subprocess.check_output( ["git",

How do you merge the master branch into a feature branch with GitPython?

前提是你 提交于 2019-12-03 16:27:25
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: Switch to master Pull changes from remote Switch to original feature branch Merge from master into feature branch 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

Get changed files using gitpython

瘦欲@ 提交于 2019-12-03 12:31:12
I want to get a list of changed files of the current git-repo. The files, that are normally listed under Changes not staged for commit: when calling git status . So far I have managed to connected to the repository, pulled it and show all untracked files: from git import Repo repo = Repo(pk_repo_path) o = self.repo.remotes.origin o.pull()[0] print(repo.untracked_files) But now I want to show all files, that have changes (not commited). Can anybody push me in the right direction? I looked at the names of the methods of repo and experimented for a while, but I can't find the correct solution.

Python os.getlogin problem

吃可爱长大的小学妹 提交于 2019-12-03 09:57:33
If i create a file like: import os print os.getlogin() and run it with cron, I get an exception print os.getlogin() OSError: [Errno 22] Invalid argument If I run it manually in shell -- it works. Problem is, GitPython 0.3.1 in commit() uses this function, and i need to use it. Is there any workaround? I've tested it on Ubuntu10.10/python2.6.6 and Debian5.0.6/python2.5.2. From the os.getlogin() docs : "Returns the user logged in to the controlling terminal of the process." Your script does not have a controlling terminal when run from cron . The docs go on to suggest: "For most purposes, it is