2020年8月28日 星期五

[ Python 常見問題 ] How to checkout a tag with GitPython

 Source From Here

Question
In a python script, I try to checkout a tag after cloning a git repository. I use GitPython 0.3.2.
  1. #!/usr/bin/env python  
  2. import git  
  3. g = git.Git()  
  4. g.clone("user@host:repos")  
  5. g = git.Git(repos)  
  6. g.execute(["git""checkout""tag_name"])  
With this code I have an error:
g.execute(["git", "checkout", "tag_name"])
File "/usr/lib/python2.6/site-packages/git/cmd.py", line 377, in execute
raise GitCommandError(command, status, stderr_value)
GitCommandError: 'git checkout tag_name' returned exit status 1: error: pathspec 'tag_name' did not match any file(s) known to git.

If I replace the tag name with a branch name, I have no problem. I didn't find informations in GitPython documentation. And if I try to checkout the same tag in a shell, I have non problem. Do you know how can I checkout a git tag in python ?

HowTo
Assuming you cloned the repository in 'path/to/repo', just try this:
  1. from git import Git  
  2.   
  3. g = Git('path/to/repo')  
  4. g.checkout('tag_name')  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...