DEV Community

Cover image for How to Initialize a Git Repository and Create a GitHub Repository Using the GitHub CLI
FORHAD
FORHAD

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Repository on GitHub

  1. For a public repository, run:
   gh repo create <repository-name>
Enter fullscreen mode Exit fullscreen mode
  1. For a private repository, run:
   gh repo create <repository-name> --private
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

  1. Push your local commits:
   git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Bonus Features

1. Show Remote URL:

To check the remote URL of your repository:

git remote -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

3. Rename a Branch:

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

git branch -M new-branch-name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5. Switch to a Different Branch:

To switch to a different branch:

git checkout branch-name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

7. View Commit History:

To view the commit history:

git log
Enter fullscreen mode Exit fullscreen mode

8. Stash Changes:

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

git stash
Enter fullscreen mode Exit fullscreen mode

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

git stash apply
Enter fullscreen mode Exit fullscreen mode

9. Check Repository Status:

To see the current status of your repository, including changes to be committed and untracked files:

git status
Enter fullscreen mode Exit fullscreen mode

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, stash changes, and keep track of your repository status. These practices will enable smooth version control and collaboration for your project, making it easier to manage and track your work. Happy coding!

Top comments (0)