How to Initialize a Git Repository and Create a GitHub Repository Using the GitHub CLI

Forhad HossainForhad Hossain
3 min read

Setting up version control for your project is essential. This guide will help you initialize a Git repository locally and create a corresponding repository on GitHub using the GitHub Command Line Interface (CLI).

Step 1: Initialize a Git Repository

  1. Open your terminal.

  2. Navigate to your project directory.

  3. Run the following command to initialize a Git repository:

     git init
    

Step 2: Install and Authenticate GitHub CLI

  1. Install the GitHub CLI from GitHub CLI website.

  2. If you are not logged in, authenticate the CLI with your GitHub account by running:

     gh auth login
    

Step 3: Create a Repository on GitHub

  1. For a public repository, run:

     gh repo create <repository-name> --public
    
  2. For a private repository, run:

     gh repo create <repository-name> --private
    

    Replace <repository-name> with your desired repository name.

Step 4: Add Files and Make Initial Commit

  1. Add your files and make an initial commit:

     git add .
     git commit -m "Initial commit"
    

Step 5: Push Your Local Repository to GitHub

  1. Add the GitHub repository as a remote:

     git remote add origin https://github.com/your-username/<repository-name>.git
    

    Replace your-username with your GitHub username and <repository-name> with the name of your repository.

  2. Push your local commits:

     git push -u origin main
    

Bonus Features

1. Show Remote URL:

You can check the remote URL of your repository using:

git remote -v

2. Change Remote URL:

If you need to change the remote URL, use:

git remote set-url origin https://github.com/your-username/<new-repository-name>.git

Replace <new-repository-name> with your new repository name.

3. Change Branch Name:

By default, Git creates a branch named main. To rename the branch:

git branch -M new-branch-name

Replace new-branch-name with your desired branch name.

4. List All Branches:

To see a list of all branches in your repository:

git branch

5. Switch to a Different Branch:

To switch to a different branch:

git checkout branch-name

Replace branch-name with the name of the branch you want to switch to.

6. Create and Switch to a New Branch:

To create a new branch and switch to it immediately:

git checkout -b new-branch-name

7. View Commit History:

To view the commit history:

git log

8. Stash Changes:

If you need to switch branches but have uncommitted changes, you can stash them:

git stash

This command temporarily saves your changes. To apply the stashed changes later, use:

git stash apply

Conclusion

By following these steps and utilizing the bonus features, you can efficiently set up a Git repository locally and on GitHub, manage remote URLs, handle branches, and use stashing for temporary change management, enabling smooth version control and collaboration for your project.

1
Subscribe to my newsletter

Read articles from Forhad Hossain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Forhad Hossain
Forhad Hossain