11 Deleting Files in Git

Files and directories sometimes outlive their usefulness. You can remove them from your working tree and tell Git to quit tracking them using the git rm command.

This doesn’t remove the file from your repository’s history; it removes it only from your working tree going forward. You can always go back in the history of the repository and see the files or directories that have been removed.

You call git rm and provide it with a filename to tell Git to remove it (or a standard shell pattern—*.php matches all files that end in .php). You don’t have to provide the --, but it’s necessary if you’re trying to remove a file that conflicts with a command-line option. It tells Git that you’re done providing options, and everything else is a file.

You must provide the -r option if you are deleting a directory and all the files under it. It tells Git to recursively delete all the files starting at the provided directory.

Like most other actions in Git, git rm requires git commit to finalize its action. git rm stages the removal, and git commit finalizes it.

You can undo a git rm before you make a commit through a two-step process. First, you have to reset the index using git reset HEAD. Be sure to provide the filename if you want to leave other staged files alone. Second, check out the file from the repository to restore it using git checkout -- path/to/file.

git rm attempts to keep you from accidentally deleting a file that has changes that have not been committed. You can override this behavior with -f, but be careful. Forcing Git to delete the file removes the file and all traces of the changes that haven’t been committed yet.

What To Do...
  • Delete a file from Git.

    To delete a file called outdated.py, use this:

     
    prompt>​ git rm -- outdated.py
     
    rm 'outdated.py'
     
    prompt>​ git commit -m "remove outdated.py"
     
    [master 42010bf] remove outdated.py
     
    1 files changed, 0 insertions(+), 17 deletions(-)
     
    delete mode 100644 outdated.py
  • Delete a directory from Git.

    To delete a directory called old/, use this:

     
    prompt>​ git rm -r -- old/
     
    rm 'old/outdated.py'
     
    prompt>​ git commit -m "remove the old/ directory"
     
    [master ddbd005] remove the old/ directory
     
    1 files changed, 0 insertions(+), 17 deletions(-)
     
    delete mode 100644 old/outdated.py
  • Get a directory back after deleting it but before committing it.

    This example uses the previous example where old/ is deleted using git rm, but before the staged deletes are committed. There are two steps. First, reset the index:

     
    prompt>​ git reset HEAD -- old/
     
    Unstaged changes after reset:
     
    M old/outdated.py

    Second, check out the files from the repository:

     
    prompt>​ git checkout -- old/
  • Force a file to be removed.
     
    prompt>​ git rm -f -- outdated.py
     
    rm 'outdated.py'

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.122.68