Tutorials  /  Linux Basics

Git Push Tutorial: Commands, Examples, and Safe Force Push Options

Ccentron Redaktion · September 2025 ·6 min read ·Linux Basics, Tutorial

Pushing in Git refers to transferring your local commits to a remote repository, making your updates available to others and keeping the project synchronized. While git push uploads changes from your local branch, its counterpart git pull retrieves and merges changes from the remote into your current branch. Grasping this push/pull interaction is essential for handling contributions in distributed workflows. This guide explores how git push operates in scenarios such as GitHub, bare repositories, and force-push workflows, along with usage examples and details about common flags like -u, --tags, and --force.

Quick Reference Examples

Console
# 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

These are the most frequent ways to push changes in Git. Let’s dive deeper into how each command functions and when to use them.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Pushing Changes to GitHub

The git push command transfers your local commits to a remote repository like GitHub. This updates the remote branch with your latest work, allowing collaborators to see and use your changes.

Command Syntax

Console
git push [OPTIONS]  
  • : Typically origin, the default remote name.
  • : The branch you want to update, e.g., main or feature/login.
  • [OPTIONS]:
    • -u / --set-upstream: Defines the default remote and branch for subsequent pushes.
    • -f / --force: Replaces the remote branch history with your local commits.
    • --all: Pushes all local branches.
    • --tags: Pushes all annotated tags.

Command Walkthrough

Stage all files in the working directory:

Console
$ git add .

Stage a specific file:

Console
$ git add 

Commit your staged changes with a message:

Console
$ git commit -m "Add new feature"

Verify that your remote is configured:

Console
$ git remote -v

Example output:

Code
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 default for future pushes:

Console
$ git push -u origin main

Example output:

Code
Branch 'main' set up to track remote branch 'main' from 'origin'.

From now on, simply run:

Console
$ git push

Pushing Changes to Bare Repositories

A bare repository is a Git repository without a working directory. It stores only the project’s history and is usually used as a central repository on a server. Developers push and pull code from this repository, but files cannot be directly edited there.

Steps to Push to a Bare Repository

Connect to your server via SSH:

Console
$ ssh user_name@server_ip

Move to the directory where repositories will be stored:

Console
$ cd /path/to/your/repositories/

Create a new bare repository:

Console
$ git init --bare your-project.git

Example output:

Code
Initialized empty Git repository in /root/git-repos/your-project.git/

Exit the SSH session:

Console
$ exit

On your local machine, add the bare repository as a remote:

Console
$ git remote add bare-server user_name@server_ip:/path/to/your/repositories/your-project.git

Verify the configuration:

Console
$ git remote -v

Example output:

Code
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 bare repository:

Console
$ git push bare-server master

Example output:

Code
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—commonly after squashing commits, rebasing, or otherwise rewriting history. A force push makes the remote branch mirror your local state.

Warning

The --force flag rewrites the remote branch’s history. Use it only when you are certain no one else has pushed updates. For safer collaboration, prefer --force-with-lease, which helps prevent accidental overwrites.

If you must overwrite remote history after a rebase, squash, or amended commit, use one of the force-push commands below.

Force push the local branch to overwrite remote history:

Console
$ git push --force origin main

Use a safer variant to avoid clobbering changes made by others:

Console
$ git push --force-with-lease origin main

--force-with-lease proceeds only if the remote branch still matches your local view.

If you amended the latest commit with:

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 GitHub and to bare repositories using git push—including staging changes, setting upstream branches, checking remote configuration, and using force push when appropriate. With these practices, you can reliably sync local work to remotes and maintain a clean, collaborative Git history.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren