Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

Pushing Code

Pushing code copies commits from your local computer to a hosted platform such as GitHub, GitLab, or Bitbucket. This makes your committed changes available to other developers and allows your team to review them through a Pull Request.

Follow this checklist to push your code safely and handle common Git push errors.


The Standard Pushing Checklist

Before pushing code, check your working directory, synchronize with the remote branch when appropriate, and then push your commits.

1. Check Your Workspace Status
git status

Shows which files have been modified, staged, or committed. Remember that git push uploads commits, not uncommitted file changes.

2. Pull Incoming Changes
git pull origin <your-branch-name>

Fetches changes from the specified remote branch and integrates them into your current branch. Resolve any conflicts before continuing.

Tip: Ensure you are on the intended local branch before pulling. If you have uncommitted changes, commit or safely stash them first when necessary.
3. Push Your Code
git push origin <your-branch-name>

Uploads your local commits on the specified branch to the remote repository, provided the push is permitted and can be applied.


Pushing Scenarios Matrix

Choose the command that matches your objective.

Goal Command What It Does
Track a New Branch git push -u origin <branch> Pushes the branch and configures upstream tracking.
Push All Local Branches git push origin --all Pushes all local branches to the remote.
Push Tags / Versions git push origin --tags Pushes local tags to the remote repository.
Force Push After Rewriting History git push --force-with-lease origin <branch> Attempts a force push while checking that the remote reference has not changed unexpectedly.
Warning: Force pushes can replace remote history and affect other developers. Use --force-with-lease only when you understand the consequences and are authorized to rewrite the branch history.

Troubleshooting: What If Git Blocks Your Push?

Non-Fast-Forward Rejection

If you see an error containing [rejected - non-fast-forward], the remote branch contains commits that your local branch does not include.

First, fetch and integrate the remote changes:

git pull --rebase origin <your-branch-name>

If conflicts occur, resolve them and continue the rebase as Git instructs. Then push again:

git push origin <your-branch-name>
Alternative: You can use git pull origin <your-branch-name> to merge remote changes instead of rebasing. Follow your team's preferred workflow.
Authentication or Permission Errors

If Git reports an authentication or permission error, check that you are signed in with the correct account, that your credentials or SSH key are configured, and that you have permission to push to the repository.

Protected Branch Errors

If the remote blocks direct pushes to main, create a feature branch, push it, and open a Pull Request if that is your team's required workflow.

git switch -c feature/my-change
git push -u origin feature/my-change

Quick Reference

Command Purpose
git status Check working directory and staging status
git pull origin <branch> Fetch and integrate remote changes
git push origin <branch> Push commits to a remote branch
git push -u origin <branch> Push a branch and set upstream tracking
git push origin --all Push all local branches
git push origin --tags Push local tags
Key Takeaway: Check your branch and working directory, integrate incoming changes when needed, and push your commits to the correct remote branch. Use force-push commands cautiously.