Git – How to push Local Branch to Origin


The basic command to push a local branch to a remote repository is git push.

This command has many options and parameters that you can pass to it, and in this article you will learn the options and parameters that you will use most often.

How to push a local Git branch to origin

If you run the simple command git pushBy default, Git will choose to add two parameters for you: remote repository to push to and branch to push.

The general form of the command is:

$ git push <remote> <branch>

By default, Git chooses origin for your remote and for yours Current branch like branch to push.

If your current branch is maincommand git push will provide two default parameters — running efficiently git push origin main.

In the example below, origin the remote is a GitHub repository and the current branch is main:

(main)$ git remote -v 
origin  [email protected]:johnmosesman/burner-repo.git (fetch)
origin  [email protected]:johnmosesman/burner-repo.git (push)

(main)$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
   b7f661f..ab77dd6  main -> main

From the output, you can see that local main branch is pushed to remote main branch:

   b7f661f..ab77dd6  main -> main

How to push a branch in Git

Normally you would push to a branch and add to its commit history.

But, there are times when you need to over write history of a branch.

There are a number of reasons you might want to do this.

The first reason is to fix bugs — though it’s probably better to just make a new commit revert the changes.

The second and more common scenario is after an action like rebase-change commit history:

Internally, Git is complete [a rebase] by creating new commits and applying them to the specified base. It is very important to understand that even though the branch looks the same, it includes brand new commits.

A created rebase brand new commitment.

This means that if you try to push a restored branch locally — but not on the remote — the remote repository will recognize that the commit history has changed and it will prevent you from pushing until you resolve the differences:

(my-feature)$ git push
 ! [rejected]        my-feature -> my-feature (non-fast-forward)
error: failed to push some refs to '[email protected]:johnmosesman/burner-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

You can make a git pull here to merge the differences, but if you really want to override remote repository you can add --force flags for your promotion:

(my-feature)$ git push --force origin my-feature
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 184 bytes | 184.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
 + edb64e2...52f54da my-feature -> my-feature (forced update)

(Note: you can use -f as a shorthand instead of --force.)

Thrust is a destructive act — only use it when you’re sure that’s what you want to do.

Forced push by lease

Sometimes you may want to push – but only if no one else contributes to the branch.

If someone contributes to your branch and pushes their changes to the remote — and you’re forced to push through that — you’ll overwrite their changes.

To prevent this, you can use --force-with-lease right to buy.

Again from documentation:

–force-with-lease alone, unspecified, will protect all remote refs to be updated by requiring their current value to be the same as the remote tracking branch we have for them.

You’re basically asking Git to force an update to this branch only if it looks like the last time you saw it.

If you are collaborating with others in your branch, it would be better if you avoid using --force or at least use --force-with-lease to avoid losing changes made by other collaborators.

How to push to a branch with a different name on Git

You’ll usually push your local branch to a remote branch with the same name—but not always.

To push to a branch with a different name, simply specify branch you want to push and the name of the branch you want arrive push to separated by a colon (:).

For example, if you want to push a branch called some-branch arrive my-feature:

(some-branch)$ git push origin some-branch:my-feature
Total 0 (delta 0), reused 0 (delta 0)
 + 728f0df...8bf04ea some-branch -> my-feature

How to push all local branches to remote

You won’t need to push all the branches from your local as often, but if you do, you can add --all flag:

(main)$ git branch
* main

(main)$ git push --all
   b7f661f..6e36148  main -> main
 * [new branch]      my-feature -> my-feature


The git push command is a command you will use often and there are a lot of options that can be used with it. I encourage you Read the document for useful options and shortcuts.


Hope this helps!

Source link