Partitioning Project Variations in Git Branches

Ole Ersoy
3 min readMay 9, 2022
Image by katja from Pixabay

Scenario

We have three different variations of a project managed with Git that we want to share while keeping the file contents of each variation unique and specific to that variation.

Just to keep this scenario simple we will say that the first variation is a markdown file mark1.md containing.

Project Variation 1

The second project variation will contain both mark1.md and another file mark2.md .

The third variation will contain only a third file mark3.md . Each of these variation scenarios will have be stored in their own git branch.

Approach

First we will create a new project directory ~/Temp/variations and initialize it as a git project.

cd ~/Temp
mkdir variations
cd variations
git init

Variation 1 Branch

Create a new branch called variation1.

Temp/variations $ git checkout -b variation1Switched to a new branch 'variation1'

Now we can create the mark1.md file and commit it.

touch mark1.md
// Add the content to mark1.md
// Project Variation 1
git add .
git commit -m "Adding mark1.md to the variation1 branch"

Variation 2 Branch

Create the variation2 branch.

Temp/variations $ git checkout -b variation2Switched to a new branch 'variation1'

This branch will contain the mark1.md file, as it is copied over from the version history.

Create mark2.md and initialize its contents.

touch mark2.md
// Add the mark2 contents
// Project Variation 2

Commit the change.

git add .
git commit -m "Adding mark2.md to the variation2 branch"

The variation2 branch now has both mark1.md and mark2.md in it.

Variation 3 Branch

Create the variation3 branch.

Temp/variations $ git checkout -b variation3Switched to a new branch 'variation3'

If we run git ls-files on this branch we will see that it contains the first two files we created.

Temp/variations $ git ls-files
mark1.md
mark2.md

We want this branch to only have the file mark3.md in it, so first we will delete the files already in it and then add the new file.

Temp/variations $ git rm -r .
rm 'mark1.md'
rm 'mark2.md'

And now the branch is empty.

Temp/variations $ git ls-filesTemp/variations $

Lets add mark3.md along with contents.

touch mark3.md
// Add the mark2 contents
// Project Variation 3

Commit the change.

git add .
Temp/variations $ git commit -m "Adding mark3.md to the variation3 branch"

The console lets us know that the changes have been made.

[variation3 878e3c3] Adding mark3.md to the variation3 branch
2 files changed, 2 deletions(-)
delete mode 100644 mark1.md
delete mode 100644 mark2.md

Summary

We now have a git project with 3 branches containing our file and content variations.

Our peers can move between the project branches like this:

Switch to Variation 1

Temp/variations $ git checkout variation1Switched to branch 'variation1'

Switch to Variation 2

Temp/variations $ git checkout variation2Switched to branch 'variation2'

Switch to Variation 3

Temp/variations $ git checkout variation3Switched to branch 'variation3'

--

--