1

I would like to write a script that needs a clean working directory. I want to make sure that any rebases, merges, or anything else that might not have ended properly is completely gone from my working directory.

git reset --hard should work for merges, and git rebase --abort will work for rebases, but there are other states like reverting or cherry-picking as well. Is there any sure way to clear all such events in order to get my working directory into a clean state?

EDIT:

For example:

((5d8999f...))
$ git rebase develop

(detached HEAD|REBASE 1/3)
$ git reset --hard

(detached HEAD|REBASE 1/3)
$ git checkout HEAD -- .

(detached HEAD|REBASE 1/3)
$ git status
rebase in progress; onto 41e6808
You are currently rebasing.
  (all conflicts fixed: run "git rebase --continue")

I know that I can do git rebase --abort, but if there is no rebase going on that will cause an error, which I am not in the mood of dealing with. Also, since this will be an automatic script I do not want to find out that there may be other states that I may not have accounted for.

I need one foolproof way to revert my working directory to a pristine state so that I can do any command I want without failure.

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40

1 Answers1

0

is completely gone from my working directory.

git clean -Xfd
git clean -xfd

 # you need to use the 2 different X cases:
git clean –Xfd [Remove only files ignored by Git]
git clean –xfd [removing all untracked files]
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • This is unsatisfactory. I want to remove any rebases/merges/reverts - not untracked files. – Joseph K. Strauss Jul 07 '16 at 20:10
  • Ok, so simply remove the "dirty" branch and checkout the origin branch, it will clear your branch content. another way id to use `git reflog`, read about it here: http://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location-detached-head/34519716#34519716 – CodeWizard Jul 07 '16 at 20:11
  • If it answer your question i will update the answer accordinally – CodeWizard Jul 07 '16 at 20:12