Wojciech Morawiec Wojciech Morawiec - 9 days ago 4
Git Question

Correct workflow with git for comparing different solutions/implementations

Recently I decided to brush up my Python skills and started doing the Project Euler problems; additionally, I'd like to get used to using git as a solo user during this project.

Here is the thing I am trying to achieve with git:

In my

project_euler
folder I have the Python files called
001.py
and so on, so just the problem numbers. Those files that are being 'worked on' are in the main folder and I commit them as soon as I change something significant (but not necessarily solve the problem).

There are two sub-folders,
working_solutions
and
optimized_solutions
. I copy the files as soon as they are working, i.e. give the correct solution, into the former - after that I'd like to have the chance to give my implementation some thoughts and then eventually copy an optimized solution into the latter.

Now I want to keep and compare different implementations of a solution without having to rename the files
001_algorithm1.py
,
001_algorithm2.py
and so on - it just seems somehow wrong to me (after all, git should take care of that stuff).

On the other hand, commiting one version, trying something else and then commiting the second version seems to be the git way - but that means that whenever I want to compare timings, for example, I have to switch between the two versions via reverting and commiting since I don't have both (or all) different implementations available locally.




Is this setting the correct one for forking, but only single files?

Does the folder structure mentioned above make sense when using git?

Should I just rename the files appropriately,
add
them and commit them?

Answer

How about using git branches? You could name the branches based on the pattern $problemNumber-$algoNumber for example 001_algo1, 001-algo2 etc and then when you want to compare two approaches, you would do the following:

git diff 001_algo1..001_algo2 -- optimized_solutions/001.py

So the steps would be:

# Let's assume that you are on master branch and that optimized_solution does not have any solutions for problem 001
# (This creates and switches to that branch and is a combination of git branch 001_algo1 and git checkout 001_algo1)
git checkout -b 001_algo1

# Let's assume you have made your changes to the uber 001.py containing the solutions for problem 001 using algo1
git add optimized_solutions/001.py
git commit -m "Solutions for Prob 001 using algo1"

# Now switch to master and create the branch for the next algo
git checkout master
git checkout -b 001_algo2

# Make changes and commit
git add optimized_solutions/001.py
git commit -m "Solutions for Prob 001 using algo2"

# And finally when you want to compare
git diff 001_algo1..001_algo2 -- optimized_solutions/001.py

Update: Executing two versions of a file as a python script

Just checked that the following works (on OSX at least) :

some-git-repo - [master] » time python ./ex1.py
Hello World!
./ex1.py  0.01s user 0.01s system 87% cpu 0.019 total

some-git-repo - [master] » git show 001_algo2:ex1.py >! temp.py && time python ./temp.py
Hello World in a branch!!
python ./temp.py  0.01s user 0.01s system 87% cpu 0.020 total

(Note that in the second command you need the >! only if you are using zsh which, by default, is set to not clobber existing files)

Comments