The following untracked working tree files would be overwritten by merge, but I don't care

前端 未结 16 1021
挽巷
挽巷 2020-12-02 03:37

On my branch I had some files in .gitignore

On a different branch those files are not.

I want to merge the different branch into mine, and I don\'t care if t

相关标签:
16条回答
  • 2020-12-02 04:11

    Update - a better version

    This tool (https://github.com/mklepaczewski/git-clean-before-merge) will:

    • delete untracked files that are identical to their git pull equivalents,
    • revert changes to modified files who's modified version is identical to their git pull equivalents,
    • report modified/untracked files that differ from their git pull version,
    • the tool has the --pretend option that will not modify any files.

    Old version

    How this answer differ from other answers?

    The method presented here removes only files that would be overwritten by merge. If you have other untracked (possibly ignored) files in the directory this method won't remove them.

    The solution

    This snippet will extract all untracked files that would be overwritten by git pull and delete them.

    git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"
    

    and then just do:

    git pull
    

    This is not git porcelain command so always double check what it would do with:

    git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"
    

    Explanation - because one liners are scary:

    Here's a breakdown of what it does:

    1. git pull 2>&1 - capture git pull output and redirect it all to stdout so we can easily capture it with grep.
    2. grep -E '^\s - the intent is to capture the list of the untracked files that would be overwritten by git pull. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them.
    3. cut -f2- - remove whitespace from the beginning of each line captured in 2.
    4. xargs -I {} rm -rf "{}" - us xargs to iterate over all files, save their name in "{}" and call rm for each of them. We use -rf to force delete and remove untracked directories.

    It would be great to replace steps 1-3 with porcelain command, but I'm not aware of any equivalent.

    0 讨论(0)
  • 2020-12-02 04:13

    The only commands that worked for me were:

    git fetch --all
    git reset --hard origin/{{your branch name}}
    
    0 讨论(0)
  • 2020-12-02 04:13

    A replacement for git merge that will overwrite untracked files

    The comments below use 'FOI' for the 'files of interest', the files that

    • exist in the donor branch,
    • do not exist in the receiving branch,
    • and are blocking the merge because they are present and untracked in your working directory.
    git checkout -f donor-branch   # replace FOI with tracked `donor` versions
    git checkout receiving-branch  # FOI are not in `receiving`, so they disapppear
    git merge donor-branch  # now the merge works
    

    A replacement for git pull that will overwrite untracked files

    pull = fetch + merge, so we do git fetch followed by the git checkout -f, git checkout, git merge trick above.

    git fetch origin  # fetch remote commits
    git checkout -f origin/mybranch  # replace FOI with tracked upstream versions
    git checkout mybranch  # FOI are not in mybranch, so they disapppear
    git merge origin/mybranch  # Now the merge works. fetch + merge completes the pull.
    

    Detailed explanation

    git merge -f does not exist, but git checkout -f does.

    We will use git checkout -f + git checkout to remove the Files Of Interest (see above), and then your merge can proceed normally.

    Step 1. This step forcibly replaces untracked FOI with tracked versions of the donor branch (it also checks out the donor branch, and updates the rest of the working dir).

    git checkout -f donor-branch
    

    Step 2. This step removes the FOI because they they are tracked in our current (donor) branch, and absent in the receiving-branch we switch to.

    git checkout receiving-branch
    

    Step 3. Now that the FOI are absent, merging in the donor branch will not overwrite any untracked files, so we get no errors.

    git merge donor-branch
    
    0 讨论(0)
  • 2020-12-02 04:16

    If you consider using the -f flag you might first run it as a dry-run. Just that you know upfront what kind of interesting situation you will end up next ;-P

    -n 
    --dry-run 
        Don’t actually remove anything, just show what would be done.
    
    0 讨论(0)
提交回复
热议问题