Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - discard added files

Tags:

git

Seems like this should be super-simple but looking around for a simple (or half-way straightforward) solution seems impossible:

Say I add a bunch of files to a Git project. "git status" now says:

# On branch master  
# Untracked files:  
#   (use "git add <file>..." to include in what will be committed)  
#  
#   Classes/FileA.h  
#   Classes/FileA.m  
#   Classes/FileB.h  
#   Classes/FileB.m  
nothing added to commit but untracked files present (use "git add" to track)  

I then decide I want to discard these files and get back to my original status within the project. Do I have to delete each file before proceeding?!!

I've tried:

git checkout master  

which gives "Already on 'master'" and

git reset --hard HEAD

but my added files are still there for both methods.

I've been through the entire Git tutorial (http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html) plus various man pages.

Why is this so difficult to do?!!

like image 566
Snowcrash Avatar asked Jun 23 '10 17:06

Snowcrash


People also ask

How do I delete unstaged files in git?

For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.

Does git reset remove added files?

git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.

How do I discard a file in git?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.


1 Answers

git clean

should do the trick. By default, this requires the -f flag to really do anything.

like image 91
Thomas Avatar answered Oct 12 '22 12:10

Thomas