threads / discuss / 40518

Git deletes files when rebasing an amended merge-commit.

Subject: Git deletes files when rebasing an amended merge-commit.

## tl;dr

2 messages between Oct 8, 2015 and Oct 8, 2015.

replies: 1people: 2as markdown or json

Hans Weltar· Oct 8, 2015, 12:56 UTC · lore

When you have a fast-forwardable merge commit, you can amend this commit to hold additional changes. When you now do a rebase, git will believe this merge-commit is fast-forwardable, and will delete all amended changes. I believe when you amend a merge-commit it should become non-fastforwardable.

As rebase often needs to be applied (eg rebase -i; pull --rebase) this is a rather insidious way to loose files without warning.

Here's a small testcase that creates a ff merge commit holding files f1, f2 and f3. When you execute the last rebase command, the f3 file will disappear from the filesystem.

git init touch f1; git add f1; git commit -m "add f1" git checkout -b mybranch touch f2; git add f2; git commit -m "add f2" git checkout master git merge mybranch --no-ff -m "Merge mybranch" touch f3; git add f3 git commit --amend -m "Add f2 and f3" # Amend merge git rebase HEAD^ # f3 will sneakily be deleted, breaking the build and giving you CI duty :)

kind regards Hans

Junio C Hamano· Oct 8, 2015, 16:06 UTC · re: Hans Weltar · lore

Re: Git deletes files when rebasing an amended merge-commit.

Hans Weltar <hansweltar@gmail.com> writes:
> When you have a fast-forwardable merge commit, you can amend this
> commit to hold additional changes.

The real issue may be that there is a difference between "you can" and "it is a good idea to", though ;-)

I think the fast-forwardable-ness is a red herring in your example; rebase by default replays the patches commit by commit to flatten the history, and evil merges where your result does not match a mechanical merge result will be lost, and that is true even if you were dealing with a real merge.

I'd imagine that you would need something like
http://thread.gmane.org/gmane.comp.version-control.git/198125/focus=198516

where it proposes a mode of rebase that picks the change in the first parent chain.

Thanks.

← back to recent threads