Firstly, it's worth learning about Git branches and how to use them. At the very least, you should learn how to create them and commit code to them. You can learn later how to merge, rebase and cherry-pick if required.
The problem you will have is how to manage those experiments in the long term.
Depending on what you do, you may find some of these experiments useful much later (possibly years).
Experiments may be abandoned in your project, but the main goal is to keep track of what you've learnt.
In an ideal world, you'd write a small, separate project that's a proof of concept for what you were trying to do. You may even want to explain everything in a blog/wiki associated with the project.
That should generally be your preferred option, but that may require time that you don't have.
I'm guessing that if you (or someone else) has decided to give up on that experiment, it's because there were other constraints that pushed you to work on something else as a higher priority. At that stage, spending a few hours writing nice notes might just not be an option, unfortunately.
Also, there might be some benefit in keeping the experiment in the context of the full code at the time, even if your main branch evolves into something completely different later. (At the very least, you might be able to run that old proof of concept at a later date to remember what you were trying to do, what worked and what didn't.)
The main problem is that keeping old experimental branches in the long term can become messy to manage.
There are a few things that can help:
Define a convention to name those branches. That's essential. You could a prefix like experiment_
. You could maybe even put a date in that name (e.g.experiment_20240105_feature_X
), although that date may vary later.
The main subsequent problem is that you might not want to see all of these branches in your working tree or your main remote repository.
- To address this, you can create a second remote repository where you push those experimental branches.
- That way, you avoid polluting the main remote repository used by your co-workers and yourself, but you can keep that old code. Tools like GitHub and GitLab can help there if you just want to browse those experimental branches later.
- You can also delete those experimental branches in your local Git repository.
All this said, if you're going to use experimental branches, try to have commit messages that are reasonably meaningful, more or less as if they'd been intended for your main branch. Avoid messages like "Trying something else...".
If you can spare the time, tidy up those experimental branches (e.g. squash what belongs together) before pushing them to your experimental repository.
Long-term storage of experimental branches can be useful, but you still want to give the reader (including your future self) a chance to understand what was going on (as you should with any code).
main
. However, I have never used branches before, so I am reluctant to start if there is a simpler option.” Branches are the simplest option that does exactly what you describe. – Giacomo1968 Jan 05 '24 at 21:04git checkout -b my-shiny-branch; git commit -am 'WIP: felt cute (but unneccessary) might delete later'; git push; git checkout main'
– Bakuriu Jan 06 '24 at 09:29