Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge back deleted file from a branch to master

Tags:

git

git-merge

I have a branch A and master in a git repo.

In branch A I have done several additions, deletions. At some point, these changes were also merged in Master but we had to release master without these changes so we removed the file in master.

Now, when merging master into branch A, to get the last changes, Git obviously deletes the files since they have been deleted in master after the last changes.

What is the easiest way to keep branch A changes while merging back last Master changes?

like image 297
arnaud.breton Avatar asked May 29 '26 00:05

arnaud.breton


1 Answers

What git does makes sense, it sounds like you agree with that. I'd let git do what it does and keep the default merge commit, then re-add the missing file in an extra commit:

$ git checkout master
$ git merge A    # the file is gone
$ git checkout A -- file.txt
$ git add file.txt
$ git commit -m"Reintroduce file.txt, which was deleted for release"

I don't like modifying merge commits manually, rather let git do them, then fix afterwards. It causes less surprise.

like image 119
Gauthier Avatar answered May 31 '26 14:05

Gauthier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!