0

If I merge commit or branch with elder files, will old files overwrite new ones in Git? Or new files always have preference? Or merge order matters?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • Possible duplicate of [Difference in git merge order?](https://stackoverflow.com/questions/21867407/difference-in-git-merge-order). According to it, the result will be the same (assuming no conflicts), but the order can change referring to different commits (HEAD^1~5, or something like that). – Major Feb 13 '18 at 15:49
  • @Major result will be the same and -- which? New files will be put instead of old ones? – Dims Feb 13 '18 at 17:08
  • `git merge` merges the files, meaning it will keep both files. Any difference in the files will both be there, so if `old` is merged with `new`, everything in both files will be there. A caveat is if the file has conflicts, meaning the same portion of the file is different in `old` and `new`. [This page](https://linux.die.net/man/1/git-merge) gives a bit more information about conflicts and conflict resolution. – Major Feb 13 '18 at 17:38
  • @Major suppose there are no conflicts. Suppose file A has some code removed in commit 2 with respect to commit 1 and file B has some code added in commin 2 with respect to commit 1. What will happen on merge? – Dims Feb 13 '18 at 20:57
  • If there are no conflicts, then everything in both commits will be incorporated in the new commit. Say `commit a` has `file_1` and `file_2`. `commit b` has `file_1` and `file_3`. The new, `commit x` will have all three files. Think of a merge as a combining everything together. If you want to select different sections and decide on a per-section basis what you want in the new commit, look into [`git add --patch`](https://nuclearsquid.com/writings/git-add/). – Major Feb 13 '18 at 21:16
  • @Major regard only 2 files; first file grew, second file shrunk – Dims Feb 14 '18 at 10:27
  • git tracks changes made to files, not necessarily the files themselves. So if you have two branches come off the same commit, the new branches only track the changes that were made to them. If one branch had things removed, and the other had things added, when merged the differences will be applied. – Major Feb 15 '18 at 17:09

0 Answers0