Create orphan branch using octokit api #1984

Is there an API to create an orphan branch using Octokit.
Most probably and alternative of following git operation:

git checkout --orphan orp;
git commit -m 'orp branch initial commit';
git push -u origin orp;
1 Like

Hello, @techierishi. Is this a solution for you?
Inspired by Python script to create an orphan branch · GitHub and the suggestion in the gist

// > Git has a well-known, or at least sort-of-well-known, empty tree whose SHA1 ...
// https://stackoverflow.com/questions/9765453
const SHA1_EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";

const owner = 'xxx'
const repo = 'yyy'

const octokit = new Octokit({ auth: token });
const res = await octokit.request("POST /repos/{owner}/{repo}/git/commits", {
  owner,
  repo,
  message: "orp branch initial commit",
  tree: SHA1_EMPTY_TREE,
  parents: [],
});
await octokit.request("POST /repos/{owner}/{repo}/git/refs", {
  owner,
  repo,
  // If it doesn't start with 'refs' and have at least two slashes, it will be rejected.
  ref: 'refs/heads/orp',
  sha: res.data.sha,
});
3 Likes

This solution worked. Thanks for you help.

1 Like

@techierishi Does this solution continue to work for you. I get a 500 response when I try to create the commit with SHA1_EMPTY_TREE as the tree.

1 Like

I’m facing the same. It seems to work for some repos but not all the time for every repo. I created a ticket for this 500 error while creating a new commit

1 Like