0

I have a javascript project under git version control and I want to make some "risky" changes with a high likelyhood of breaking the application.
I understand as far as I should create an "experimental" branch and perform changes on this branch, with: (creating a new branch off the develop branch)

git checkout -b experimental develop

If it turns out the changes I perform on this branch are unusable, I understand I can always just checkout back to the develop branch, but how do I then "tidy up after myself" i.e.

  • remove the experimental changes and
  • Delete the branch
Michael Coleman
  • 3,288
  • 3
  • 19
  • 18

2 Answers2

2

Yes, this is a good approach.

Deleting a branch is pretty easy:

git branch -D experimental

If you happened to push this branch, you'll have to remove it from the remote too:

git push origin --delete experimental

Cleaning up your commits is not necessary, they'll get GC'd after a while after they become unreachable (that is, after they are removed from the reflog and after a git gc which will happen automatically sooner or later).

If you really want to remove any trace of what you did, here's the solution:

git reflog --expire=now --all
git prune
git gc

Beware this will delete your whole reflog (which is your safety net).

Community
  • 1
  • 1
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • thanks, yes I figured the changes would probably continue to exist in git system somehow, but it would be better if any redundant files/code was removed to save space – Michael Coleman Sep 21 '14 at 09:55
  • @bcmcfc thanks - I went with this answer because it covers me for a couple of different outcomes - if they crop up – Michael Coleman Sep 21 '14 at 10:16
2

You won't need to do anything specific to remove those experimental changes. That's the whole point of creating the branch and is why people say "branches are cheap". If you don't need its changes you can just delete it.

To delete an unmerged branch, which yours will be as you won't be merging it into your primary development branch, use:

git branch -D experiment

The uppercase variant of D allows you to delete a branch that isn't merged into another.

bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • thanks. So I take it this will remove the branch - and it will no longer be able to be checked out. But does this also remove the work? I guess it doesnt matter as - the "failed experiment" will no longer be able to be seen? – Michael Coleman Sep 21 '14 at 09:52
  • It would still be present in git reflog for a while and would eventually be removed as part of Git's garbage collection. If you do want to retain it, but don't want a branch, you could always tag it: `git tag experiments/tried_xyz` – bcmcfc Sep 21 '14 at 09:57