How will git merge this?

John and Tom clone the same remote repo. The contents of the remote repo are as follows:

FileA.c 

John makes a local commit like this, i.e. it moves the FileA.c file to a folder named John and commits it locally.

 John\FileA.c 

Tom also makes a local commit like this, i.e. it moves FileA.c to a folder named Tom and commits it locally.

 Tom\FileA.c 

If Tom and John both push their local repositories to a remote repo, tell GitHub, how does Git merge this?

I hope I get it. Thanks!

+4
source share
6 answers

When you do git commit but don't do git push , the changes are added to your local repository and are not tied to the global repo. So, no matter who promotes his changes, he will not run into conflicts, since the remote server is not aware of the changes made by another user, not yet pushed . Later, when the next user tries to push the changes, git will give an error that the global repo has been changed, and you must first transfer the changes. Since the changes are in one file and the location has been changed, there will be a conflict that will not be resolved by git itself. The next user will have to make changes again and click it to update the global repo.

+4
source

Git / SVN / etc, do the merging as best as possible: if they see that separate, non-interfering lines of code have been added / deleted / changed, then it will simply merge the changes made by John / FileA.c and Karen / FileA.c (his practice of making someone in your example a girl = P). If the merge script shows that the same lines of code have been edited, then it marks it as a conflict in the file.

I think this moment has been made, but this process takes place locally; either Karen or John, depending on who the person was trying to push the latter. The person who clicked the last one will be notified to pull out of the remote repo and resolve any conflicts before making their changes.

+2
source

Git will never merge during push . One of the two developers (the poor one who is trying to click the last one) should resolve the conflict locally and tell git where to move the file. git cannot know where the file should end without human intervention.

 # dev1 git clone … mkdir John && git mv FileA.c John/FileA.c git commit -m 'move file to john subdir' git push origin master # dev2 git clone … mkdir Tom && git mv FileA.c Tom/FileA.c git commit -m 'move file to tom subdir' git push origin master # git errors out: non-fast-forward, pull first to resolve potential conflicts git pull origin master # merge conflict in FileA.c # tell git which file to delete and which file to keep: git rm Tom/FileA.c && git add John.FileA.c git commit # creates a merge commit git push origin master 
+1
source

The second push operation will not be performed; merging should be done locally. In this case, the conflict will be resolved manually.

+1
source

The best part about git is that it’s easy to try these things for yourself without affecting any servers. Result for the last guy to press:

 02:32:54 ~/desktop/tom $ git push To /cygdrive/h/desktop/test ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '/cygdrive/h/desktop/test' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (eg 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details. 02:32:54 ~/desktop/tom $ git pull remote: Counting objects: 3, done. remote: Total 2 (delta 0), reused 0 (delta 0) Unpacking objects: 50% (1/2) Unpacking objects: 100% (2/2) Unpacking objects: 100% (2/2), done. From /cygdrive/h/desktop/test 307f68a..2539f44 master -> origin/master error: refusing to lose untracked file at 'John/FileA.c' error: refusing to lose untracked file at 'John/FileA.c' CONFLICT (rename/rename): Rename "FileA.c"->"Tom/FileA.c" in branch "HEAD" rename "FileA.c"->"John/FileA.c" in "2539f448bb15d10f14bef74688dc3470975c2dbf" CONFLICT (rename/rename): Rename "FileA.c"->"Tom/FileA.c" in branch "HEAD" rename "FileA.c"->"John/FileA.c" in "2539f448bb15d10f14bef74688dc3470975c2dbf" Automatic merge failed; fix conflicts and then commit the result 

Git is enjoyable and asks which rename is correct. Please note that not all version control systems are capable of detecting such a rename / rename conflict.

+1
source

The key point from the other answers is that git does not know what should happen [using some pre-encoded presumption], but rather it will warn the user about difficulties because he cannot fail to read the various users intentions.

Most other systems that claim the ability to perform such mergers do not pass the clairvoyance test - they do not know what you really wanted. Let smart people (that you and I ;-) make decisions.

0
source

Source: https://habr.com/ru/post/1369199/


All Articles