-2

EveryOne. I wanna create Github repository using access token via CLI I wanna work on Command Prompt which has below functions.

  1. Clone repository from github
  2. Create repository on My github account
  3. Add that repository to my Github

All the works must be done via CLI Is there anyone knows how to do this ?

I've tried at CLI and didn't work well.

1
  • The task looks like a fork. Do you want to fork the original repo? Try gh repo fork. Commented Apr 25, 2024 at 8:39

1 Answer 1

1

It's pretty simple to do:

  1. Clone repository from github
git clone <url> <folder>
  1. Create repository on My github account.
    See the script below

  2. Add that repository to my Github.
    See the script below.

    cd <repo folder>
    
    # Set the repository to point to the "new" repository
    git remote set-url origin <your new repo url>
    
    # "Upload" changes to the new repo
    git push origin <branch>
    

Working Demo (Script)

#!/bin/bash

# Your GitHub token
TOKEN="ghp_xxx"

# The name of the repository to create
REPO_NAME="StackOverflowDemo"

# The GitHub API URL for creating a new repository
API_URL_CREATE="https://api.github.com/user/repos"

###
### Create the repository
###
# The JSON data to send in the POST request
DATA="{\"name\":\"$REPO_NAME\"}"

# Use curl to send a POST request to the GitHub API to create a new repository
curl -H "Authorization: token $TOKEN" -d "$DATA" $API_URL_CREATE

###
### Verify that the repository exists
###
USER_NAME=xxx

# The API for getting the repository information
API_URL_GET="https://api.github.com/repos"

# Send a GET request to the GitHub API to "get" the repository details  
RESPONSE=$(curl -s -H "Authorization: token $TOKEN" $API_URL_GET/$USER_NAME/$REPO_NAME)

# Check if the repository exists
if [[ $RESPONSE == *"Not Found"* ]]; then
  echo "Repository does not exist"
else
  echo "Repository exists"
fi

# Clone the original repo
git clone [email protected]:xxx/xxx.git FOLDER_NAME

# Chage the repository remote to your new GitHub repo
cd FOLDER_NAME
git remote set-url origin <your new repo url>

git push origin <branch>
###
### Thats it :-)
###


Execution Demo

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.