After initializing a Git repository, you can configure it and push your code to a remote repository on GitHub. Here’s how:
- Create a Remote Repository on GitHub:
- Go to the GitHub website (https://github.com) and log in to your account.
- Click the “+” sign in the top right corner and select “New repository.”
- Fill in the repository name, description, and other settings as needed.
- Click “Create repository.”
- Connect Local Repository to Remote Repository:
- On the GitHub repository page, you’ll see the repository URL. It will look something like:
- Add a Remote to Your Local Repository:
- In your terminal, navigate to your local repository.
- Run the following command, replacing the URL with the URL of your GitHub repository:
git remote add origin https://github.com/username/repository-name.git
- Push Your Code to GitHub:
- Start by committing your changes locally using
- Then, use the
git pushcommand to push your commits to the remote repository on GitHub:
git push -u origin mainHere,
mainis the branch you’re pushing. If your repository uses a different default branch name (such as
mainwith that branch name.
- Enter Your GitHub Credentials:
- If this is your first time pushing to the remote repository, Git will prompt you to enter your GitHub username and password (or personal access token).
- Authentication Methods:
- It’s recommended to use a personal access token (PAT) for authentication instead of your password. You can create a PAT in your GitHub account settings and use it as your password when pushing. This is more secure and allows you to use two-factor authentication.
And that’s it! Your local repository is now connected to your remote repository on GitHub, and you’ve pushed your code to the remote repository. Subsequent pushes can be done using
git push without the
-u flag, as the tracking relationship has been established.