how to remove untracked files in Git?

后端 未结 8 957
鱼传尺愫
鱼传尺愫 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 15:47

    Those are untracked files. This means git isn't tracking them. It's only listing them because they're not in the git ignore file. Since they're not being tracked by git, git reset won't touch them.

    If you want to blow away all untracked files, the simplest way is git clean -f (use git clean -n instead if you want to see what it would destroy without actually deleting anything). Otherwise, you can just delete the files you don't want by hand.

    0 讨论(0)
  • 2020-12-22 15:50

    The command for your rescue is git clean.

    0 讨论(0)
  • 2020-12-22 15:53

    User interactive approach:

    git clean -i -fd
    
    Remove .classpath [y/N]? N
    Remove .gitignore [y/N]? N
    Remove .project [y/N]? N
    Remove .settings/ [y/N]? N
    Remove src/com/amazon/arsdumpgenerator/inspector/ [y/N]? y
    Remove src/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
    Remove src/com/amazon/arsdumpgenerator/s3/ [y/N]? y
    Remove tst/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
    Remove tst/com/amazon/arsdumpgenerator/s3/ [y/N]? y
    

    -i for interactive
    -f for force
    -d for directory
    -x for ignored files(add if required)

    Note: Add -n or --dry-run to just check what it will do.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-22 16:02

    Well, I had the similar issue. I had taken latest but there were some changes in the local due to which the merge was not happening to a particular file. The file was untracked and I did not want them so What I did was -

    $ git checkout filepath/filename

    filepath - The location from where I did the git bash. then when I took the latest the changes were available

    0 讨论(0)
  • 2020-12-22 16:04

    For deleting untracked files:

    git clean -f

    For deleting untracked directories as well, use:

    git clean -f -d

    For preventing any cardiac arrest, use

    git clean -n -f -d

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