Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to clean up un-staged deleted files in Git

Tags:

git

Often I move files in a git repository using my IDE or via the command line (not via git mv).

As a result I and end up with several unstaged files to be deleted on my next commit as below:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.html
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    css/bootstrap.css
#   deleted:    css/bootstrap.min.css
#   deleted:    img/glyphicons-halflings-white.png
#   deleted:    img/glyphicons-halflings.png
#   deleted:    js/bootstrap.js
#   deleted:    js/bootstrap.min.js

I typically will select all the deleted files and edit them in a text editor to produce like:

git rm  css/bootstrap.css
git rm  css/bootstrap.min.css
git rm  img/glyphicons-halflings-white.png
git rm  img/glyphicons-halflings.png
git rm  js/bootstrap.js
git rm  js/bootstrap.min.js

Which I then throw back into the console.

Is there a way to do this without having to copy/paste?

like image 357
Allyl Isocyanate Avatar asked Jun 06 '12 12:06

Allyl Isocyanate


People also ask

How do I remove deleted files from staging?

In your case you must have used git rm to remove the file, which is equivalent to simply removing it with rm and then staging that change. If you first unstage it with git reset -- <file> you can then recover it with git checkout -- <file> .

How do I remove staged changes in git?

If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job: $ git reset HEAD file # Or everything $ git reset HEAD . To only remove unstaged changes in the current working directory, use: git checkout -- .


3 Answers

If I understand your question correctly, you want to commit your deletes...that is, you want to perform the equivalent of git rm for all of the files that show up as deleted. git clean won't do this.

You can run git add -u:

Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

This will pick up all changes to tracked files, including deletes. So if you start with this:

# Changes not staged for commit: #   (use "git add/rm <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #   deleted:    file2 #   deleted:    file3 #   deleted:    file4 #   deleted:    file5 

Running git add -u will get you to this:

# Changes to be committed: #   (use "git reset HEAD <file>..." to unstage) # #   deleted:    file2 #   deleted:    file3 #   deleted:    file4 #   deleted:    file5 

And a commit at this point will do the right thing.

like image 146
larsks Avatar answered Sep 18 '22 08:09

larsks


I usually do a commit of the files I made changes to.

If i'd have deleted redundant files, I would do the following after this:

git commit -a -m 'deleted redundant files' 
like image 43
Jeffrey Vandenborne Avatar answered Sep 18 '22 08:09

Jeffrey Vandenborne


git add -u will update the status of tracked files.

like image 21
KurzedMetal Avatar answered Sep 20 '22 08:09

KurzedMetal