Git Pull Origin branch overwrites Master branch
This article explains how we can revert the changes made to the mastergit pull origin <branch>
branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories .
You pull changes from the remote feature branch to your local repository, but notice that you have it checked out in your local master branch.
How do you revert changes made to the master branch and pull the changes to the feature branch?
Git Pull OriginOverwrite the Master branch
To understand this concept, let's replicate the scenario illustrated above.
Assuming that there are changes in our remote feature branch that are not present in our local repository, we will run git pull
the command as shown below to merge the changes into our local repository.
$ git pull origin feature
请注意
, we are still checked out in the master branch, but ideally we want to bring the changes from the remote feature branch to the local feature branch. Note that the above operation will transfer all commits from the remote branch to the local master branch and create a merge commit.
How can we revert our master branch to its previous state and merge the changes into our local feature branch?
We will start by restoring the master branch. At this point we will use git reset --hard
the command.
We can reset our master branch in two ways using the command.
We can use git reset --hard
the pull command on the parent commit. A parent commit is simply the commit that existed before the merge commit that was produced by the pull.
Run git log --oneline
the command and note the SHA-1 of the parent commit . Remember which commits were introduced from the remote branch.
To reset the master branch, run the following command.
$ git reset --hard 11bd00c
This will reset our master branch to the state it was in before the pull. Alternatively, you can use the HEAD reference.
In this case, we would run the following command:
$ git reset --hard HEAD~1
The command above is the safest option as working with commits can be confusing, especially if the pull pulls in multiple commits. We can now move on to our local feature branch.
$ git checkout feature
We know that git pull
the command combines the git fetch
and git merge FETCH_HEAD
commands. This means that we already have changes from the remote feature branch, but they have not yet been 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 shown below.
$ git merge FETCH_HEAD
This will update our local feature branch to match the remote feature branch.
In summary, if git pull origin <branch>
the command incorrectly updates your master branch, you can revert the changes and move them to the correct branch as described above. The above method applies to all branches.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git shows remote tracking branches
Publish Date:2025/04/03 Views:125 Category:Git
-
Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot
View merged and unmerged branches in Git
Publish Date:2025/04/03 Views:96 Category:Git
-
This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig
The difference between Fork and Branch on GitHub
Publish Date:2025/04/03 Views:157 Category:Git
-
This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar
How to determine the current branch in Git
Publish Date:2025/04/03 Views:164 Category:Git
-
Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams
Difference between Git Merge Origin/Master and Git Pull
Publish Date:2025/04/03 Views:195 Category:Git
-
This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha
Copy files from another branch in Git
Publish Date:2025/04/03 Views:52 Category:Git
-
In Git, merging various files can lead to a lot of conflicts. Through these merge conflicts, our files may be compromised, so we have to copy these files or folders from one branch to another to keep them safe. A popular approach is cherry
Push to a specific branch in Git
Publish Date:2025/04/02 Views:54 Category:Git
-
In Git, we use branches to develop independent features directly from the main workflow of the project. Since Git is considered to be the best version control system so far, we have local and remote branches in our repository for different
Solve the Git Push Everything Up-To-Date Problem
Publish Date:2025/04/02 Views:120 Category:Git
-
Git is a free, open source version control system designed to work with projects quickly and efficiently. You can make changes to your repo and push them to master branches. This article explains how to use git push the command to resolve e
Git merge development branch into feature branch
Publish Date:2025/04/02 Views:127 Category:Git
-
Creating new branches and merging them is considered to be the most powerful tool of Git. The ability of Git to create new branches and merge them into the development code is very useful for developers working in a team environment. This f