3

In a topic branch I wrongly duplicated a model and a migration (previously created in the master branch) with:

rails generate model User name:string email:string 
bundle exec rake db:migrate

The duplicate model was created but the migration failed because the database already had a users table. Following instructions in how to discard git local branch changes?, I tried to discard all uncommitted changes with git reset --hard, but git status still shows presence of untracked files:

# Untracked files:
# (use "git add <file>..." to include in what will be committed)

    app/models/user.rb
    db/migrate/
    test/fixtures/users.yml
    test/models/user_test.rb

# nothing added to commit but untracked files present (use "git add" to track)

Why did git reset --hard preserve these files? What can I do to safely remove this files in order to completely reset the topic branch to the last commit? Would rails destroy model User suit me?

Community
  • 1
  • 1
Asarluhi
  • 1,280
  • 3
  • 22
  • 43

4 Answers4

4

As @HBHB already correctly said: git reset --hard resets just tracked files.

However you can also remove untracked files using git by calling git clean -df. -d removes also untracked directories and -f (force) has to be provided or otherwise Git won't delete the files.

Tobias
  • 4,523
  • 2
  • 20
  • 40
1

These files are not tracked by git (note they are listed under "Untracked files" in the git status output). git reset --hard removes the changes in all files tracked by git. From the documentation for git reset --hard:

Resets the index and working tree. Any changes to tracked files in the working tree since commit are discarded.

To get rid of them, simply remove them:

rm app/models/user.rb
rm db/migrate/
rm test/fixtures/users.yml
rm test/models/user_test.rb
houtanb
  • 3,852
  • 20
  • 21
1
git clean -f -d 

would be safest bet to remove untracked files. Further you may look at this answer

Community
  • 1
  • 1
Dusht
  • 4,712
  • 3
  • 18
  • 24
0

I always use this command:

git reset --hard HEAD~1

and

rails git:(master) ✗ git reset --hard HEAD~1 HEAD is now at 45e2c462 Skip the pipeline for webpack

AlexSh
  • 1,468
  • 15
  • 12