How To Push To a Branch with Simply "git push"

How To Push To a Branch with Simply "git push"

Learn how to configure a local branch to use `git push` instead of `git push origin name_of_the_branch`.

Many times in your projects you have to create Git branches to keep your workflow and code in order and to safe guard your code from yourself. Let's say you cloned a repository and locally created a branch named development in it. When you make changes to this branch and you do a git add followed by a git commit, the next thing to do is to push your changes to your remote development branch. However you cannot simply run a git push, you have to run git push origin name_of_the_branch (in our case it's git push origin development ). Now that's a lot of typing.

The Solution

To solve this problem, after you do a git add and a git commit, run this command:

git push --set-upstream origin name_of_the_branch

In our imaginary sencario you created a development branch, so you will enter this:

git push --set-upstream origin development

What this does is it tells Git the name of the remote branch to which changes made in your local branch should be pushed to.

Wait! Does Git not know that I am in the development branch and all changes pushed from that branch should go to the remote development branch?

Well by default Git only knows that changes made to the local main or master branch should be pushed to the remote main or master branch. For any other local branch that you create, it will not know where changes from that branch should be pushed. So we have to explicitly tell it where to push. And that's what the above command does.

NOTE: Ensure that you are in the correct branch before running this command, because if for example you are in a branch called staging and run this command:

git push --set-upstream origin development

all changes made to the staging branch will be pushed to the development branch whenever you run git push. Be careful!

Understanding The Command

git push --set-upstream origin name_of_the_branch

git push

In general his command is used to push commits from your local repository to a remote repository.

--set-upstream

This option is used to configure the current local branch to track a remote branch. It sets up a connection between the local and the remote branch so that in the future when you use git push or even git pull, Git knows which remote branch to push to or pull from without explicitly specifying it.

origin

This specifies the name of the remote repository. "Origin" is the default name given to the remote repository when you clone it. It's like a nickname for the remote repository URL.

name_of_the_branch

This is the name of the branch on the remote repository that you want to push your changes to. In our case, we were pushing changes from the local development branch to the remote development branch. If this branch does not exist on the remote repository, it will be automacitally created after running the command and the changes will be pushed to it.

Shorter Command

There is a shorter alternative to this command, it looks like this:

git push -u origin name_of_the_branch

It does the same thing as the previous command.

Conclusion

So running one of these commands after a git add and a git commit, your changes will be automatically pushed to the specified remote repository and your branch will be configured to simply use git push when pushing future changes made to that branch. It will also be configured to use a simple git pull, instead of git pull origin name_of_the_branch.

Thank you for reading. Feel free to follow me for more programming related content. Take care💜