Pushing your changes to GitHub makes your work visible to others and backs up your code in the cloud. This guide will show you how to push your commits from your local branch to the remote repository on GitHub.
What is Pushing?
Pushing sends your local commits to a remote repository (GitHub). It's the opposite of pulling, which downloads changes from the remote repository to your local machine.
- Local commits exist only on your computer
- Pushed commits are stored on GitHub and visible to collaborators
Prerequisites
- A branch with committed changes
- Git configured with GitHub credentials
- Network access to GitHub
- Appropriate permissions to push to the repository
Pushing Your Changes
Step 1: Verify you have commits to push
This should show your branch is ahead of 'origin/branch-name' by X commits.
View unpushed commits:
Step 2: Check your current branch
Make sure you're on the correct branch before pushing.
Step 3: Push your branch to GitHub
First time pushing a new branch:
The -u flag (or --set-upstream) sets up tracking, so future pushes can simply use git push.
Subsequent pushes (after tracking is set up):
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1.23 KiB | 1.23 MiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
To https://github.com/CharlieBigley/ra-tools.git
abc1234..def5678 feature/my-feature -> feature/my-feature
Understanding Push Output
- Enumerating/Counting objects: Git is preparing your commits
- Compressing: Git is compressing data for efficient transfer
- Writing objects: Data is being sent to GitHub
- abc1234..def5678: The range of commits being pushed
- feature/my-feature -> feature/my-feature: Local branch pushed to remote branch
Common Push Scenarios
Scenario 1: Pushing to a specific remote and branch
This explicitly specifies the remote (origin) and the branch name.
Scenario 2: Pushing all local branches
This pushes all your local branches to the remote.
Scenario 3: Pushing tags
Or push tags along with commits:
Scenario 4: Force push (use with extreme caution)
This overwrites the remote branch with your local version. Only use when absolutely necessary.
main or develop. This can cause serious problems for other collaborators. Only force push to your own feature branches if needed.Setting Up Authentication
HTTPS Authentication:
If using HTTPS, you'll need to authenticate:
- Personal Access Token (recommended): Create one at GitHub Settings > Developer settings > Personal access tokens
- Use the token as your password when prompted
- Configure Git to remember credentials:
SSH Authentication:
If using SSH, ensure your SSH key is added to GitHub:
- Generate an SSH key:
ssh-keygen -t ed25519 -C "This email address is being protected from spambots. You need JavaScript enabled to view it. " - Copy the public key:
cat ~/.ssh/id_ed25519.pub - Add it to GitHub: Settings > SSH and GPG keys > New SSH key
Checking Remote Configuration
Step 4: View configured remotes
This shows which remote repositories are configured. You should see origin pointing to the ra-tools repository.
Step 5: Check tracking branch
This shows which remote branch each local branch is tracking.
Working with Forks
Your origin remote points to your fork. You may want to add the original repository as upstream:
Then push to your fork:
Verifying Your Push
Step 6: Verify on GitHub
- Go to the repository on GitHub: https://github.com/CharlieBigley/ra-tools
- Click the "branches" dropdown or "branches" link
- You should see your branch listed
- Click on your branch to view the commits
Pushing Updates to an Existing Branch
Workflow: Continue working and pushing
git add .
git commit -m "Update user validation logic"
# Push the new commits
git push
# Make more changes
git add .
git commit -m "Add error handling"
# Push again
git push
Each push adds your new commits to the remote branch.
What's Next?
After pushing your changes to GitHub, you can:
- Create a pull request to propose your changes
- Request code review from maintainers
- Make additional commits if reviewers request changes
- Merge your changes into the main codebase
Troubleshooting
Rejected Push - Non-Fast-Forward
If you get a "non-fast-forward" error:
This means the remote branch has commits you don't have locally. Solution:
git push
Authentication Failed
If authentication fails:
- For HTTPS: Ensure you're using a Personal Access Token, not your password
- For SSH: Verify your SSH key is added to GitHub
- Check your remote URL:
git remote get-url origin
Permission Denied
If you get a permission error:
- Verify you have write access to the repository
- If working with a fork, make sure you're pushing to your fork, not the original repository
- Check that you're logged in with the correct GitHub account
Nothing to Push
If Git says "Everything up-to-date":
- You've already pushed all your commits, or
- You haven't made any new commits since your last push
- Verify with:
git status
Large Files or Slow Push
If your push is very slow or fails due to large files:
- Check for accidentally committed large files or binaries
- Use
.gitignoreto exclude build artifacts and dependencies - Consider using Git LFS (Large File Storage) for large binary files
