# Git rebasing attribute the change to the wrong file [bug]

2 messages from 2024-02-20 to 2024-02-21. Participants: John Gabo, Elijah Newren.
Thread: https://gitlist.dev/t/60965

## John Gabo, 2024-02-20 20:01

Subject: Git rebasing attribute the change to the wrong file [bug]
Message-ID: <CALPZ9FGr8K=AdqOzX2P7A3T8+_V25SOjcHA5kY-Fg5TUFEvh7w@mail.gmail.com>
URL: https://gitlist.dev/e/CALPZ9FGr8K%3DAdqOzX2P7A3T8%2B_V25SOjcHA5kY-Fg5TUFEvh7w%40mail.gmail.com

```
Hello,
I think I found a bug in git, where a change get attributed to the
wrong file when doing a rebasing.

# Short summary
Assume `master_branch`
ˋˋˋ
.
└── parent_folder
    ├── project_a
    │   └── file.txt
    └── project_b
        └── file.txt # the 2 file.txt have the same content on master
ˋˋˋ
Assume `feature_branch` from master
ˋˋˋ
.
└── parent_folder
    ├── project_a
    │   └── file.txt
    └── project_b
        └── file.txt # this file got modified with a feature
ˋˋˋ
Assume `refactor_branch` from master
ˋˋˋ
.
└── refactored_parent_folder # got renamed
    ├── project_a
    │   └── file.txt
    └── project_b
        └── file.txt
ˋˋˋ
then rebase `feature_branch` on `refactor_branch`,

ˋˋˋ
.
└── refactored_parent_folder
    ├── project_a
    │   └── file.txt # the feature got moved here !
    └── project_b
        └── file.txt # instead of here, where it should be !
ˋˋˋ

# Reproduction bash script
ˋˋˋ
#!/usr/bin/env bash

# tested on linux, git version 2.34.1, 2.43.2.

set -ex
# create a master branch
mkdir -p git_repo
cd git_repo
git init
mkdir -p parent_folder/project_a
echo -e "1\n2\n3" > parent_folder/project_a/file.txt
cp -r parent_folder/project_a parent_folder/project_b
git add .
git commit -m "initial commit"
# we create a feature_branch from master
git branch "feature_branch"

# we create a refactoring_branch from master
git checkout -b "refactoring_branch"

git mv parent_folder refactored_parent_folder
git add .
git commit -m "refactor"

# we create a new feature in project_b
git checkout "feature_branch"
echo -e "1\n2_some_new_feature_in_project_b\n3" >
parent_folder/project_b/file.txt
git add .
git commit -m "new feature"

# we rebase
git rebase refactoring_branch
cat refactored_parent_folder/project_a/file.txt
# refactored_parent_folder/project_a/file.txt contains the line
2_some_new_feature_in_project_I think I found a bug in gitb, which is
WRONG !
# It should have ended  up in refactored_parent_folder/project_b/file.txt


ˋˋˋ
# ENV
tested on linux, git version 2.34.1, 2.43.2 are affected


[System Info]
git version:
git version 2.43.2
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.5.0-18-generic #18~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC
Wed Feb  7 11:40:03 UTC 2 x86_64
compiler info: gnuc: 11.4
libc info: glibc: 2.35
$SHELL (typically, interactive shell): /usr/bin/zsh

[Enabled Hooks]

```

## Elijah Newren, 2024-02-21 02:18

Subject: Re: Git rebasing attribute the change to the wrong file [bug]
Message-ID: <CABPp-BGi6m3SLce8TbZJOMuu610o0PEf+NgaqqfTzZHrqcYzFA@mail.gmail.com>
URL: https://gitlist.dev/e/CABPp-BGi6m3SLce8TbZJOMuu610o0PEf%2BNgaqqfTzZHrqcYzFA%40mail.gmail.com
In-Reply-To: <CALPZ9FGr8K=AdqOzX2P7A3T8+_V25SOjcHA5kY-Fg5TUFEvh7w@mail.gmail.com>

```
On Tue, Feb 20, 2024 at 12:02 PM John Gabo <charleforalia@gmail.com> wrote:
>
> Hello,
> I think I found a bug in git, where a change get attributed to the
> wrong file when doing a rebasing.
>
> # Short summary
> Assume `master_branch`
> ˋˋˋ
> .
> └── parent_folder
>     ├── project_a
>     │   └── file.txt
>     └── project_b
>         └── file.txt # the 2 file.txt have the same content on master
> ˋˋˋ
> Assume `feature_branch` from master
> ˋˋˋ
> .
> └── parent_folder
>     ├── project_a
>     │   └── file.txt
>     └── project_b
>         └── file.txt # this file got modified with a feature
> ˋˋˋ
> Assume `refactor_branch` from master
> ˋˋˋ
> .
> └── refactored_parent_folder # got renamed
>     ├── project_a
>     │   └── file.txt
>     └── project_b
>         └── file.txt
> ˋˋˋ
> then rebase `feature_branch` on `refactor_branch`,
>
> ˋˋˋ
> .
> └── refactored_parent_folder
>     ├── project_a
>     │   └── file.txt # the feature got moved here !
>     └── project_b
>         └── file.txt # instead of here, where it should be !
> ˋˋˋ

You may want to read through the Support section of
https://git.github.io/rev_news/2024/01/31/edition-107/.  You are
dealing with inexact renames rather than exact, but it's still the
same basic issue: git is detecting renames based on file similarity
and you have multiple equally valid targets for it to choose, so it
simply picks one.  As such, it might be considered a limitation of
rename detection that you can't manually enforce which files are
paired, but it's not what we'd classify as a bug since it did
correctly pick one of the files with the most similar contents.

```
