{"thread":{"id":"60965","subject":"Git rebasing attribute the change to the wrong file [bug]","startedAt":"2024-02-20T20:02:12Z","lastAt":"2024-02-21T02:18:24Z","messageCount":2,"participants":["John Gabo","Elijah Newren"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"489023","messageId":"CALPZ9FGr8K=AdqOzX2P7A3T8+_V25SOjcHA5kY-Fg5TUFEvh7w@mail.gmail.com","threadId":"60965","inReplyTo":null,"subject":"Git rebasing attribute the change to the wrong file [bug]","fromName":"John Gabo","fromEmail":"charleforalia@gmail.com","sentAt":"2024-02-20T20:01:59Z","receivedAt":"2024-02-20T20:02:12Z","isPatch":false,"sender":{"key":"charleforalia@gmail.com","avatar":null},"body":"Hello,\nI think I found a bug in git, where a change get attributed to the\nwrong file when doing a rebasing.\n\n# Short summary\nAssume `master_branch`\n```\n.\n└── parent_folder\n    ├── project_a\n    │   └── file.txt\n    └── project_b\n        └── file.txt # the 2 file.txt have the same content on master\n```\nAssume `feature_branch` from master\n```\n.\n└── parent_folder\n    ├── project_a\n    │   └── file.txt\n    └── project_b\n        └── file.txt # this file got modified with a feature\n```\nAssume `refactor_branch` from master\n```\n.\n└── refactored_parent_folder # got renamed\n    ├── project_a\n    │   └── file.txt\n    └── project_b\n        └── file.txt\n```\nthen rebase `feature_branch` on `refactor_branch`,\n\n```\n.\n└── refactored_parent_folder\n    ├── project_a\n    │   └── file.txt # the feature got moved here !\n    └── project_b\n        └── file.txt # instead of here, where it should be !\n```\n\n# Reproduction bash script\n```\n#!/usr/bin/env bash\n\n# tested on linux, git version 2.34.1, 2.43.2.\n\nset -ex\n# create a master branch\nmkdir -p git_repo\ncd git_repo\ngit init\nmkdir -p parent_folder/project_a\necho -e \"1\\n2\\n3\" > parent_folder/project_a/file.txt\ncp -r parent_folder/project_a parent_folder/project_b\ngit add .\ngit commit -m \"initial commit\"\n# we create a feature_branch from master\ngit branch \"feature_branch\"\n\n# we create a refactoring_branch from master\ngit checkout -b \"refactoring_branch\"\n\ngit mv parent_folder refactored_parent_folder\ngit add .\ngit commit -m \"refactor\"\n\n# we create a new feature in project_b\ngit checkout \"feature_branch\"\necho -e \"1\\n2_some_new_feature_in_project_b\\n3\" >\nparent_folder/project_b/file.txt\ngit add .\ngit commit -m \"new feature\"\n\n# we rebase\ngit rebase refactoring_branch\ncat refactored_parent_folder/project_a/file.txt\n# refactored_parent_folder/project_a/file.txt contains the line\n2_some_new_feature_in_project_I think I found a bug in gitb, which is\nWRONG !\n# It should have ended  up in refactored_parent_folder/project_b/file.txt\n\n\n```\n# ENV\ntested on linux, git version 2.34.1, 2.43.2 are affected\n\n\n[System Info]\ngit version:\ngit version 2.43.2\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.5.0-18-generic #18~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC\nWed Feb  7 11:40:03 UTC 2 x86_64\ncompiler info: gnuc: 11.4\nlibc info: glibc: 2.35\n$SHELL (typically, interactive shell): /usr/bin/zsh\n\n[Enabled Hooks]\n"},{"id":"489048","messageId":"CABPp-BGi6m3SLce8TbZJOMuu610o0PEf+NgaqqfTzZHrqcYzFA@mail.gmail.com","threadId":"60965","inReplyTo":"CALPZ9FGr8K=AdqOzX2P7A3T8+_V25SOjcHA5kY-Fg5TUFEvh7w@mail.gmail.com","subject":"Re: Git rebasing attribute the change to the wrong file [bug]","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2024-02-21T02:18:00Z","receivedAt":"2024-02-21T02:18:24Z","isPatch":false,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Tue, Feb 20, 2024 at 12:02 PM John Gabo <charleforalia@gmail.com> wrote:\n>\n> Hello,\n> I think I found a bug in git, where a change get attributed to the\n> wrong file when doing a rebasing.\n>\n> # Short summary\n> Assume `master_branch`\n> ```\n> .\n> └── parent_folder\n>     ├── project_a\n>     │   └── file.txt\n>     └── project_b\n>         └── file.txt # the 2 file.txt have the same content on master\n> ```\n> Assume `feature_branch` from master\n> ```\n> .\n> └── parent_folder\n>     ├── project_a\n>     │   └── file.txt\n>     └── project_b\n>         └── file.txt # this file got modified with a feature\n> ```\n> Assume `refactor_branch` from master\n> ```\n> .\n> └── refactored_parent_folder # got renamed\n>     ├── project_a\n>     │   └── file.txt\n>     └── project_b\n>         └── file.txt\n> ```\n> then rebase `feature_branch` on `refactor_branch`,\n>\n> ```\n> .\n> └── refactored_parent_folder\n>     ├── project_a\n>     │   └── file.txt # the feature got moved here !\n>     └── project_b\n>         └── file.txt # instead of here, where it should be !\n> ```\n\nYou may want to read through the Support section of\nhttps://git.github.io/rev_news/2024/01/31/edition-107/.  You are\ndealing with inexact renames rather than exact, but it's still the\nsame basic issue: git is detecting renames based on file similarity\nand you have multiple equally valid targets for it to choose, so it\nsimply picks one.  As such, it might be considered a limitation of\nrename detection that you can't manually enforce which files are\npaired, but it's not what we'd classify as a bug since it did\ncorrectly pick one of the files with the most similar contents.\n"}]}