I have a master and a dev branch. I made commits in both. I also deleted some files in dev. I made other commit in master, so this master branch is more recent.
My i
I had the same problem. In my case I think the problem was that, when I did the merge, my copy of the merged-from branch was outdated compared to the remote. (a colleague had done the deletion, not me)
In any case, what fixed it was deleting the whole working copy and cloning it anew.
The only way I can fathom this possible situation is if you created two different files, each with the same filename, in independent branches.
i.e. let's say that the master
and dev
branches already exist.
file.txt
to master
dev
, then again create and commit file.txt
to dev
. Now because you have created two distinct files, git views them as two separate entities despite the same filename, defeating the whole purpose of version control.file.txt
from dev
dev
into master
, and low and behold file.txt
still exists in master
, and this makes sense because like I said, git views the two files as completely independent.notice if you had not deleted file.txt
from dev
and attempted a merge, then you would have gotten a merge conflict because git wouldn't know how to handle two different entities with the same path/filename.
If this is your scenario, then I'm going to risk arrogance and say you're doing it wrong ;)
The point of a version control system is to let the tool manage your differences between a file at different stages in time as well as the relationship of those changes to other files in the repository.
My suggestion to improve the workflow in this situation would be to checkout the specific file from the other branch:
file.txt
to master
checkout dev
, then just grab the particular file from the other branch
git checkout master -- file.txt
In this situation, you will still be on the dev
branch, but have now added file.txt
from the master
branch.
now git recognizes that these are the same entity. so you can delete the file and commit the removal in dev
dev
into master
will now delete file.txt
from master
Merges definitely delete files when there's not a conflict with changes in branches.
git does not preserve files during merging if they are deleted in the merging branch. - Nils_M
To prove it to myself, I had to try to reproduce your scenario. See this demonstration in hopes that you can see what it is you are doing differently.
mkdir test
cd test
git init
Initialized empty Git repository in /test/.git/
echo one > 1.txt
echo two > 2.txt
git add .
git commit -m "init repo"
[master (root-commit) feaa910] init repo
2 files changed, 2 insertions(+)
create mode 100644 1.txt
create mode 100644 2.txt
git checkout -b new
Switched to a new branch 'new'
echo three > 3.txt
rm 2.txt
git add .
git status
On branch new
Changes to be committed:
(use "git reset HEAD ..." to unstage)deleted: 2.txt
new file: 3.txt
git commit -m "changes in new"
[new db6b1a0] changes in new
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 2.txt
create mode 100644 3.txt
git checkout master
Switched to branch 'master'
echo update >> 1.txt
git commit -am "update master"
[master 912a520] update master
1 file changed, 1 insertion(+)
tree
.
├── 1.txt
└── 2.txt
git checkout new
Switched to branch 'new'
tree
.
├── 1.txt
└── 3.txt
git checkout master
Switched to branch 'master'
git merge new master
Removing 2.txt
Merge made by the 'recursive' strategy.
2.txt | 1 -
3.txt | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 2.txt
create mode 100644 3.txt
tree
.
├── 1.txt
└── 3.txt
as you can see, the file 2.txt
deleted in the new
branch is now definitely deleted in master
following the merge.
You can run the merge without committing, make sure you have all of the correct files, and then run the commit.
Perform the merge
git checkout master
git merge --no-commit dev
Make the necessary changes, and fix merge conflicts (if any).
git rm my-files-to-delete
Commit the merge, finishing the process.
git commit