Git pull origin <branch> Overwrites master
The “git pull” command is used to get the content of the GitHub repository in the local repository. While working on Git, developers work on different branches for each feature. Once they complete work on the local machine, they are required to push all new changes to the GitHub repository branches. For this purpose, it is required to perform the git pull operation. Additionally, Git allows them to overwrite the local branch with the Git remote branch.
This write-up will explain if and how the “git pull origin <branch>” command overwrites the master.
How Does “git pull origin <branch>” Command Overwrites master?
To overwrite the local master branch with a similar remote branch, first, go to the required repository and verify the remote URL list. Then, download the latest remote repository changes to the local Git repository. Next, push the local content to the GitHub repository. Lastly, merge the “master” branch of the local repository with the “master” branch of the remote Git repository to overwrite it.
Step 1: Move to Local Git Directory
First, run the below-stated command along with the required repository path and redirect to it:
Step 2: Check Remote URL List
Then, verify whether the local repository is linked with the centralized server known as GitHub or not:
According to the below-given output, the local machine is connected with the remote repository:
Step 3: View Repository Content
Execute the “ls” command to display the existing list of content of the current working repository:
The screenshot below indicates that the current repository contains three text files:
Step 4: Fetch Remote Repository Content
After that, download the content of the remote repository in the local repository with the help of the given-below command:
Step 5: Pull Remote Branch Content
To get the latest content of the remote branch, run the “git pull” command:
Here, the “–allow-unrelated-histories” option is used for telling the Git that user is permitted to merge the branches of both unrelated local and remote repositories:
Note: Here, we have not specified the name of the remote, which indicates that we are already connected with the remote repository at the back end through the remote URL provided previously.
Step 6: Push Local Content to Remote Repository
After that, run the below-provided command to push the local changes to the specific remote repository:
Step 7: Perform Merging Operation on Local Branch and Remote Branch
Finally, set the position of the HEAD pointer to the remote branch “master” with the help of the “git reset” command:
Here, the “–hard” option is used to reset the current branch HEAD and delete changes in the working directory and staging index:
It can be seen that the local branch has merged with the remote branch.
Step 8: Verify New Changes
Lastly, check the commit history to view the new changes:
In the below screenshot, it can be observed that the HEAD is pointing to the local “master” branch as well as the remote “master” branch which means the local branch has been overwritten with the remote branch.
If the user pushes the local branch content to the remote branch, they are not required to mention the remote URL. It will push changes to the provided remote URL:
That was all about how the “git pull origin <branch>” command overwrites the master branch.
To overwrite the “master” branch of the local repository with the “master” branch of the remote Git repository, first, redirect to the desired local repository. Then, fetch and pull the remote branch to get the latest content of it. Next, push the local changes to the GitHub repository. Lastly, merge both branches using the “git reset –hard <remote>/<branch>” command to overwrite them. This write-up demonstrated how to overwrite the master branch using the “git pull origin <branch>” command.