how to remove untracked files in Git?

后端 未结 8 956
鱼传尺愫
鱼传尺愫 2020-12-22 15:22

I\'m working on a branch, say \"experimental\" branch which I branch out from my master branch.Then, I generate a user model in experimental branch, but does not add them to

8条回答
  •  囚心锁ツ
    2020-12-22 16:01

    While git clean works well, I still find it useful to use my own script to clean the git repo, it has some advantages.

    This shows a list of files to be cleaned, then interactively prompts to clean or not. This is nearly always what I want since interactively prompting per file gets tedious.

    It also allows manual filtering of the list which comes in handy when there are file types you don't want to clean (and have reason not to commit).


    git_clean.sh


    #!/bin/bash
    readarray -t -d '' FILES < <(
        git ls-files -z --other --directory |
            grep --null-data --null -v '.bin$\|Cargo.lock$'
    )
    if [ "$FILES" = "" ]; then
        echo  "Nothing to clean!"
        exit 0
    fi
    
    echo "Dirty files:"
    printf '  %s\n' "${FILES[@]}"
    
    DO_REMOVE=0
    while true; do
        echo ""
        read -p "Remove ${#FILES[@]} files? [y/n]: " choice
        case "$choice" in
            y|Y )
                DO_REMOVE=1
                break ;;
            n|N )
                echo "Exiting!"
                break ;;
            * ) echo "Invalid input, expected [Y/y/N/n]"
                continue ;;
        esac
    done
    
    if [ "$DO_REMOVE" -eq 1 ];then
        echo "Removing!"
        for f in "${FILES[@]}"; do
           rm -rfv "$f"
        done
    fi
    

提交回复
热议问题