In today’s software development workflow, GitHub has become an essential platform for version control and collaboration. Knowing how to push your code to GitHub using the terminal is a fundamental skill for developers. This guide will walk you through the process clearly and efficiently.
Prerequisites
Before you start, ensure you have the following:
- Git installed on your local machine. You can download it from git-scm.com.
- A GitHub account.
- A repository created on GitHub where you want to push your code.
- Basic knowledge of terminal or command-line interface.
Step 1: Set Up Git Locally
If this is your first time using Git, configure your Git username and email in the terminal:
git config --global user.name "Your Name"
git config --global user.email "yo********@ex*****.com"
This information will be linked to your commits.
Step 2: Initialize Git in Your Project Directory
Navigate to your project folder using the terminal:
cd /path/to/your/project
Initialize the Git repository if you haven’t done so:
git init
This command creates a .git
folder that tracks changes in your project.
Step 3: Add Remote Repository
Connect your local repository to the GitHub remote repository. You need the URL of your GitHub repo, which you can find on the repository page (use HTTPS or SSH).
For HTTPS:
git remote add origin https://github.com/username/repository.git
For SSH:
git remote add origin gi*@gi****.com:username/repository.git
Step 4: Stage Your Changes
Add files to the staging area using:
git add .
This command stages all the files in the directory for the next commit. Alternatively, you can add specific files:
git add filename.extension
Step 5: Commit Your Changes
Commit your staged files with a descriptive message:
git commit -m "Initial commit"
A good commit message briefly explains what the changes are about.
Step 6: Push to GitHub
Finally, push your local commits to the GitHub repository:
git push -u origin main
Note: The default branch may be called main
or master
depending on your repository settings. Adjust accordingly.
If this is your first push, Git may prompt you for your GitHub username and password or personal access token.
Bonus Tips
- Check your current branch:
git branch
- Pull latest changes before pushing:
git pull origin main
- Use SSH keys for password-less authentication:
Setting up SSH keys improves security and convenience. Check GitHub’s SSH documentation for detailed instructions.
Conclusion
Pushing code to GitHub from the terminal is straightforward once you understand the workflow. It empowers developers to manage version control efficiently and collaborate seamlessly. Whether you’re just starting or brushing up your skills, mastering Git commands is invaluable for any modern developer.
If you follow these steps, you will confidently push your code to GitHub using just your terminal.