How to Initialize a Git Repository and Create a GitHub Repository Using the GitHub CLI
Published: 2024-07-02 10:03:34

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:

   ```bash
   git init
   ```

#### Step 2: Install and Authenticate GitHub CLI

1. Install the GitHub CLI from [GitHub CLI website](https://cli.github.com/).
2. If you are not logged in, authenticate the CLI with your GitHub account by running:

   ```bash
   gh auth login
   ```

#### Step 3: Create a Repository on GitHub

1. For a public repository, run:

   ```bash
   gh repo create <repository-name>
   ```

2. For a private repository, run:

   ```bash
   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:

   ```bash
   git add .
   git commit -m "Initial commit"
   ```

#### Step 5: Push Your Local Repository to GitHub

1. Add the GitHub repository as a remote:

   ```bash
   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:

   ```bash
   git push -u origin main
   ```

#### Bonus Features

**1. Show Remote URL:**

To check the remote URL of your repository:

```bash
git remote -v
```

**2. Change Remote URL:**

If you need to change the remote URL, use:

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

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:

```bash
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:

```bash
git branch
```

**5. Switch to a Different Branch:**

To switch to a different branch:

```bash
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:

```bash
git checkout -b new-branch-name
```

**7. View Commit History:**

To view the commit history:

```bash
git log
```

**8. Stash Changes:**

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

```bash
git stash
```

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

```bash
git stash apply
```

**9. Check Repository Status:**

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

```bash
git status
```

### 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!