{"thread":{"id":"61893","subject":"Merging problem","startedAt":"2024-08-02T18:50:20Z","lastAt":"2024-08-02T20:54:06Z","messageCount":4,"participants":["Michael Salman","brian m. carlson","rsbecker@nexbridge.com","Sergey Organov"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"499966","messageId":"3db395fe-8d32-4a33-8f16-7df95f3ff194@pathcom.com","threadId":"61893","inReplyTo":null,"subject":"Merging problem","fromName":"Michael Salman","fromEmail":"mjsalman@pathcom.com","sentAt":"2024-08-02T18:41:48Z","receivedAt":"2024-08-02T18:50:20Z","isPatch":false,"sender":{"key":"mjsalman@pathcom.com","avatar":null},"body":"I am new to using git and I encountered the following problem\n\n1) Created a repository using Notepad added a file (FileA.txt). Put one \nline of text in the file. Did a git commit -a. so far so good.\n\n2) I created a branch (my-branch), did a git checkout my-branch\n\n3) Using notepad loaded FileA and changed the first line of text to \nsomething else. Gave command git commit -a no problems\n\n4) Git checkout master looked at FileA nothing changed\n\n5) Did a git merge my-branch. No conflict reported\n\n6) Loaded FileA in master the text of the first line had changed to what \nis in FileA from the branch\n\nYour help with this problem would be appreciated. I hope this is not due \nto my lack of understanding\n\nMike\n"},{"id":"499970","messageId":"Zq0684YmxLqqsSRk@tapette.crustytoothpaste.net","threadId":"61893","inReplyTo":"3db395fe-8d32-4a33-8f16-7df95f3ff194@pathcom.com","subject":"Re: Merging problem","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2024-08-02T20:00:51Z","receivedAt":"2024-08-02T20:00:54Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2024-08-02 at 18:41:48, Michael Salman wrote:\n> I am new to using git and I encountered the following problem\n> \n> 1) Created a repository using Notepad added a file (FileA.txt). Put one line\n> of text in the file. Did a git commit -a. so far so good.\n> \n> 2) I created a branch (my-branch), did a git checkout my-branch\n> \n> 3) Using notepad loaded FileA and changed the first line of text to\n> something else. Gave command git commit -a no problems\n> \n> 4) Git checkout master looked at FileA nothing changed\n> \n> 5) Did a git merge my-branch. No conflict reported\n> \n> 6) Loaded FileA in master the text of the first line had changed to what is\n> in FileA from the branch\n> \n> Your help with this problem would be appreciated. I hope this is not due to\n> my lack of understanding\n\nLet me provide a small shell script that reproduces your report so we\ncan make sure we're on the same page.  Here it is:\n\n----\n#!/bin/sh -e\n\ndir=\"$(mktemp -d)\"\ntrap 'rm -fr \"$dir\"' EXIT\n\ncd \"$dir\"\ngit init -b master\necho \"step 1\" >FileA.txt\ngit add .\ngit commit -m 'step 1'\ngit checkout -b my-branch\necho \"step 2\" >FileA.txt\ngit add .\ngit commit -m 'step 2'\ngit checkout master\ngit merge my-branch\n----\n\nNow, let's look at what happens if we add `git log --graph --all\n--decorate` to the end of the script:\n\n----\n* commit 4dd858b4e2b96ec24055d3a19d87e2080c4f1393 (HEAD -> master, my-branch)\n| Author: brian m. carlson <sandals@crustytoothpaste.net>\n| Date:   Fri Aug 2 19:51:42 2024 +0000\n|\n|     step 2\n|\n* commit a1bf54082762cdcffec185d4cf6eef2c753af535\n  Author: brian m. carlson <sandals@crustytoothpaste.net>\n  Date:   Fri Aug 2 19:51:42 2024 +0000\n\n      step 1\n----\n\nWhat's happened here is that the commits on `my-branch` are a strict\nsuperset of the commits on `master`.  That is, `master` is an ancestor,\nand there are only new commits in `my-branch`.  When that happens, Git\ndoes what's called a fast-forward by default, and simply updates\n`master` to `my-branch` without even invoking the merge algorithm.\nThat's why the contents of the branch are those of `my-branch`.\n\nThe reason that Git does that is that it's much more efficient and\nproduces the same results as actually doing a merge.  When Git does a\nthree-way merge (which is the default behaviour), it only really\nconsiders three points in the merge: the two heads (in this case,\n`master` and `my-branch`), and the _merge base_, which is usually the\nmost recent common ancestor.\n\nWhen it does a merge, Git determines if there is a change in a\nparticular file between the merge base and each of the heads (not\nconsidering intermediate commits).  If one side has a change and the\nother does not, Git adopts that change, regardless of what happened in\nin between.  For this reason, you can see that the merge base is the old\nversion of `master`, and that's also one of the heads, while the other\none is `my-branch`.  So one side never has any changes because its the\nsame commit, and the other side may have changes or not, so it's safe to\njust update the branch pointer to the new commit.\n\nNote that you can merge with `git merge --no-ff my-branch`, which will\ndo a merge, create a merge commit, and avoid the fast forward, but the\nresult is the same, as mentioned above.\n\nSo I think this is working as intended.\n-- \nbrian m. carlson (they/them or he/him)\nToronto, Ontario, CA\n"},{"id":"499971","messageId":"03ed01dae517$22f7bac0$68e73040$@nexbridge.com","threadId":"61893","inReplyTo":"Zq0684YmxLqqsSRk@tapette.crustytoothpaste.net","subject":"RE: Merging problem","fromName":"","fromEmail":"rsbecker@nexbridge.com","sentAt":"2024-08-02T20:04:03Z","receivedAt":"2024-08-02T20:04:21Z","isPatch":false,"sender":{"key":"randall.becker@nexbridge.ca","avatar":"https://avatars.githubusercontent.com/u/28956764?v=4"},"body":"On Friday, August 2, 2024 4:01 PM, brian m. carlson wrote:\n>On 2024-08-02 at 18:41:48, Michael Salman wrote:\n>> I am new to using git and I encountered the following problem\n>>\n>> 1) Created a repository using Notepad added a file (FileA.txt). Put\n>> one line of text in the file. Did a git commit -a. so far so good.\n>>\n>> 2) I created a branch (my-branch), did a git checkout my-branch\n>>\n>> 3) Using notepad loaded FileA and changed the first line of text to\n>> something else. Gave command git commit -a no problems\n>>\n>> 4) Git checkout master looked at FileA nothing changed\n>>\n>> 5) Did a git merge my-branch. No conflict reported\n>>\n>> 6) Loaded FileA in master the text of the first line had changed to\n>> what is in FileA from the branch\n>>\n>> Your help with this problem would be appreciated. I hope this is not\n>> due to my lack of understanding\n>\n>Let me provide a small shell script that reproduces your report so we can make sure\n>we're on the same page.  Here it is:\n>\n>----\n>#!/bin/sh -e\n>\n>dir=\"$(mktemp -d)\"\n>trap 'rm -fr \"$dir\"' EXIT\n>\n>cd \"$dir\"\n>git init -b master\n>echo \"step 1\" >FileA.txt\n>git add .\n>git commit -m 'step 1'\n>git checkout -b my-branch\n>echo \"step 2\" >FileA.txt\n>git add .\n>git commit -m 'step 2'\n>git checkout master\n>git merge my-branch\n>----\n>\n>Now, let's look at what happens if we add `git log --graph --all --decorate` to the end\n>of the script:\n>\n>----\n>* commit 4dd858b4e2b96ec24055d3a19d87e2080c4f1393 (HEAD -> master, my-\n>branch)\n>| Author: brian m. carlson <sandals@crustytoothpaste.net>\n>| Date:   Fri Aug 2 19:51:42 2024 +0000\n>|\n>|     step 2\n>|\n>* commit a1bf54082762cdcffec185d4cf6eef2c753af535\n>  Author: brian m. carlson <sandals@crustytoothpaste.net>\n>  Date:   Fri Aug 2 19:51:42 2024 +0000\n>\n>      step 1\n>----\n>\n>What's happened here is that the commits on `my-branch` are a strict superset of\n>the commits on `master`.  That is, `master` is an ancestor, and there are only new\n>commits in `my-branch`.  When that happens, Git does what's called a fast-forward\n>by default, and simply updates `master` to `my-branch` without even invoking the\n>merge algorithm.\n>That's why the contents of the branch are those of `my-branch`.\n>\n>The reason that Git does that is that it's much more efficient and produces the same\n>results as actually doing a merge.  When Git does a three-way merge (which is the\n>default behaviour), it only really considers three points in the merge: the two heads\n>(in this case, `master` and `my-branch`), and the _merge base_, which is usually the\n>most recent common ancestor.\n>\n>When it does a merge, Git determines if there is a change in a particular file between\n>the merge base and each of the heads (not considering intermediate commits).  If\n>one side has a change and the other does not, Git adopts that change, regardless of\n>what happened in in between.  For this reason, you can see that the merge base is\n>the old version of `master`, and that's also one of the heads, while the other one is\n>`my-branch`.  So one side never has any changes because its the same commit, and\n>the other side may have changes or not, so it's safe to just update the branch\n>pointer to the new commit.\n>\n>Note that you can merge with `git merge --no-ff my-branch`, which will do a merge,\n>create a merge commit, and avoid the fast forward, but the result is the same, as\n>mentioned above.\n>\n>So I think this is working as intended.\n\nAnother option, from my own bag of tricks, is to use git merge --squash for this activity.\nThis collapses your branch into a single commit and applies it to master. This is done\nfor simplification of history. It may not be appropriate for everyone (I have a list of\nreasons why not, if interested).\n\nRegards,\nRandall\n\n"},{"id":"499973","messageId":"87o76alk7v.fsf@osv.gnss.ru","threadId":"61893","inReplyTo":"3db395fe-8d32-4a33-8f16-7df95f3ff194@pathcom.com","subject":"Re: Merging problem","fromName":"Sergey Organov","fromEmail":"sorganov@gmail.com","sentAt":"2024-08-02T20:53:56Z","receivedAt":"2024-08-02T20:54:06Z","isPatch":false,"sender":{"key":"sorganov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8501568?v=4"},"body":"Michael Salman <mjsalman@pathcom.com> writes:\n\n> I am new to using git and I encountered the following problem\n>\n> 1) Created a repository using Notepad added a file (FileA.txt). Put one line of text in the file. Did a git commit -a. so far so good.\n>\n> 2) I created a branch (my-branch), did a git checkout my-branch\n>\n> 3) Using notepad loaded FileA and changed the first line of text to something else. Gave command git commit -a no problems\n>\n> 4) Git checkout master looked at FileA nothing changed\n>\n> 5) Did a git merge my-branch. No conflict reported\n>\n> 6) Loaded FileA in master the text of the first line had changed to what is in FileA from the branch\n>\n> Your help with this problem would be appreciated. I hope this is not\n> due to my lack of understanding\n\nYou didn't tell us what outcome you expected, but to me all looks fine:\nyou've merged your changes (made at step 3) from side branch to the\nmaster branch, and now your changes (made at step 3) are there on the\nmaster branch as well: that's the whole purpose of merges.\n\nAs there were no any changes on master, there is nothing there for a\npotential conflict, so all went pretty smooth, as expected. If you\nchange first line of the file in notepad (differently from step 3)\nbetween steps 4 and 5 as well, and commit your changes, you will then\nget conflict during merge.\n\n-- Sergey Organov\n"}]}