Git
How to Push Code to GitHub from the Terminal?
GitHub is a widely used platform for hosting and collaborating on code. While graphical interfaces like GitHub Desktop or integrated development environments (IDEs) simplify pushing code to GitHub, many developers prefer using the terminal for direct control over their repositories.
In this blog, you will learn how to push code to GitHub from the terminal, from setting up your repository to making your first commit and push.
Prerequisites
Before starting, ensure the following:
- Git Installed: Download and install Git from git-scm.com.
- GitHub Account: Sign up for an account at github.com if you don’t already have one.
- SSH Key (Optional but Recommended): Set up an SSH key for secure authentication. You can refer to GitHub’s SSH documentation.
Step 1: Set Up a Local Repository
1. Initialize Git in Your Project Folder
Navigate to your project directory in the terminal:
cd /path/to/your/project
Initialize a Git repository:
git init
This command creates a .git
folder in your project directory, which Git uses to track changes.
Step 2: Create a GitHub Repository
- Log in to GitHub: Go to GitHub and log in to your account.
- Create a New Repository:
- Click the New button (+ icon) or go to Create Repository.
- Fill in the repository name, description (optional), and visibility settings (public or private).
- Leave “Initialize this repository with a README” unchecked, as you’re pushing an existing project.
- Copy the Repository URL: After creating the repository, GitHub will display a URL (HTTPS or SSH). Copy it for later use.
Step 3: Add a Remote Repository
In the terminal, link your local repository to the GitHub repository:
git remote add origin <repository-url>
Replace <repository-url>
with the URL of your GitHub repository. For example:
- HTTPS:
git remote add origin https://github.com/username/repository.git
- SSH (if configured):
git remote add origin [email protected]:username/repository.git
Step 4: Stage and Commit Your Code
- Check the Status: To see which files are tracked by Git and which are not:
git status
- Stage the Files: Add files to the staging area:
git add .
The.
stages all changes in the current directory. Alternatively, specify individual files:git add filename.py
- Commit the Changes: Create a commit with a descriptive message:
git commit -m "Initial commit"
Step 5: Push to GitHub
To push your changes to the GitHub repository:
git push -u origin main
Explanation:
-u
: Setsorigin
as the default remote andmain
as the default branch for future pushes.origin
: Refers to the remote repository you added earlier.main
: The branch to which you’re pushing the code. Replace withmaster
or another branch name if applicable.
Step 6: Verify the Push
- Visit Your Repository: Go to your GitHub repository URL in a browser.
- Check the Files: Your files should now appear in the repository.
Additional Commands and Tips
1. Switching Branches
If you’re working on a branch other than main
, switch to it using:
git checkout branch-name
2. Updating the Remote URL
If you need to update the remote URL:
git remote set-url origin <new-repository-url>
3. Pushing Changes After the First Push
For subsequent pushes, you can use:
git push
4. Pull Before Pushing
If your remote repository has updates, pull them first to avoid conflicts:
git pull origin main
Common Issues and Troubleshooting
- Authentication Errors:
- If you encounter errors, ensure your credentials are correct.
- Use SSH for secure and hassle-free authentication.
- Rejected Push:
- If your push is rejected, pull the latest changes from the remote repository and resolve any conflicts:
git pull origin main
- If your push is rejected, pull the latest changes from the remote repository and resolve any conflicts:
- Branch Naming Issues:
- If your local branch is named
master
but the remote branch ismain
, rename your branch:git branch -M main
- If your local branch is named
- Permission Errors:
- Ensure you have the necessary permissions for the remote repository, especially for private repositories.
Conclusion
Pushing code to GitHub from the terminal is a fundamental skill for developers. This guide covered everything from initializing a local repository to successfully pushing code to GitHub. By mastering these steps, you can streamline your workflow and collaborate effectively on projects.
For advanced use cases, consider exploring Git commands like branching, merging, and rebasing. With practice, the terminal will become your go-to tool for version control and repository management.