5

Use GitHub cli (gh), how do I get a current repo name?

I can get a listing of the repos I have but how do I get the current repo?

1
  • What do you mean with "Current repo"?
    – Kevin
    Commented Mar 22, 2022 at 13:31

2 Answers 2

11

To get the name of the repo in the curernt working directory, run

gh repo view --json name -q ".name"

This extracts the name of the repo from the command that lists information about the repo in the current directory, and extracts the actual name from the JSON output. For instance, if you were in the working directory for the repo with a URL of http://github.com/rust-lang/rust, the output would be rust.

2
  • 1
    I just stumbled across the same problem. While your solution works, it does an API request that should not be required. However, I didn't find a way to do it without the API request either. Commented Dec 16, 2022 at 15:19
  • 4
    If you'd like the fullname you can also use gh repo view --json nameWithOwner -q ".nameWithOwner" Commented Jan 11, 2023 at 19:19
1

In github actions, there are environment variables that provide this information. In particular:

  • GITHUB_REPOSITORY The owner and repository name. For example, octocat/Hello-World.
  • GITHUB_REPOSITORY_OWNER The repository owner's name. For example, octocat.

From this you can get the name without the owner:

  • ${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/} The repository name. For example, Hello-World

For a full list of variables, see: https://docs.github.com/en/actions/learn-github-actions/variables

Not the answer you're looking for? Browse other questions tagged or ask your own question.