Prerequisites
Before you begin, you’ll need the following:
- Git installed on your local machine. Download Git here.
- A GitHub account. Sign up for GitHub if you don’t have one.
- A local project directory that you want to put under version control.
Step 1: Set ‘main’ as Your Default Git Branch
By default, Git may use ‘master’ as the initial branch name. To use ‘main’ instead, configure your Git settings with the following command:
git config --global init.defaultBranch main
Step 2: Initialize Your Local Repository
Navigate to your project’s directory in the terminal:
cd path/to/your/project
Initialize the repository:
git init
This creates a new .git
directory in your project folder, which Git uses to track changes.
Step 3: Add Your Project Files to the Repository
Add all the files to the staging area:
git add .
Check the status to confirm that the files are staged (you’ll see them listed under “Changes to be committed”):
git status
Step 4: Commit the Files
Commit your files with a message describing the initial commit:
git commit -m "Initial commit"
Step 5: Create a Repository on GitHub
Log into your GitHub account and create a new repository:
- Click the plus (+) icon in the upper right corner and select “New repository”.
- Name your repository and set its visibility (public or private).
- Skip the
- initialization options (like adding a README or .gitignore) if you’re pushing an existing project.
- Click “Create repository”.
- Step 6: Add the GitHub Repository as a Remote
Step 6: Add the GitHub Repository as a Remote
After creating the new repository on GitHub, you’ll be given a URL for it, which looks something like https://github.com/yourusername/your-repository.git
.
Go back to your terminal and add this URL as a remote to your local repository:
git remote add origin https://github.com/yourusername/your-repository.git
Replace https://github.com/yourusername/your-repository.git
with the URL of the repository you just created.
Step 7: Push Your Code to GitHub
Push your main
branch to GitHub:
git push -u origin main
The -u
flag sets the upstream for your local branch, so in the future, you can push and pull without specifying the remote and branch.
Checking Status and Troubleshooting
- Check Status: Always use
git status
to see which files are staged, which are not, and which are not being tracked by Git. - List Configurations: If you need to see your Git configurations, run
git config --list
. - List Remote Connections: To see the remote connections you’ve set up, run
git remote -v
. - Unstage Files: If you accidentally stage a file that you didn’t mean to commit, you can unstage it with
git reset HEAD <file>
. - Amend Commits: If you need to amend your last commit (perhaps to change the commit message), you can do so with
git commit --amend
. - Check Ignored Files: To check which files are being ignored by Git, you can inspect the
.gitignore
file.
Common Errors and Fixes
- Remote Repository Not Found: If you see an error about the remote repository not being found, check that you have the correct URL and that you’ve actually created the repo on GitHub.
- Branch Name Mismatch: If you get errors about the branch name, make sure you’ve set
main
as your default branch globally, as shown in Step 1. - Authentication Failed: If you’re having trouble with authentication, ensure that your GitHub credentials are up to date. If you have two-factor authentication enabled, you may need to use a personal access token instead of a password.
- Push Rejected: If your push is rejected, you might need to pull and merge changes from GitHub first with
git pull origin main
and then push again. - SSL Certificate Issue: On rare occasions, you might encounter SSL certificate errors. You can bypass SSL verification (not recommended) with
git config --global http.sslVerify false
, but it’s better to resolve the certificate issue.
By following these steps, you’ll have your existing local project set up with Git, with ‘main’ as your default branch, and pushed up to GitHub, ready for collaboration, backup, and open-source sharing.