-2

Every once in a while, I use a test branch for experimentation... I can commit the experimental branch, checkout the previous branch, and then delete the experimental branch...

I am looking for a way to simply abandon the experimental branch when I am done with it... I figured just swapping branches would abandon the changes, but this actually keeps all of the new files, even after changing branches. The only way I have right now to make this work is the three-step process I listed above...

I have read the Git documentation a few times now, so I am thinking there may be a way to use the 'Rebase' command to make this work? I will appreciate getting pointed in the right direction on this one.

  • Why are you committing the changes just to delete the branch? If you don't want them, _reset_ them. – jonrsharpe Aug 16 '22 at 19:01
  • Because I don't know any better... That is why I am asking the question... It seems like git reset --hard will do exactly what I am looking for tho. – BenjaminApprill Aug 17 '22 at 02:41

2 Answers2

1

You can cancel all your change with git reset --hard Untracked files will remain you can delete them with git clean

So from an existing branch you do your experimentation then

git reset --hard

to reset all your change Then

git clean -fd

to remove untracked files and directory

You can do a dry run to tell you what files will be removed

git clean -n 

This will only list the files, to list down the folders use

git clean -nd
Ôrel
  • 7,044
  • 3
  • 27
  • 46
1

If you are abandoning without committing, then you never needed an experimental branch to start with. You've done things in the wrong order. Do it like this:

  1. Stay on the real branch.
  2. Do some initial experimentation.
  3. Decide whether to abandon or keep moving forward in the experiment.
    1. If abandon, git reset --hard etc. as described in other answer.
    2. If keep moving, git switch -c experiment; git add .; git commit and now keep working on the experimental branch.
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    If you don't understand why and when files get "left behind" when you switch branches, see my https://stackoverflow.com/a/67453215/341994 – matt Aug 16 '22 at 19:42
  • My initial branch is a base branch... The other branches stem from that one... The goal is not to keep merging everything into the main branch. The main branch operates as a template branch... The plan is to have a base branch for Unity's render pipeline, and I can branch off of this for separate games... This allows for experimentation, off of a base template of code. – BenjaminApprill Aug 17 '22 at 02:20
  • Of course, but that is not in the least germane to what I just said – matt Aug 17 '22 at 02:32
  • Oh... Now I see what you mean... I thought you meant commit to the base branch for some reason... Which would muddle it up... Now I see what you mean. I can use reset to get rid of the new stuff... Ok, sorry for that. I will have to give that a shot! I don't even remember the reset command from reading through the documents -.- – BenjaminApprill Aug 17 '22 at 02:40