简体   繁体   中英

Ceate github codespace from current git directory repository?

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.

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

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)\""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM