1

I want through the terminal to create a new codespace on Github, from the current git repository.

I can create codespace from gh with this params

$ gh codespace create
? Repository: [? for help, tab for suggestions]

and then enter repo name with username/repo-name format.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
njfamirm
  • 164
  • 2
  • 12

2 Answers2

1

You can get the repo name with username/repo-name format from the git URL, and then create a codespace from them.

  1. get git repository URL
git config --get remote.origin.url
  1. remove domain and .git from URL to have username/repo-name format
$ git config --get remote.origin.url  | sed 's/https:\/\/github\.com\///' | sed 's/\.git$//'
  1. pipe name to gh command
$ git config --get remote.origin.url  | sed 's/https:\/\/github\.com\///' | sed 's/\.git$//' | xargs gh codespace create -r
njfamirm
  • 164
  • 2
  • 12
1

gh 2.21.0 (Dec. 2022) adds two new elements:

  • Use -R for --repo shorthand and deprecate -r
  • gh codespace create: allow setting display name for the new codespace.

So:

cd /path/to/current/local/repository
gh repo set-default
gh codespace create -R $(gh repo view --json owner,name --jq '"\(.name)/\(.owner.login)"') \
   --display-name yourName

See "Github CLI add another remote and work with it" on the new necessity to define your current repository as the default one.

See gh formatting on --json --jq formatting options, which allows to extract owner/name from the current repository:

gh repo view --json owner,name --jq '"\(.name)/\(.owner.login)"'

# on CMD Windows
gh repo view --json owner,name --jq "\"\(.name)/\(.owner.login)\""
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @njfamirm Also [posted on Mastodon](https://fosstodon.org/@vonc/109585334159967290). – VonC Dec 28 '22 at 07:21