← Back to Upcase

Delete experimental branch along with changes


(Jon Seidel) #1

I created an experimental branch to try thngs out. Now I’m done and just want to delete the branch and all the modifications that were made Do I use:

git br -D ExpBranch

or is there some other incantation? I know I can delete the modified files individually and also that I’ll have to manually rm any untracked files.


(Derek Prior) #2

git branch -D <branch_name> will delete the branch even if there are unmerged commits. If you have untracked files you can then do git clean -f to delete all the untracked files.

Beware that git clean -f is unrecoverable. You may be able to recover from git branch -D using git reflog, but git can’t restore files it never knew about.


(Jon Seidel) #3

@derekprior; I tried that out and learned that it does, in fact, delete the branch. However, any uncommitted changes are still left in the index. To use this approach, what I believe has to be done is:

  1. git add any changes made during the experiment
  2. git commit those changes into the branch
  3. git delete the branch as you suggested
  4. Then do the git clean -f to get rid of any untracked files.

What I was looking for is a command to do the following:

  1. Issue a git reset HEAD <file> command to unstage any files in the staging (index) area
  2. Issue a git checkout -- <file> command to ‘unmodify’ any tracked files that have been changed

Researching, I think that means doing:

git reset --hard master

if the master branch contains the state prior to initiating the experimental branch, followed by deleting the branch as you suggested and then removing any new, untracked files.