git/push

Push to Remote

Upload your local commits to a remote repository, making your changes available to collaborators and updating the remote branch

git
push
remote
upload
share
collaboration

Command

git push

Explanation

The git push command uploads your local branch commits to a remote repository, making your changes available to other team members and updating the remote branch with your local work. This is a critical command for collaboration, as it's how you share your code changes with others. When you push, Git sends your commits, along with any necessary objects (files, trees, etc.), to the remote repository. The command requires you to have write access to the remote repository. The first push of a new branch typically requires the -u flag to set up tracking between your local branch and the remote branch. Pushing only sends commits that exist locally but not on the remote - it won't overwrite remote commits unless you use force push (which should be used with extreme caution). Regular pushing is essential in team environments to keep the remote repository up to date with everyone's work. The command also supports pushing tags, specific branches, or all branches. Understanding when and how to push is fundamental to collaborative Git workflows and prevents conflicts by keeping all team members synchronized.

Common Use Cases

  • Sharing local commits with team members
  • Uploading a new feature branch to remote repository
  • Updating remote branch after local commits
  • Deploying code to production after local testing
  • Backing up local work to remote repository

Best Practices

  • Always pull before pushing to avoid conflicts
  • Use git push -u origin <branch> for new branches
  • Review changes with git log before pushing
  • Never force push to main/master branches
  • Push frequently to keep remote repository updated
  • Use --dry-run flag to preview what will be pushed

Common Mistakes to Avoid

  • Pushing without pulling first and causing conflicts
  • Using force push on shared branches (destructive)
  • Forgetting to set upstream with -u on first push
  • Pushing to wrong remote or branch
  • Pushing sensitive data like passwords or API keys

Troubleshooting

Problem: Rejected push - remote has work you don't have

Solution: Pull the remote changes first with git pull, resolve any conflicts, then push again.

Problem: Permission denied when pushing

Solution: Check your SSH keys or HTTPS credentials. Verify you have write access to the repository.

Problem: Branch has no upstream tracking

Solution: Use git push -u origin <branch-name> to set upstream, or configure with git branch --set-upstream-to.

Examples

Push current branch to tracked remote

git push

Push to specific remote and branch

git push origin main

Push new branch and set upstream tracking

git push -u origin new-branch

Push all tags to remote

git push --tags

Force push (dangerous - overwrites remote history)

git push --force