0

I have a 'master' branch with 5 files names

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

I branched master into 'edit_branch'. I make these changes

edit file1.txt
edit file2.txt
delete file3.txt
delete file4.txt
delete file5.txt
add file6.txt

When I merge 'edit_branch' to master, I want these changes
file1.txt --> updated
file2.txt --> updated
file3.txt --> no change
file4.txt --> no change
file5.txt --> no change
file6.txt --> added

Essentially, the files that are added or modified in the new branch should merge back in the 'master' branch. The deletions mentioned above are necessary.

Any suggestions on how to branch out and set up the repo?

Shaswat Rungta
  • 3,635
  • 2
  • 19
  • 26

2 Answers2

3

This is a question relating the fundamental behavior of git. You want to merge the changes from a branch to another branch while treating deletions as "no change". But basically, I think, that is impossible, because git treats deletions as changes and hence reflects the deletions on merge.

In your specific case, master still contains file3.txt, file4.txt, file5.txt and these 3 files are not modified in any sense on branch master.

But in the branch 'edit_branch' they have been removed (preferably by git rm) and hence changed on edit_branch.

So, now if you merge 'edit_branch' to master, git will see that master is basically an ancestor commit of edit_branch, and hence will do a fast-forward merge by simply designating the latest commit of edit_branch as master. That means it will always reflect the deletions.

But if the files on master are modified after creation of the edit_branch, then git will at least show merge CONFLICTS on the deleted files, thus telling you about which files have been deleted and hence need being taken care about.

So, basically there is no direct way of doing what you want to do. But a probable workaround would be to do some dummy modifications (like adding a single whitespace in the end / or writing a comment explaining why the modification was done) on all files on master, just after creating edit_branch. Then even if you switch to edit_branch and delete some files there accidentally, at least you'll get notified about it during merging and get a chance to handle it before merging.

I would like to add that such deletion-merge scenarios are never really useful in any known real use cases. When you delete some files on a branch just to keep the directory clean and make the branch intentionally different from the core, you do not merge it to the core thereafter. For example, in case of github-pages, we create a branch gh-pages and delete every file other than the webpages on that branch, effectively hiding unnecessary content from the branch. But we do not find the need to merge gh-pages back to master.

PS. If you are coming home during the convocation, consider buying me a beer :)

  • I thank you for a detailed explanation. But this still does not solve the problem at hand. The closest I had was to stage only the modified and added files for commit and in some sense, do a partial merge with the master. The workaround is easy to miss, though. Curses to manual steps. :| – Shaswat Rungta Jan 12 '16 at 05:25
  • i does not look like there is a direct solution to this problem :( – Ganesh Prasad Jan 15 '16 at 14:44
0

Did you merge from master ?

ls : Licence.txt MyPatch.patch Readme.txt

git branch test
git checkout test

rm Licence.txt

git status : test   deleted:        Licence.txt
git add --all
git commit -m "Remove Readme.txt"

ls : Licence.txt MyPatch.patch

git checkout master

ls : Licence.txt MyPatch.patch Readme.txt

git merge test : Readme.txt | 2 --  1 file changed, 2 deletions(-) 
                 delete mode 100644 Readme.txt

ls : Licence.txt MyPatch.patch

Mirouf
  • 92
  • 1
  • 6