- Learn
- Stack Essentials
- Git & GitHub
- Setting Up Git & GitHub
Install Git on your system and create your first GitHub repository to start saving your code.
Setting Up Git & GitHub
Think of building a project like playing an old school game without auto-save. As you're building, you want to utilize GitHub to save your progress. That way, if something goes wrong you can easily go back to a previous version of your project.
Trust me from experience, there is no worse feeling than when you've been working days on a project to then break something and not be able to fix it.
So my advice is, as you're building develop the mentality of when you make changes to your code and it works, commit it to GitHub.
Prerequisites
Before we begin, make sure you have:
- An existing project folder on your computer
- A GitHub account (expand below if you need to create one)
- Git installed on your system (expand below if you need to install it)
Create Your First Repository
1. Create a New Repository on GitHub
Go to https://github.com and sign in to your account.
2. Start Creating Your Repository
In the upper-right corner of any page, click the + icon, then select New repository.
3. Configure Your Repository
Give your repository a name like "my-project" and you can add a description if you like.
4. Set Repository Visibility
For visibility, you have the option to choose private or public. Choose private to keep your code secure while learning.
5. Create the Repository
Click the Create repository button. Important: Do not initialize with README, .gitignore, or license files since you're adding an existing project.
6. Copy the Repository URL
After creating the repository, scroll down to the "...or push an existing repository from the command line" section and copy the commands shown.
Connect Your Local Project
7. Open Your Terminal
Navigate to your project directory:
cd your-project-folder
8. Initialize Git
If your project isn't already a Git repository, initialize it:
git init
9. Add Your Files
Add all your project files to Git:
git add .
10. Make Your First Commit
Commit your files with a descriptive message:
git commit -m "Initial commit"
11. Connect to GitHub
Add your GitHub repository as the remote origin (replace with your actual URL):
git remote add origin https://github.com/yourusername/your-repo-name.git
12. Set the Default Branch
Ensure you're on the main branch:
git branch -M main
13. Push Your Code
Push your local repository to GitHub:
git push -u origin main
14. Verify Your Setup
Go back to your GitHub repository page and refresh. You should now see all your project files uploaded to GitHub.
Next Steps
Your project is now connected to GitHub! You can:
- Make changes locally and push them with
git push - Create branches for new features
- Collaborate with others by sharing your repository
- Use GitHub's issue tracking and project management features
Your code is now safely backed up and version controlled. Every change you make can be tracked, and you can always revert to previous versions if needed.