-1

So I made a new git branch, did some things, added some files, tested some other things.

Decided I didn't want any of those changes so I committed on the new branch and then deleted it thinking that git would delete everything that was done in that new branch.

I switched back to my master and some of the files in the branch directory were still there.

The revisions of other files returned to their pre-branched state which is good but I am still left with a fair bit of folder garbage that is left over.

I tried doing a...

git reset --hard oldcommit

but that did not cause the files to be removed either.
How can I make git remove these files?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Joff
  • 11,247
  • 16
  • 60
  • 103
  • Have you tried git rm – User123456 Dec 23 '15 at 06:17
  • 2
    Possible duplicate of [git reset --hard HEAD leaves untracked files behind](http://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind) – Maximillian Laumeister Dec 23 '15 at 06:20
  • I know I can manually remove them with git-rm, but what if I don't want to manually remove tons of files, there should be a way for git to delete them automatically, right? – Joff Dec 23 '15 at 06:22

1 Answers1

1

You have to "clean" the working directory and the index from all the content that you did not commit

You can do it with:

rm <any leftovers>
git clean -Xfd // Capital X
git clean -xfd // small x 

Here are some more advanced options:

Several options:

  1. Git reset to a given commit

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    
  2. checkout the last desired

    git checkout -b <new_branch> <commit id>
    
    // Now your branch contains all the commits up to the 
    // desired one and without the newer ones
    
  3. git revert

    git revert will allow you to rollback any commit, what you need to do is to make a list of all the commits in A which you want to remove (with script) and then revert them from the second repository

    git revert <commit1> <commit2> ... <commitn>
    
  4. filter-branch

    Use this option to completely remove the commits from the history.

    git filter-branch --commit-filter '
        if [ `git rev-list --all --grep "<log-pattern>" | grep -c "$GIT_COMMIT"` -gt 0 ]
        then
            skip_commit "$@";
        else
            git commit-tree "$@";
        fi'  
    HEAD 
    
CodeWizard
  • 128,036
  • 21
  • 144
  • 167