How to Pull Origin Branch Overwrites Master Branch in Git
This article illustrates how we can revert changes made to the master
branch after running the git pull origin <branch>
command. Let’s say you have a master
branch and a feature
branch on your local and remote repositories.
You pull changes from the remote feature
branch to your local repository but notice that you are checked out in your local master
branch.
How do you go about reverting the changes made to the master
branch and pulling the changes to the feature
branch?
Git Pull Origin <Branch>
Overwrites Master Branch
To understand the concept, let’s replicate the scenario illustrated above.
Assuming that we have changes in our remote feature
branch that are not present in the local repository, we will run the git pull
command, as illustrated below, to incorporate the changes to our local repository.
$ git pull origin feature
Note that we are still checked out in our master
branch, but ideally, we want to bring the changes from the remote feature
branch to the local feature
branch. Note that the action above will bring down all the commits from the remote branch to the local master
branch and create a merge commit.
How do we go about reverting our master
branch to its previous state and incorporating the changes to the local feature
branch?
We will start by reverting the master
branch. We will employ the git reset --hard
command at this juncture.
We can use the command to reset our master
branch in two ways.
We can use the git reset --hard
command with the parent commit. The parent commit is simply the commit that was present before the merge commit that resulted from the pull.
Run the git log --oneline
command and note down the SHA-1
of the parent commit. Keep in mind what commits were introduced from the remote branch.
To reset the master
branch, run the command below.
$ git reset --hard 11bd00c
This will reset our master
branch to its previous state before the pull. Alternatively, you can use the HEAD
reference.
In this case, we will run the following:
$ git reset --hard HEAD~1
The above command is the safest option, as using the commit can be confusing, especially if the pull introduces multiple commits. We can now proceed to our local feature
branch.
$ git checkout feature
We know that the git pull
command combines the git fetch
and git merge FETCH_HEAD
commands. This means we already have the changes from the remote feature
branch, but they are not yet merged into our feature
branch.
We do not need to run the git pull origin feature
command again. Instead, we can merge the changes, as illustrated below.
$ git merge FETCH_HEAD
This will update our local feature
branch to match the remote feature
branch.
In conclusion, if the git pull origin <branch>
command updates your master
branch by mistake, you can revert the changes and move them to the correct branch, as discussed above. The above method applies to all branches.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn