Git does not apply deleted files when merging an old branch into the master. How can I tell Git to apply deleted files?

前端 未结 4 777
小鲜肉
小鲜肉 2021-01-02 03:42

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

相关标签:
4条回答
  • 2021-01-02 04:15

    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.

    0 讨论(0)
  • 2021-01-02 04:19

    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.

    1. create and commit file.txt to master
    2. checkout 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.
    3. later delete file.txt from dev
    4. merge 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:

    1. create and commit file.txt to master
    2. 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.

    3. now git recognizes that these are the same entity. so you can delete the file and commit the removal in dev

    4. merging dev into master will now delete file.txt from master
    0 讨论(0)
  • 2021-01-02 04:26

    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.

    create a new repository

    mkdir test
    cd test
    git init
    

    Initialized empty Git repository in /test/.git/

    create and add some files to master branch

    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

    create new branch and add/delete some files

    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

    commit some more changes in master

    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

    verify files still deleted in new

    git checkout new
    

    Switched to branch 'new'

    tree
    

    .
    ├── 1.txt
    └── 3.txt

    merge new into master

    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.

    0 讨论(0)
  • 2021-01-02 04:33

    You can run the merge without committing, make sure you have all of the correct files, and then run the commit.

    1. Perform the merge

      git checkout master
      git merge --no-commit dev
      
    2. Make the necessary changes, and fix merge conflicts (if any).

      git rm my-files-to-delete
      
    3. Commit the merge, finishing the process.

      git commit
      
    0 讨论(0)
提交回复
热议问题