Signup/Sign In

Git Origin Master

Origin and master are two terms frequently used when working on a project managed using Git. Origin is the default name given to a remote repository, and master is simply a branch name. Let's learn more about origin and master, and see how they are used.

Git Origin Master

Origin in GIT

  • Origin, in simple terms, is described as the place from where something begins or is derived.
  • This same definition can be applied to the word origin which is used in Git. In Git terminology, the origin is the name given to the remote repository from where we clone our local repository. This name will be used in the future when we have to push or pull changes from the remote repository.
  • It is a standard default name given to the repository from where our local repository originated. There is nothing extraordinary about this word and we can use any other name that we like.

Consider the example below where we try to clone a remote repository to our local system. First, we will run the Git Clone command, and pass the URL for the remote repository.

Cloning a remote repository

Next, we are going to check the remotes present by using the Git Remote command. We can see in the image below that we have a remote called origin.

Checking the remote name

We can also change this default name with the help of the Git Remote Rename command.

Renaming the remote

Master in GIT

  • Master is the default name that Git gives to a branch when we create a new repository.
  • When a new Git repository is initialized using the Git Init command, it will only have a single branch and that branch, by default, is called the master.
  • This branch will have the final, up-to-date, production-ready code. All other branches will be created based on some commit of this branch and will eventually merge into the master branch.
  • Again, there is nothing exceptional about this name, and we can rename our master branch to something else. This name is just used to denote that it is the main branch of our repository.

Consider the following example where we initialize a new Git repository. We will create an empty commit because a branch is just a pointer to a commit, and we will need one to view the master branch(the branch name is also visible in blue color next to the path of our directory).

Initializing a Git Repository and adding an empty commit to it

Now, we can run the Git Branch command and see that we only have a single branch in our repository which is called the master.

View the branch name by using the Git Branch command

We can rename this branch by using the -m flag with the Git Branch command.

Renaming the master branch

Origin/Master in GIT

  • We just learned that origin and master are two different things but sometimes we may get confused when we see the word origin/master.
  • Origin/master is just a remote-tracking branch present in our local repository that tracks the changes made to the master branch in the remote repository.
  • The origin in origin/master is used to denote the remote name and the master is used to denote the remote branch that it is tracking.
  • Any branch of the format <remote-name>/<remote-branch-name> is a remote tracking branch.
  • If we had changed the name of our remote from origin to genesis and the name of the remote branch was main instead of master, then instead of having origin/master, we will have the name genesis/main.

Now that we know about the general terminology, let's see what actually happens when we clone a repository.

Suppose we have a remote repository with just a single branch called master with three commits on it.

A remote repository with a master branch and three commits

Now, if we clone this repository to our local system, then we will get a local master branch with all three commits. We will also have a remote-tracking branch called origin/master that will track the changes in the remote master branch. And now the remote repository will be the origin of our local repository.

After cloning the remote repository, we get a local master branch and also an origin/master branch that tracks the remote master

Summary

Origin and master are just two default names used commonly by Git. Origin is the remote repository from where we cloned our local repository and we will be pushing and pulling changes to it. Master is the default name given to the first branch present in a Git repository when it is initialized. Both of these names can be changed by the user and have nothing special about them. They are just part of a naming convention that Git follows.