Git overwrites Master with branch
Git is used to keep track of the source code we are working with; it also facilitates collaboration and helps us keep our projects in their current state.
As we develop new features, their history should be at our fingertips as it is very helpful in developing any application or documentation.
Git overwrites Master with branch
Git has two ways to mix changes from one branch into another. One is to merge changes from one branch into another repository rebase
, and the other is to merge changes from any branch 合并
into another repository.
This article will discuss cleanly merging a branch in Git from another repository branch master
.
When using a Git workflow, the changes we make to the code must eventually end master
up in a branch when the application task is completed.
We also have to understand that we might have developed some other branch which contains code changes which are not ready for production deployment and we name that branch as per the organizational requirements and rules dev
.
In some cases, we dev
made too many changes to the branch and then faced difficulties in combining dev
the branch with master
the branch; it could not be performed easily.
One way to overcome this busy situation is to dev
completely replace our master
branch with the branch. We can do it in two ways.
The merge strategy isOurs
To accomplish this strategy, we will first execute the following command to merge the branch into the branch ours
with the help of the merge strategy as shown below.dev
master
git checkout dev
git merge -s ours master
git checkout master
git merge dev
The option in the merge --strategy=ours
is intended to replace the old history of the feature branch. Now our master
will have dev
all the contents of and ignore master
all changes in .
By applying this approach, we will get a clean and safe merge commit; other developers working with these branches can also benefit from this merge as they will not face problems when merging their feature branches.
On the other hand, the disadvantage of this approach is that if our dev
branch and master
branch radiate to a larger scale in the project, this merge may not work.
Force push
Another option is to force push of dev
the branch under a different name, but this approach is brute force compared to the issues discussed above.
git push -f origin dev: master
With the help of the alias flag mentioned in the command -f
, our previous branch master
is completely dev
overwritten by the branch, including the history.
We have to be very careful while applying the above method as it will remove master
all the commits present in the branch as they dev
are also not available in the repository's branch.
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
Close the Git commit editor on Windows
Publish Date:2025/03/31 Views:62 Category:Git
-
In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t
Git Squash Commits
Publish Date:2025/03/31 Views:162 Category:Git
-
We will learn about Git squashing in this tutorial. The basic idea is to merge multiple consecutive commits into one. The main purpose is to compress many commits into a few related commits. Thus, doing so will make the git history look con
Merge remote branches into local branches in Git
Publish Date:2025/03/31 Views:134 Category:Git
-
This tutorial will merge a remote git branch into a local branch by cloning the remote repository and updating the changes locally. Merge remote branches into local branches in Git by cloning the remote repository and updating the changes l
Changing drives in Git Bash
Publish Date:2025/03/30 Views:57 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:106 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se