Private Commit in Git

Gespeichert von Erik Wegner am/um Sa., 02.08.2014 - 13:19

There may be times when it is necessary to distinguish between your public and private repository. The private repository contains all commits, but the public repository must consolidate a number of private commits to a single public one.

The key is to squash commits.

Let's start with an empty directory and these commands:

git init .
echo V1 > file
git add file
git commit -m "V1"

At this point, we want to develop in a secret chamber and only publish the shiny final version.

So we create a new branch:

git branch public

And do the fine art of coding:

echo V2 > file
git commit -a -m "V2"
echo V3 > file
git commit -a -m "V3"
echo V4 > file
git commit -a -m "V4"

The working copy now looks like this:

Working copy with private commits

The goal now is to have the latest three commits appear as one single commit to the outside world. This is done with the merge command and the special option --squash:

git checkout public
git merge --squash master
git commit -m "Merged for public repo"

The result looks like this:

Working copy with private commits

The public branch now contains the commit that is the sum of the private V2..V4 commits. The last step is required to join the master with the public branch again and ease the future development:

git checkout master
git merge public

This is the final result:

  • the branch visualization
  • the diff of the merge