
Pushing in Git is the process of transferring your local commits to a remote repository, making them available to collaborators and keeping your project in sync. While git push
uploads changes from your local branch, its counterpart git pull
downloads and merges changes from the remote into your working branch. Understanding this push/pull relationship is key to managing contributions in distributed workflows. This article explains how git push
works across scenarios like GitHub, bare repositories, and force-push workflows, along with usage examples and tips for common flags like -u
, --tags
, and --force
.
The Short Answer Version
# push the local main branch to the origin remote
$ git push origin main
# push the dev branch and link it to origin/dev for future pushes
$ git push -u origin dev
# upload all local Git tags to the remote
$ git push --tags
# delete the old-feature branch from the remote
$ git push --delete origin old-feature
You now know the most common ways to push changes in Git. Let's break down how these commands work and when to use each one.
Push Changes to GitHub
The git push
command uploads your local commits to a remote repository like GitHub. This updates the remote branch with your latest changes, making them visible to collaborators.
Command Syntax
git push [OPTIONS] <REMOTE> <BRANCH>
<REMOTE>
: Usuallyorigin
, the default name for your remote.<BRANCH>
: The branch to update, such asmain
orfeature/login
.[OPTIONS]
:-u
/--set-upstream
: Sets the default remote and branch for future pushes.-f
/--force
: Replaces the remote branch history with your local changes.--all
: Pushes all local branches.--tags
: Pushes all annotated tags.
Command Demonstration
Stage all files in your working directory.
console$ git add .
To stage a specific file.
console$ git add <filename>
Commit your changes with a message.
console$ git commit -m "Add new feature"
Check that your remote is configured.
console$ git remote -v
Output.
origin git@github.com:username/repository.git (fetch) origin git@github.com:username/repository.git (push)
Push to the
main
branch and set it as the default for futuregit push
calls.console$ git push -u origin main
Output:
Branch 'main' set up to track remote branch 'main' from 'origin'.
For all future pushes, just run:
console$ git push
Push Changes to Bare Repositories
A bare repository is a Git repository without a working directory. It stores only version history and is typically used as a central repository on a remote server. Developers push and pull code from this repository using Git operations. Files cannot be edited directly on a bare repo.
Follow these steps to connect to a server, create a bare repository, and push your changes.
Connect to your remote server via SSH.
console$ ssh user_name@server_ip
Replace
user_name
with a valid SSH user (e.g.,ubuntu
) andserver_ip
with the server’s IP or hostname.Change to the location where you want to store repositories.
console$ cd /path/to/your/repositories/
Create a new bare Git repository.
console$ git init --bare your-project.git
Output.
Initialized empty Git repository in /root/git-repos/your-project.git/
Exit the SSH session.
console$ exit
Back on your local system, add the bare repository as a remote:
console$ git remote add bare-server user_name@server_ip:/path/to/your/repositories/your-project.git
Confirm that the remote is configured correctly.
console$ git remote -v
Output.
bare-server user@your-server:/path/to/your-project.git (fetch) bare-server user@your-server:/path/to/your-project.git (push)
Push the local
master
branch to the new remote.console$ git push bare-server master
Output.
To server:/path/your-project.git * [new branch] master -> master
Force Push Changes
Use a force push when your local branch history has diverged from the remote. This often happens after squashing commits, rebasing, or rewriting history. A force push updates the remote branch to match your local state.
--force
flag rewrites the remote branch history. Use it only when you're sure that no one else has pushed changes. For safer collaboration, prefer --force-with-lease
, which prevents accidental overwrites.
If you need to overwrite remote history after rebasing, squashing, or amending commits, use one of the following force-push commands.
Force push the local branch to overwrite remote history.
console$ git push --force origin main
Use a safer alternative to avoid overwriting remote changes made by others.
console$ git push --force-with-lease origin main
--force-with-lease
only proceeds if the remote branch matches your local copy.If you amended the latest commit using:
console$ git commit --amend
Then update the remote branch to reflect the amended commit.
console$ git push --force-with-lease origin main
Conclusion
You learned how to push local commits to both GitHub and bare repositories using the git push
command. This included staging changes, setting upstream branches, handling remote configurations, and using force push when needed. With these techniques, you can confidently sync your local work with remote repositories and maintain a clean, collaborative Git history.
No comments yet.