-
-
Notifications
You must be signed in to change notification settings - Fork 935
checkout an orphan branch #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
But annoyingly although the branch is created I get a ValueError for the reference:
so I cannot delete the git cached files. |
Thanks for posting! Could you provide a script that helps to reproduce the issue? Thank you |
The workflow that I would like to copy with gitpython is: # set up repository
git init
git add "."
git commit -m "feat: add initial structure"
# setup up deploy branch
deploy_branch="gh-pages"
git checkout --orphan "${deploy_branch}"
# clear files
git rm -r --cached "."
old_ignore=${GLOBIGNORE}
GLOBIGNORE=".git"
rm -rf * .*
GLOBIGNORE=${old_ignore}
mkdir "Results"
touch "Results/.keep"
echo "Soon(tm)" > "index.html"
git add --all "."
git commit -m "feat: add initial \`${deploy_branch}\` structure" When I go through the same steps using gitpython, I run into the problem that the branch is not considered valid. I'll dig out that script, give me a moment. |
Here's a Jupyter notebook showcasing the error I came across. Maybe I'm just too new to using gitpython but it seems like an issue to me. Using GitPython==2.1.3. |
@Byron do you need any more information from me? |
@Midnighter Thanks a lot! I think now there is enough information to pick up the issue. |
I need this functionality for a work script I'm writing. If someone wants to point me in the right direction towards a fix I can get cracking on a pr? |
@Midnighter I did some more research and under the hood it seems that when doing an orphan checkout the branch is not actually created. The branch will only exist after the first git commit in which case the refs/heads/ will point to the newly created commit with no parents. |
This is explained here: https://stackoverflow.com/questions/47078961/create-an-orphan-branch-without-using-the-orphan-flag So unless I'm mistaken this would actually end up being expected functionality... |
Ok I've finished experimenting and I've found a way of creating orphan branches. from os import getcwd
from git import Repo, Head
orphan_branch = 'lumberjack'
r = Repo(getcwd())
# Change the head reference to the new branch
r.head.reference = Head(r, 'refs/heads/'+orphan_branch)
# Do whatever you need to, to make the index what you want
index = r.index # apparently r.index recreates the index everytime
index.remove(['foo.txt'])
index.commit("Test commit message", parent_commits=None) # The orphan branch is created at this step |
@Byron I'm not sure if this needs to be encapsulated in a function... but what I can say is that this functionality isn't always immediately obvious and the documentation on how orphan branches were created in git is hard to find, so I'm not sure if an example in the documentation might be needed. |
Awesome, thank you @jaitaiwan for digging into this. |
No worries @Midnighter :) |
Hello, from os import getcwd
import git
repo = git.Repo(getcwd())
repo.git.checkout('--orphan','new_branch_name') |
Just wanted to leave a note since I couldn't find documentation on it anywhere and it took me a while to figure it out. In order to checkout an orphan branch named, for example,
gh-pages
, run:The text was updated successfully, but these errors were encountered: