Skip to content

Create orphan branch using octokit api #1984 #24699

Create orphan branch using octokit api #1984 #24699
Jan 22, 2021 · 4 answers

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;
github.com/octokit/rest.js

Create orphan branch using octokit api

<div class="github-info">
  <div class="date">
    opened <span class="discourse-local-date" data-format="ll" data-date="2021-01-20" data-time="12:23:35" data-timezone="UTC">12:23PM - 20 Jan 21 UTC</span>
  </div>


  <div class="user">
    <a href="https://github.com/techierishi" target="_blank" rel="noopener nofollow ugc">
      <img alt="techierishi" src="https://user-images.githubusercontent.com/7880021/181086312-3869c6c4-9807-44eb-84b5-a5eb8a15a960.jpeg" class="onebox-avatar-inline" width="20" height="20">
      techierishi
    </a>
  </div>
</div>

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

support

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 wi…

Replies

4 suggested answers

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,
});

0 replies
Answer selected

This solution worked. Thanks for you help.

0 replies

@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.

0 replies

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

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants