Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git conflict (rename/rename)

After merging branched I've received a conflict (rename/rename) on bunch of files, with file~HEAD, and file~my_test_branch created. How to resolve these?

Thanks

like image 768
spacemonkey Avatar asked Feb 06 '13 15:02

spacemonkey


People also ask

How do I rename a file in Git?

Git has no rename, just like in Linux. Git facilities a rename through heuristics of a delete and add in the same commit. The file can be modify on the other end, but if it differs to much it will consider it a new file.

How do I resolve a merge conflict in Git?

There are three ways to resolve a merge conflict in Git: 1. Accept the local version. To accept all changes on a file from the local version, run: git checkout --ours <file name>. Alternatively, to accept the local version for all conflicting files, use: git merge --strategy-option ours.

How do I know if I have unmerged paths in Git?

In the unlikely event that you have overlooked these warning messages when the conflict happened, Git additionally informs you whenever you run git status: $ git status On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add/rm <file>..."

Is it possible to merge branches in Git?

Actually, Git's merging capabilities are one of its greatest advantages: merging branches works effortlessly most of the time, because Git is usually able to figure things out on its own. But there are situations where contradictory changes were made - and where technology simply cannot decide what's right or wrong.


1 Answers

Given the following test-setup:

git init resolving-rename-conflicts
cd resolving-rename-conflicts
echo "this file we will rename" > will-be-renamed.txt
git add -A
git commit -m "initial commit"
git checkout -b branch1
git rename will-be-renamed.txt new-name-1.txt
git commit -a -m "renamed a file on branch1"
git checkout -b branch2 master
git rename will-be-renamed.txt new-name-2.txt
git commit -a -m "renamed a file on branch2"
git checkout master

Then merging branch1 and branch2

git merge --no-ff branch1
git merge --no-ff branch2

Yields:

CONFLICT (rename/rename): Rename "will-be-renamed.txt"->"new-name-1.txt" in branch "HEAD" rename "will-be-renamed.txt"->"new-name-2.txt" in "branch2"
Automatic merge failed; fix conflicts and then commit the result.

git status

On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

    added by us:        new-name-1.txt
    added by them:      new-name-2.txt
    both deleted:       will-be-renamed.txt

no changes added to commit (use "git add" and/or "git commit -a")

If you want to keep one file, say new-name-2.txt:

git add new-name-2.txt
git rm new-name-1.txt will-be-renamed.txt
git commit

Of course in chosing one file or the other, you may have other changes to make to files that reference this file by-name. Also, if there are other non-rename changes to the file, pre-or-post rename on the branches, you will need to manually diff and merge those to retain them in the file you are retaining.

If you instead want to keep both files:

git add new-name-1.txt new-name-2.txt
git rm will-be-renamed.txt
git commit
like image 186
javabrett Avatar answered Oct 17 '22 14:10

javabrett