Git Push

The git push command uploads your local commits to a remote repository, like GitHub or GitLab. Until you push, your commits are only on your computer.

Syntax

git push <remote> <branch>
git push -u origin main
git push
git push origin --all
git push origin --tags
git push origin --delete <branch>

Push for the first time

In this chapter we use a local folder as the server, so you can try everything without any account. With GitHub, only the URL is different.

Terminal output

$ git push -u origin main
To /home/robin/team/server.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.

The option -u (upstream) connects your local main with origin/main. After that, you can just type git push and git pull without any names.

Push new commits

Terminal output

$ echo "v2" >> app.txt
$ git commit -q -am "Update app"
$ git push
To /home/robin/team/server.git
043f4e2..e57ca08 main -> main
$ git status -sb
## main…origin/main

When the push is rejected

Now a second developer, Bob, takes the project, makes a change and pushes it first.

Terminal output

$ cd ..
$ git clone -q server.git bob
$ cd bob
$ echo "bob change" > bob.txt
$ git add .
$ git commit -q -m "Bob adds a file"
$ git push
To /home/robin/team/server.git
e57ca08..513b153 main -> main

Alice does not know about the work of Bob. When she tries to push her own new commit, Git refuses, because her history is behind the remote.

Terminal output

$ cd ../alice
$ echo "alice change" > alice.txt
$ git add .
$ git commit -q -m "Alice adds a file"
$ git push
To /home/robin/team/server.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '/home/robin/team/server.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push –help' for details.

Read the hint. The remote has a commit which you do not have. The right solution is to take the remote changes first with git pull (see Git Fetch and Pull), and then push again.

Terminal output

$ git pull
From /home/robin/team/server
e57ca08..513b153 main -> origin/main
Merge made by the 'ort' strategy.
bob.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 bob.txt
$ git push
To /home/robin/team/server.git
513b153..bd1622b main -> main

Force push (be careful)

A force push replaces the remote branch with your local one, even if it deletes the commits of other people. It is needed only after you rewrote history (for example after rebase or amend) on your own branch.

git push --force-with-lease

The option --force-with-lease is much safer than plain --force, because it fails if someone pushed new commits that you have not seen.

Useful push commands

CommandWhat it does
git push -u origin branchPush a new branch and set its upstream
git push origin --allPush all local branches
git push origin --tagsPush all tags
git push origin --delete branchDelete a branch on the remote
git push origin local:remotePush a local branch to a different remote name
Note: Never force push to main or any shared branch. Most teams protect the main branch on GitHub or GitLab so nobody can do it by mistake. Next: Git Fetch and Pull.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.