You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Thanks for posting! Could you provide a script that helps to reproduce the issue?
Just running the first provided line (repo.active_branch.checkout(orphan="gh-pages")) doesn't do it for me.
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.
@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.
Ok I've finished experimenting and I've found a way of creating orphan branches.
fromosimportgetcwdfromgitimportRepo, Headorphan_branch='lumberjack'r=Repo(getcwd())
# Change the head reference to the new branchr.head.reference=Head(r, 'refs/heads/'+orphan_branch)
# Do whatever you need to, to make the index what you wantindex=r.index# apparently r.index recreates the index everytimeindex.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.
Hello,
I know it is a pretty old discussion, but I was recently looking at creating an orphan branch with gitpython and finally it was as simple as calling directly the git function.
Activity
Midnighter commentedon Mar 21, 2017
But annoyingly although the branch is created I get a ValueError for the reference:
so I cannot delete the git cached files.
Byron commentedon Apr 9, 2017
Thanks for posting! Could you provide a script that helps to reproduce the issue?
Just running the first provided line (
repo.active_branch.checkout(orphan="gh-pages")) doesn't do it for me.Thank you
Midnighter commentedon Apr 10, 2017
The workflow that I would like to copy with gitpython is:
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.
Midnighter commentedon Apr 11, 2017
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.
Midnighter commentedon May 4, 2017
@Byron do you need any more information from me?
Byron commentedon Jun 10, 2017
@Midnighter Thanks a lot! I think now there is enough information to pick up the issue.
jaitaiwan commentedon Jan 5, 2018
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?
jaitaiwan commentedon Jan 16, 2018
@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.
jaitaiwan commentedon Jan 16, 2018
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...
jaitaiwan commentedon Jan 16, 2018
Ok I've finished experimenting and I've found a way of creating orphan branches.
jaitaiwan commentedon Jan 16, 2018
@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.
Midnighter commentedon Jan 16, 2018
Awesome, thank you @jaitaiwan for digging into this.
jaitaiwan commentedon Jan 16, 2018
No worries @Midnighter :)
boubou191911 commentedon May 6, 2021
Hello,
I know it is a pretty old discussion, but I was recently looking at creating an orphan branch with gitpython and finally it was as simple as calling directly the git function.