Git Push - Export commits to remote repository

Git

← Prev

In this tutorial we will learn about Git push to transfer commits from local repository to a remote repository.

In the previous tutorial Git Fetch we learned how to import changes from a remote repository into our local repository as remote branch.

The git push command is completely opposite of the git fetch command. It is used to export commits from local repository to remote repository.

In the above image we have the remote repository origin/master branch which is behind the master branch of the local repository. So, we will git push the local commits to the remote repository and it will move the origin/master pointer to point at the latest commit similar to master branch pointer of the local repository.

Caution! The git push command override changes so, be careful when pushing commits to remote repository.

Push commits

To export changes to a remote repository we use the git push [remote] [branch-name] command where, remote is the name set for the remote location and branch-name is the name of the branch that we want to push to the remote repository.

In the following example we are pushing the local dev branch to remote repository named as origin.

$ git push origin dev
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 246 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yusufshakeel/git-project.git
   8bef7bb..07ddcb7  dev -> dev

Git will prevent export (push) of commits if it is not a fast-forward merge in the destination repository.

Force push

To forcefully push commits to a remote repository we use the git push [remote] [branch-name] --force command.

In the following example we are performing push to dev branch in the remote repository but it has some changes not in the local repository. So, we are having issues in exporting the local changes to the remote repository.

$ git push origin dev
To github.com:yusufshakeel/git-project.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:yusufshakeel/git-project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So, we will perform force git push.

$ git push origin dev --force
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 246 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yusufshakeel/git-project.git
 + 1277ed3...07ddcb7 dev -> dev (forced update)

Now we are able to export (push) commits from the dev branch in the local repository to the dev branch in the remote repository.

Push all branches

To push all the branches to a remote repository we use the git push [remote] --all command.

$ git push origin --all
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 351 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:yusufshakeel/git-project.git
   07ddcb7..d320e51  dev -> dev
← Prev