Guide: Initializing a Git Repo and Pushing to GitHub
This guide walks through the standard workflow for taking a local project folder and publishing it to a new repository on GitHub.
1. Prerequisites
- You have configured Git with your identity.
- Reference: Git Configuration Guide
- You have a GitHub account.
2. On GitHub: Create the Remote Repository
- Log in to GitHub.com.
- Click the + icon in the top-right corner and select New repository.
- Give your repository a name (e.g.,
my-new-project). - Crucial Step: Leave the repository empty. Do NOT check "Add a README file," "Add .gitignore," or "Choose a license." This prevents conflicts on your first push.
- Click Create repository.
- On the next page, under "β¦or push an existing repository from the command line," copy the repository URL. It will look like
https://github.com/YourUsername/my-new-project.git.
3. On Your Local Machine: Initialize the Project
Open your terminal and navigate into your project's root folder.
-
Initialize Git:
- This creates a hidden
.gitfolder where all version history is stored.git init
- This creates a hidden
-
Stage Your Files:
- This prepares all files in the current directory for the first commit.
git add .
- This prepares all files in the current directory for the first commit.
-
Create the First Commit:
- This creates a snapshot of your project at this point in time.
git commit -m "Initial commit"
- This creates a snapshot of your project at this point in time.
4. Connect Local to Remote and Push
Now, link your local repository to the one you created on GitHub and send your code.
- Add the Remote:
- Replace the URL with the one you copied from GitHub.
git remote add origin https://github.com/YourUsername/my-new-project.git
- Replace the URL with the one you copied from GitHub.
- Push Your Code:
- The
-uflag sets the upstream tracking branch, so in the future, you can just typegit push.git push -u origin main
- The
After the push completes, refresh your repository page on GitHub. Your files will now be there.