Git - Origin Master



Git is a distributed version control system, which helps the developers to manage and track their code. The terms, origin and master is often used, but creates confusion. In this tutorial, we will learn about the differences of the two and their features.

Git - Origin

Meaning

Origin − In Git it is the default name for the remote repository, from which the local repository gets cloned.

It serves as a shorthand reference to the URL of this remote repository, which is often hosted on platforms like GitHub.

Usage

Origin points to the remote repository from which you clone or to which you push changes.

Commands Used

Following commands can be used with origin in reference:

git fetch origin

The command updates your local repository with changes from the remote repository, but do not merge it.

git fetch origin

git push origin <branch-name>

The local changes are getting pushed to the remote branch.

git push origin main

git pull origin <branch-name>

The changes from the remote branch is fetched and merged into your local branch, using this command.

git pull origin main

Git - Master

Meaning

Master − It is the default name for a repository's main branch. This branch usually contains the stable, production-ready version of the code.

Note that newer repositories might use "main" instead of master because GitHub has changed the default branch name from master to main.

Usage

master or main is the primary branch where all the production code lies.

Commands Used

Following commands can be used with master in reference:

git checkout master or git checkout main − This command helps in switching over to master/main branch.

git merge <branch-name> − This command helps in merging the changes from another branch to master/main.

Pushing Changes to <origin/master>

In order to push the changes, the command used is git push origin mater. It pushes all the local changes from master branch to the remote repository's master branch.

git push origin master

Pulling Changes from <origin/master>

In order to pull the changes, the command used is git pull origin mater. It fetches and merges all the changes from the remote repository's master branch to your local master branch.

git pull origin master

Keeping master Clean and Stable

Generally, all the development activities take place in the feature branches or development branches, which later gets merged into master / main branch, through the pull requests. These pull requests are merged after review and testing.

The best practice is to keep the master / main branch in a stable condition and should contain working and production-ready code.

Using Branches for Development

In order to keep the master branch clean, you should use branches for development, such as, for features or fixes.

  • git branch feature − Create a new branch by using this command under the master branch.

  • git checkout master − Switch to master branch.

  • git merge feature − Merge the feature branch to master branch.

  • git push origin master − Push the master branch to the remote repository.

Conflict Resolution

You may face merge conflicts while pulling or merging changes from origin/master. These conflicts must be resolved manually by editing the files and committing the changes after resolution.

Working in Collaboration

When more than one person is working on a project, it is often helpful to work in collaboration. Make sure before pushing your code to the master branch, you should pull the latest changes to avoid any conflicts. This is done using the command git pull origin master.

Advertisements