3

Is there a simple command in git to remove all changes made to a branch and remove all untracked files? I would like to have a clean branch.

git checkout does leave some untracked files and also shows a message that your files will be overritten...

git reset --hard HEAD also leave untracked files

Roman
  • 19,581
  • 6
  • 68
  • 84
Patryk
  • 618
  • 7
  • 17
  • 2
    Regarding untracked files, you can run `git clean -f` after `git reset --hard HEAD`. However, think twice before using those commands; they're irreversible. – jub0bs Jan 08 '16 at 14:30
  • 2
    `git stash --include-untracked` will remove all changes, including untracked files. You can then drop the stash. That'd kind of an abuse of the command though. – Roman Jan 08 '16 at 14:31
  • But there is no single command to do the above, you would need to add that as a **git alias** ? – Patryk Jan 08 '16 at 14:44
  • You can take a look at the list of available aliases in [oh-my-zsh git plugin](https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh#L69). There are some good ideas in there, including the one I linked to. – Roman Jan 08 '16 at 15:37

2 Answers2

2

Two ways to do this with two commands.

From Jubobsref

git reset --hard $rev
git clean -f

From R0MANARMYref

git stash --include-untracked
git stash drop

Either of those can be aliased to a single command if necessary.

Community
  • 1
  • 1
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
0

You have several ways to do what you want.

Detailed answer can be found here: How to move HEAD back to a previous location? (Detached head)

Another option which is not there and is suitable answer for your question is git clean.

git clean

You can do a git clean to remove all the un-tracked files (Backup files)

git clean -Xfd // capital X
git clean -xfd // small x

-x (lower case)

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options.

This allows removing all untracked files, including build products.
This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X (uppercase)

Remove only files ignored by Git.
This may be useful to rebuild everything from scratch, but keep manually created files.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167