
Git and GitHub are essential tools for modern software development. Git helps track changes in code, while GitHub is a cloud-based hosting service for repositories. In this guide, you'll learn how to use Git to push your code to GitHub step by step.
Prerequisites
Before you start, make sure you have:
If you're using Git for the first time, configure it with your name and email:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
You can verify the settings with:
git config --list
Step 2: Initialize a Git Repository
Navigate to your project folder and initialize Git:
cd /path/to/your/project
git init
This creates a hidden .git folder, turning the directory into a Git repository.
Once Git is initialized, add your project files to the staging area:
git add .
Then, commit the changes:
git commit -m "Initial commit"
This records the changes in your local repository.
To push code, you need to link your local repository to a GitHub repository. Copy the remote repository URL from GitHub, then run:
git remote add origin https://github.com/your-username/your-repository.git
You can verify the remote link with:
git remote -v
Now, push your code to GitHub using:
git push -u origin main
If you're using a different branch (e.g., master instead of main), modify the command accordingly:
git push -u origin master
Go to your GitHub repository, and you should see your files uploaded.
Here are some useful Git commands:
git status
git pull origin main
git checkout -b new-branch
git merge branch-name
Conclusion
You have successfully pushed your code to GitHub! With Git, you can collaborate, track changes, and maintain version control efficiently. Keep practicing and explore more Git commands to enhance your workflow.
Happy coding! 🚀