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
Open your terminal.
Navigate to your project directory.
Run the following command to initialize a Git repository:
Step 2: Install and Authenticate GitHub CLI
Install the GitHub CLI from GitHub CLI website.
If you are not logged in, authenticate the CLI with your GitHub account by running:
Step 3: Create a Repository on GitHub
For a public repository, run:
For a private repository, run:
Replace <repository-name> with your desired repository name.
Step 4: Add Files and Make Initial Commit
Add your files and make an initial commit:
git commit -m “Initial commit”
Step 5: Push Your Local Repository to GitHub
Add the GitHub repository as a remote:
Replace your-username with your GitHub username and <repository-name> with the name of your repository.
Push your local commits:
Bonus Features
1. Show Remote URL:
To check the remote URL of your repository:
2. Change Remote URL:
If you need to change the remote URL, use:
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:
Replace new-branch-name with your desired branch name.
4. List All Branches:
To see a list of all branches in your repository:
5. Switch to a Different Branch:
To switch to a different branch:
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:
7. View Commit History:
To view the commit history:
8. Stash Changes:
If you need to switch branches but have uncommitted changes, you can stash them:
This command temporarily saves your changes. To apply the stashed changes later, use:
9. Check Repository Status:
To see the current status of your repository, including changes to be committed and untracked files:
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!