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:
git init
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:
gh auth login
Step 3: Create a Repository on GitHub
- For a public repository, run:
gh repo create <repository-name>
- 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
- Add your files and make an initial commit:
git add .
git commit -m "Initial commit"
Step 5: Push Your Local Repository to GitHub
- 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.
- Push your local commits:
git push -u origin main
Bonus Features
1. Show Remote URL:
To check the remote URL of your repository:
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. Rename a Branch:
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
9. Check Repository Status:
To see the current status of your repository, including changes to be committed and untracked files:
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!
Top comments (0)