🚀 How to Push Data from a Local Git Repository to a Central Repository
Once you’ve made changes to your project and committed them locally using Git, the next big step is to push those changes to a central repository (like GitHub, GitLab, or Bitbucket). This lets you back up your work, collaborate with others, and keep your code in sync across devices.
Let’s walk through the complete process and understand what happens when you use the git push origin main
command.
📤 What Does git push origin main
Mean?
git push origin main
🔍 Explanation:
git push
: Tells Git to upload your local commits to a remote repository.origin
: This is the nickname for the remote repository you previously added.main
: This is the branch name you’re pushing to (commonly used as the default branch).
This command uploads all the changes you’ve made and committed locally on the main
branch to the remote repository linked as origin
.
🔐 Authentication: Username & Password (or Token)
When pushing for the first time (or if you’re not using SSH keys), Git will ask for your credentials:
Username: your-github-username
Password: your-github-password or personal access token
🔐 Important:
GitHub and many other services no longer allow password authentication for Git via HTTPS. Instead, you must use a Personal Access Token (PAT) as your password.
🧠 Example Workflow
Here’s how a full session looks:
git add .
git commit -m "Added new feature"
git push origin main
🖥️ Terminal Prompt:
Username: johndoe
Password: ••••••••••••••••••••••••••••
✅ Once authenticated, Git pushes your changes to the remote main
branch.
🧰 What Happens Behind the Scenes?
When you run git push origin main
:
- Git contacts the remote repository (
origin
) over HTTPS or SSH. - It authenticates your identity using your username/password or token.
- Your local commits are uploaded and applied to the remote
main
branch. - Collaborators can now see and pull your changes.
❓ Frequently Asked Questions (FAQ)
Why do I get a “403: Forbidden” error when pushing?
Answer:
This usually means:
- You’re using the wrong credentials.
- You’re using a GitHub password instead of a Personal Access Token.
- You don’t have permission to push to the repository.
👉 Fix: Create a GitHub PAT and use it as your password.
What is a Personal Access Token (PAT)?
Answer:
A Personal Access Token is a secure substitute for your password when using HTTPS to access GitHub. It allows Git to authenticate you for pushes, pulls, and other operations.
Can I push to a different branch?
Answer:
Yes! Just specify the branch name:
git push origin feature-branch
You can also create a new branch before pushing:
git checkout -b new-feature
git push origin new-feature
How do I push for the first time?
Answer:
Make sure you’ve initialized Git, added a remote, and made a commit:
git init
git remote add origin https://github.com/username/repo.git
git branch -M main
git add .
git commit -m "Initial commit"
git push -u origin main
The -u
flag sets origin/main
as the default for future pushes.
What if I get “rejected – non-fast-forward”?
Answer:
This happens when your local branch is behind the remote. You need to pull first:
git pull origin main --rebase
# Then push
git push origin main
Share Now :