Open In App

Git Push

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

The Git Push command is used to upload commits from your local repository to a remote repository (e.g., GitHub, GitLab, Bitbucket). Once pushed, other developers can access these changes and contribute by pulling them into their local repositories.

Before pushing, you must commit your changes locally using:

git commit -m "Commit message"
Git push

In the above diagram, we can observe If our local main branch is way back when compared to the central main repository after that git push origin main will publish the changes. git push is essential as same as the git merge.

git push <remote> <branch>
  1. The <remote>  The alias of the remote repository (origin)
  2. The <branch> The branch you want to push (main , develop)

Example:

git push origin main

Setting Up a Remote Repository

If no remote is linked, add one using:

git remote add <remote-name> <remote-URL>

Example:

git remote add origin https://github.com/username/repo.git

Now you can push your changes:

git push -u origin main

-u sets origin main as the default upstream branch for future pushes.

Useful Variations of Git Push

Push all branches

git push --all <remote>

Force push (use with caution)

git push --force <remote> <branch>

Overwrites remote history with your local history. Use only in special cases (e.g., rewriting commits).

Force push with safety

git push --force-with-lease <remote> <branch>

Safer than --force because it prevents overwriting changes you don’t know about.

Git Push and Synchronization

When multiple developers are working:

Commit changes locally

git commit -m "message"

Push changes to remote

git push origin branch-name

Pull latest changes before pushing again

git pull

Resolve conflicts if needed

Merge conflicts may occur if multiple people edited the same files.

Re-push after resolving conflicts

git push

Pushing To Bare Repositories

A bare repository contains only Git metadata and objects, without a working directory. It’s commonly used as a central repository.

Following are the steps to push into the bare repository:

Step 1: Create a bare repository.

git init --bare <directory>

Step 2: Clone the repository.

Clone the bare repository to the local machine. By using the following command.

git clone

Step 3: Make changes and Commit the changes.

git commit -m "Commit message"

Step 4: Push the changes.

After committing the changes to your local machine. Now it is time to push them to the bare repository by using the following command.

git push <Alias name of remote repository>  <Branch name>

By pushing the local repository to the central repository you are making it available for other developers who need to work on it.

Amended Force Push

If you want to change your last commit and update it on the remote:

Step 1: Amend commit:

git commit --amend -m "New commit message"

Step 2: Push with lease:

git push --force-with-lease <Alias name> <Branch name> 

using  "--force-with-lease"  will force push only if the remote branch's current commit matches the expected commit. It will prevent accidental overwriting of others' changes.

Git Push Usage

After developing a feature and committing it locally, you can upload changes to a remote repository so others can access them:

git push <remote> <branch>

Common Variations

git push <remote> --force: This command forces the push even if it results in a non-fast-forward merge.

git push <remote> --force

git push <remote> --all: This command pushes all the local branches to the specified remote repo.

git push <remote> --all

Set upstream branch:

git push -u origin master

-u sets the default upstream branch so future pushes can be done with just git push.

If you see an error like “origin does not appear to be a git repository”, it means no remote is set. Add one first:

git remote add origin <repo-URL>
Create a new Repository.
 

To add this repo project to your GitHub account, use the git remote add command on the terminal, in the directory where your repository is currently stored at.

git remote add <remote-name><remote URL>

This command takes two arguments:

  1. A remote name, let’s say for example, “Origin”
  2. A remote URL, which is your repo address as shown below.
HTTPs URL
 

Now you can run the git remote add command and git push command to push your code work to your repository.

git remote add
 

We used crio here in place of origin which is the repo name coz we assign an origin to another repo and if you assign an origin to a certain repo, you cannot use it again.

git repository in GitHub
 

Now, the code is successfully pushed to the repository.

Suggested Quiz
5 Questions

What is the primary purpose of the git push command?

  • A

    To save changes to the local repository

  • B

    To upload local commits to a remote repository

  • C

    To merge two local branches together

  • D

    To initialize a Git repository

Explanation:

git push sends local commits from your machine to a remote repository such as GitHub or GitLab. This makes your changes available to collaborators. It does not save changes locally — that’s the job of git commit.

What does the -u option do when running git push -u origin main?

  • A

    Deletes the upstream branch

  • B

    Sets the default editor for Git

  • C

    Sets the upstream branch so future pushes only require git push

  • D

    Forces overwriting the remote branch

Explanation:

Using -u sets the upstream tracking relationship between your local branch and the remote one. After running it once, you can simply run git push without specifying the remote or branch name.

Which variation of force push is considered safer because it prevents overwriting commits you don’t know about?

  • A

    git push --hard

  • B

    git push --all

  • C

    git push --force-with-lease

  • D

    git push --no-edit

Explanation:

git push --force-with-lease only performs a force push if the remote branch is in the expected state. If someone else pushed changes, it refuses, preventing dangerous overwrites. This makes it safer than --force.

What happens if you attempt to push but your local branch is behind the remote branch?

  • A

    The push is always forced

  • B

    Git will deny the push and ask you to pull first

  • C

    Git automatically merges changes

  • D

    The remote branch is deleted

Explanation:

If your branch is behind the remote, Git will reject the push to prevent overwriting others' work. You must run git pull, resolve any conflicts, and then push again.

Why are bare repositories commonly used as central repositories?

  • A

    They support GUI interfaces

  • B

    They allow direct editing of files on the server

  • C

    They contain no working directory and exist only for pushing/pulling

  • D

    They run faster than non-bare repositories

Explanation:

Bare repositories contain only Git metadata and no working tree, which means users cannot modify files directly. This makes them ideal central repositories for collaboration, where developers push and pull changes without risking conflicts from edited server files.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore