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

前端 未结 4 776
小鲜肉
小鲜肉 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: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.

提交回复
热议问题