Home > OS >  Abandon current Git branch, without committing
Abandon current Git branch, without committing

Time:08-17

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.

CodePudding user response:

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.

CodePudding user response:

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
  •  Tags:  
  • git
  • Related