The git push command (to push) is the operation responsible for sending your local commits to the remote repository and updating the server to reflect your new history.
In the previous article, we learned how to bring the team’s work to our computer using fetch and pull. Now it’s time to do exactly the opposite path: publish your code so the rest of the team can download and use it.
Unlike systems like Dropbox or Google Drive, which continuously sync files in the background, in Git synchronization is manual and intentional.
You decide the exact moment when your work is ready to be uploaded to the cloud (usually to origin).
Let’s see how to use git push, and how to solve the problems you might encounter when using this command (and how to fix them).
Normal Push
If you are working on a branch that already exists both on your computer and on the server(for example, the main branch), the operation is straightforward.
Simply open the terminal and execute:
git push
Copied!
What does Git do under the hood?
Compares your local branch with the server’s.
Identifies which commits you have that the server doesn’t.
Packages those commits, sends them over the network, and tells the server: “Move your main tag forward to point to this new commit”.
If you try to push a branch that only exists locally, Git will throw an error. You create a new branch on your computer (git switch -c mi-nueva-feature), make a couple of commits, run git push, and:
fatal: The current branch mi-nueva-feature has no upstream branch.To push the current branch and set the remote as upstream, use git push --set-upstream origin mi-nueva-feature
Copied!
Why does it fail? Because local branches are private by default. The origin server has no idea that the mi-nueva-feature branch exists. Git doesn’t know which remote or which target branch to send the data to.
To fix it, the first time you push a branch you have to link it with the remote. Listen to Git and use the -u flag (short for --set-upstream):
git push -u origin mi-nueva-feature
Copied!
This command does two things: uploads your branch to GitHub/GitLab and creates a permanent link between your local branch and the remote one.
Thanks to this, the next times you want to push code in that branch, you’ll only need to type git push by itself.
When Git Rejects Your Push
Now things get a bit more complicated, Git has rejected your Push. You tried to push, but your console fills with red letters:
! [rejected] main -> main (fetch first)error: failed to push some refs to 'github.com:...'hint: Updates were rejected because the remote contains work that you do not have locally.
Copied!
Blah blah blah, Push rejected
Don’t worry, nothing is broken. This is simply Git’s defense mechanism kicking in. Git is protecting the server’s code.
What happened? The server detected that its history has new commits that you don’t have on your local machine. Someone (a teammate) did a push before you.
If Git accepted your upload right now, your history would overwrite your teammate’s and their commits would be deleted. That’s why it rejects the operation.
The Solution: Download and Integrate
The error message itself gives you the clue: (fetch first).
As we saw in the previous article, you have to bring the server’s code to your machine, integrate it with yours, and then upload the final result.
Download and integrate: Run git pull. Git will merge your teammate’s commit with yours.
Push again: Now that your local history is up-to-date and contains all the changes, run git push again. This time it will go through without issues.
Sometimes, when you are an advanced user fixing a mess, you know your history is the correct one and you will want to deliberately overwrite what’s on the server.
To force Git to accept your code, ignoring its safety warnings, there is the force push:
git push -f
Copied!
(or git push --force)
What happens is devastating for your teammate’s work. Their commit disappears from the server as if it never existed:
Never, under any circumstances, do a git push -f on shared branches like main or develop unless you know exactly what you are doing and have warned the entire team.
You will delete your teammates’ work from the server irreversibly.
The use of force push is usually strictly limited to your own development branches (feature/mi-rama) before integrating them with the rest of the project.