Reputation: 21
I'm currently working on a project with different features, and for each one of them I created a different branch.
All my files are currently in master. But everytime I create a new branch, I delete all the files and keep only the feature related ones on these branches.
My question is: how can I merge the feature branch into the master branch without actually discarding all the other files?
e.g.: Master branch has the files login.html, menu.html and contact.html Feature branch has the feature.html file only.
On branch master, when I execute
git merge feature
All the files are deleted from the master branch, and only the feature.html is kept, but I wanted all of them to be on the master branch, including the feature.html.
Upvotes: 0
Views: 201
Reputation: 30277
You should not be deleting the files from the project on the new branch... but anyway, let's assume that it's a must for whatever reason. If I'm going to eventually merge the branch on master, then I would probably do a little trick to kind of avoid this problem from showing up... but it requires a little bit of additional work. I would delete the files on the first commit of the new branch and I would do nothing else on that revision. Then, when I want to merge back into master, I would not just merge, but I would cherry-pick (or rebase) discarding the commit where I deleted the files and then I would merge the rebased/cherry-picked branch.
Let's assume that I create feature1 branch from master and I do 3 revisions there... something like:
git checkout -b feature1 master
git rm index.html blahblah.html # delete the files I don't want to keep on the branch
git commit -m "Removing the files I don't want"
# work
git commit -m "First real revision of feature 1"
# more work
git commit -m "Second revision of feature 1"
At this point I'm ready to merge into master... then I would't just merge feature 1 because that would delete the files because I did that on the first revision of feature1, right?... I would discard the first revision and then merge into master, and that can be done relatively easily.
git checkout --detach master # go back to master
git cherry-pick feature1~2..feature1 # only cherry-pick the last 2 revisions
git checkout master # go back to master again
git merge --no-ff HEAD@{1} -m "Merging feature 1" # merge rebased branch
That should do. But I don't think you should delete the files anyway.
Upvotes: 2