| id | push-changes-to-github | |||||||
|---|---|---|---|---|---|---|---|---|
| title | Push Changes to GitHub | |||||||
| sidebar_label | Push Changes to GitHub | |||||||
| sidebar_position | 8 | |||||||
| description | Learn how to push your local commits to a remote repository on GitHub. Understand the difference between local and remote repositories and how to keep them in sync. | |||||||
| tags |
|
|||||||
| keywords |
|
Once you’ve made and committed changes to your local repository, the next step is to share those updates with your remote repository on GitHub. This process is known as pushing changes.
Pushing ensures your latest work is uploaded to GitHub so that you (and your team) can access it from anywhere.
The push command in Git uploads your local commits to a remote repository. Think of it as “publishing” your changes to GitHub.
git pushWhen you run this command, Git sends your local branch’s commits to the corresponding branch in your remote repository (for example, main or master).
Before pushing, make sure you’re on the correct branch.
git branchIf you’re on a feature branch (e.g., feature/update-readme), Git will push that branch to the remote repository.
If your local repo isn’t connected to GitHub yet, add the remote URL:
git remote add origin https://github.com/<your-username>/<repository-name>.gitThen verify it:
git remote -vYou should see something like:
origin https://github.com/ajay-dhangar/introduction-to-github.git (fetch)
origin https://github.com/ajay-dhangar/introduction-to-github.git (push)
Now that your repository is ready, push your commits using:
git push origin mainor, if you’re working on a branch:
git push origin your-branch-nameExample:
git push origin feature/add-login-formThis command uploads all your local commits from the branch feature/add-login-form to GitHub.
If it’s your first time pushing, Git may ask for your GitHub username and a Personal Access Token (PAT) instead of your password.
You can create one by visiting: 👉 GitHub Personal Access Tokens
After a successful push, go to your repository on GitHub — you’ll see your commits reflected there
You can also check the commit history:
git log| Issue | Cause | Fix |
|---|---|---|
error: failed to push some refs |
Your local branch is behind the remote branch | Run git pull origin main --rebase before pushing again |
authentication failed |
Invalid credentials or expired token | Generate a new PAT and reauthenticate |
remote: Repository not found |
Wrong remote URL | Double-check the repo name and your GitHub username |
:::tip
If you want Git to remember your remote branch for next time, you can use the -u flag:
git push -u origin mainNow, you can simply run:
git pushfrom then on — Git will automatically know where to push your changes.
:::
You’ve learned how to:
- Understand what “push” means in Git
- Connect a local repository to a remote one
- Push your commits to GitHub
- Handle common push errors
Need help? Ask your questions in the GitHub Discussions. We’re here to help you grow as a contributor