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

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

提交回复
热议问题