JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Merging and forcing overwrites in Git

Author:JIYIK Last Updated:2025/03/31 Views:

Most of the time, when we apply merge git pushor git mergemerge_s, we end up with some conflicts. In some cases, the resolution of a merge conflict is as simple as discarding local changes or changes from a remote or other branch.

When Git can't figure out how to merge two conflicting changes, it creates a conflict request. A conflict request is a special patch that describes the problem, contains both sides of the conflicting changes (ours and theirs), and the results of the merge attempt.

When two team members work on the same file and a conflict occurs in that file, Git applies the conflict to our working file. We can then edit the resulting file and Git will record our changes. If conflicting changes do occur, Git will mark the file as being in a conflicted state. There are several commands that can resolve conflicts in that particular branch.

Conflicts are most common when two or more people are working on the same file in the same repository. However, when conflicts are found in a file, Git is very smart and clever to resolve the conflict in a very awesome way. Git uses conflict markers to show which parts of a file are in conflict. Conflict markers are small hashes placed on both sides of the conflicting parts of the file.

Pull is not used alone. It is used with the help of fetching data from the remote server and then applying merge with the changes in the local repository. We can perform both the operations mentioned below if we want.

git fetch
git merge origin/$CURRENT_BRANCH

The meanings of mentioned above origin/$CURRENT_BRANCHare as follows.

  • Git will apply the merge options and apply the changes from the remote repository, ie origin.
  • This was added to$CURRENT_BRANCH
  • It does not currently exist in our locally checked out branch

git pullNot only is it recommended, it's just running git fetchfollowed by git mergeWe're going to have three merges, and through those merges, Git will do three fetches, one of which is what we need.

git fetch origin   # it will update all our origin/* remote-tracking branches
git checkout new branch        
git merge origin/new branch     
git checkout master
git merge origin/master
git merge -X theirs new branch  
git push origin master   

The command mentioned above will effectively ignore any divergent changes on the branch we are merging into and develop a new commit on the branch we are merging into with all the commits merged on that branch.

We can also use in a normal merge --oursto merge all the changes in the branch we're merging into, and then skip any files that exist in the branch we're merging into, effectively doing a three-way merge between the two branches, and then only using the files from the branch we're merging into.

We found it much easier git merge --oursto merge the files using and then git rebase -imanually reapply the changes from the branch I wanted to merge using .

The above command did not work for files with conflicts, but we found the following command to resolve the conflicts.

git checkout file_with_conflict
git merge --ours --no-commit file_from_branch_with_conflict
git reset --hard git add file_with_conflict git commit -m 

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.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial