Git

Git Push Origin Example

In this example, we shall understand git push origin command. git push command updates remote references using local references by sending objects necessary to complete the given references.

In simple words, git push command updates remote repository with local commits. origin represents remote name where user wants to push the changes. Let’s understand this command in more detail.
 
 
 
 
 
 
 

1. Git Push

git push command push commits made on local branch to a remote repository.

The git push command basically takes two arguments:

  • A remote name, for example, origin
  • A branch name, for example, master

This makes the structure of command as:
git push <REMOTENAME> <BRANCHNAME>

In case command line doesn’t specifies where to push with the REMOTENAME argument, branch.*.remote configuration for the current branch is referred to determine where to push. If the configuration is missing, it defaults to origin.

2. Git Push Origin in Action

To see git push origin command in action, we shall start with creating a git repository.

First we shall create a directory for master branch.

Create Directory for Master Branch
Create Directory for Master Branch

And in this repository we shall be initializing our Git repository. This shall be done using git init command.

Initialize Git Repository
Initialize Git Repository

Now we shall be create a new file in this directory with some text.

Create a new file
Create a new file

Now that we have created our file, we shall be adding it to the repository and then committing the same using git add and git commit command respectively.

Git Commit
Git Commit

Now we shall create a directory where we shall be cloning the Git branch that we created in above steps.

Create Directory where Master shall be cloned
Create Directory where Master shall be cloned

After this we shall use git clone command to clone the master git repository.

Clone Master Branch
Clone Master Branch

To see the git remote server configured, we can use command git remote. In this, it shall be origin.

Git Remote
Git Remote

We can also use command git remote -v to see the URLs that Git has stored for the shortname to be used when reading and writing to that remote.

Git Remote details
Git Remote details

Now we shall see git push origin command in action. First we shall make some changes to the file in the cloned directory.

Make changes to file
Make changes to file

After making changes we shall add and commit file using commands git add and git commit respectively.

Commit modified file
Commit modified file

And the final step, we shall use command git push origin to send the changes to remote repository.

Git Push Origin
Git Push Origin

3. Conclusion

In this example, we learnt the significance of git push origin command, what happens if it is not specified and it’s usage.

Saurabh Arora

Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Phil Savech
Phil Savech
11 months ago

Nice! Thank you!

Back to top button