Skip to content

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

Open
Midnighter opened this issue Mar 21, 2017 · 14 comments
Open

checkout an orphan branch #615

Midnighter opened this issue Mar 21, 2017 · 14 comments

Comments

@Midnighter
Copy link

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:

repo.active_branch.checkout(orphan="gh-pages")  # or: repo.head.ref.checkout
@Midnighter
Copy link
Author

But annoyingly although the branch is created I get a ValueError for the reference:

ValueError: Reference at 'refs/heads/gh-pages' does not exist

so I cannot delete the git cached files.

@Byron
Copy link
Member

Byron commented 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
Copy link
Author

Midnighter commented Apr 10, 2017

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.

@Midnighter
Copy link
Author

Midnighter commented 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
Copy link
Author

@Byron do you need any more information from me?

@Byron
Copy link
Member

Byron commented Jun 10, 2017

@Midnighter Thanks a lot! I think now there is enough information to pick up the issue.

@jaitaiwan
Copy link

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
Copy link

@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
Copy link

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
Copy link

jaitaiwan commented Jan 16, 2018

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

@jaitaiwan
Copy link

@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
Copy link
Author

Awesome, thank you @jaitaiwan for digging into this.

@jaitaiwan
Copy link

No worries @Midnighter :)

@boubou191911
Copy link

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.

from os import getcwd
import git

repo = git.Repo(getcwd())
repo.git.checkout('--orphan','new_branch_name')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants