Understanding Git Push and Its Use Cases

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

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

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

  • <REMOTE>: Typically origin, the default remote name.
  • <BRANCH>: 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:

Stage a specific file:

Commit your staged changes with a message:

$ git commit -m "Add new feature"

Verify that your remote is configured:

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

Example output:

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

From now on, simply run:

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:

Move to the directory where repositories will be stored:

$ cd /path/to/your/repositories/

Create a new bare repository:

$ git init --bare your-project.git

Example output:

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

Exit the SSH session:

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

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

Verify the configuration:

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

$ git push bare-server master

Example 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—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:

$ git push --force origin main

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

$ 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:

then update the remote branch to reflect the amended commit:

$ 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.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: