Pushing your code to a remote repository is the final stage of “saving” the changes you have made to a Git repository on your local machine.
The pushing process transfers the code from your local repository—your computer—to the remote repository with which your local code is associated. This allows you to store the changes you have made to a codebase in the main repository for a project.
This tutorial will discuss, with examples, the basics of pushing code and how to use the git push command. By the end of reading this tutorial, you’ll be an expert at pushing code using the git push command.
In Git, “saving” changes is not as simple as saving a file. When you save a file in a Git repository, the changes will be stored on your computer, but they will not be tracked by the Git repository until you specify that those changes should be tracked.
Once you have committed your code to a repository, a record of the changes you have made will be tracked. But if you want your commits to show up on a remote repository, you need to push your code after you have created a commit.
Pushing code allows you to send the commits you have made to the local version of a repository to a remote repository. For instance, if you are working on a team project, you’ll first create a commit on your local machine. Then, once you’re ready for everyone to see your code, you will push it to the remote repository so every collaborator can see your code.
How to Push Your Code
The git push command is used to “push” the changes made to a local repository to a remote repository. In a sense, git push is the opposite of git fetch, which is used to retrieve the changes made to a remote repository and apply them to your local copy of a repository.
The syntax for the git push command is as follows:
git push <remote name> <branch name>
The “remote name” parameter refers to the repository to which your code should be pushed. If you have already configured a repository, this will be set to “origin”. If you want to commit to another repository, you can specify it using the “remote name” parameter.
The “branch” name parameter refers to the branch of the remote repository to which you want to push your changes.
Suppose we want to push the changes from a local repository to the “master” branch of a remote repository. We could do so using this command:
git push origin master
The command returns:
Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 363 bytes | 363.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/jamesgallagher432/demo-repository.git 3b16026..b53b22d master -> master
For this example, our remote repository is stored on GitHub. So, when we push our code, the changes we made to our local repository are pushed to the remote GitHub repository with which our code is associated.
Now that we have pushed our changes, the code on our local machine is the same as the code in our remote repository. This means that now our code is available in the remote repository, our team members will be able to see the changes we have made.
Alternatively, if we wanted to push our code to the branch “v1.9”, we could have specified the branch name “v1.9” instead of “master”, for example.
Git Push Force
When you’re using the git push command line operation, you may want to ignore the local changes made to a Git repository and force them to be pushed to a remote repository.
By default, Git prevents you from pushing code to a repository when there is a conflict between the remote and local histories of a repository. So, if a remote repository has 10 commits that are not reflected on your local machine, you will not be able to push your code.
The –force flag allows you to “force push” the changes you have made to a repository. This will delete any changes that may have occurred since you last pulled code from the repository (or, in other words, retrieved the latest version of the repo).
You should only use the –force flag when you notice that you have made an error in a push that you have fixed. Otherwise, you should avoid using this command, to ensure you do not make unintended changes to a Git repository which are reflected in the main version of your code.
The syntax for the –force flag is:
git push <remote name> <branch name> --force
This command will force push your code.
Push All Tags
The git push command pushes your commits on one branch to a specific remote branch. However, by default, it does not push the Git tags you have created in your repository.
If you want to push your tags to a remote repository, you can use the –tags flag. Here’s the syntax for this flag:
git push <remote name> <branch name> --tags
When you run this command and specify a remote repository and branch names, both the changes you have made to your repository and the tags you have created will be pushed to your remote repository.
Git Push: A Common Scenario
Let’s walk through a common scenario where you may want to use the Git push command.
Suppose you have just made a few changes to your local code that you want to push to a remote repository. You have already created a commit using git commit in which your changes are stored. To push your code, there are a few steps you should follow.
First, you should navigate to your master branch and make sure it is up to date. You can do so using these commands:
git checkout master git fetch origin master git rebase -i origin/master
The git checkout command makes us navigate to the “master” branch. The git fetch command allows us to retrieve the latest version of our remote repository. The rebase command allows us to clean up our commits before sharing them.
Then, you can run git push to push your code:
git push origin master
Upon running this command, the code in our local repository will be pushed to our remote repository. Because we checked if our code was up-to-date before we pushed our code, there should be no errors returned by the git push command.
The git push command is used to “push” the changes from a local Git repository to a remote repository. Once you push your changes to a remote repository, everyone who is working on the codebase will be able to see the contributions you have made.
This tutorial discussed the basics of pushing code and how to use the git push command. Now you’re ready to start pushing your code to a remote Git repository like a Git master!