Forcing arbitrary content deployment to the git branch

One of the popular options to deploy something nowadays is git-based deployment (Github pages is a good example). Here’s a recipe to push some content to the remote branch ignoring whatever this branch already has:

1
2
3
4
5
6
7
8
9
10
11
mkdir deploy
cd deploy
echo `date` > 1.txt
git init
git add .
git commit -m "autocommit"
git push -u https://github.com/loki2302/git-force-push-experiment.git \
master:deployed --force
cd ..
rm -rf ./deploy
echo DONE

In this snippet:

  • The directory deploy is created.
  • Within that directory, a current time is echoed to 1.txt.
  • Then, a brand new git repository is created, the entire folder contents are added and committed to the local master.
  • After it comes a trick: we take the local master and push it to the remove deployed branch.

See the demo repository here.