Skip to content

Instantly share code, notes, and snippets.

@erikzaadi
Last active March 26, 2024 22:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikzaadi/81ed1b1f08b1ad44e583c6d8be66071c to your computer and use it in GitHub Desktop.
Save erikzaadi/81ed1b1f08b1ad44e583c6d8be66071c to your computer and use it in GitHub Desktop.
Drying up your CI/CD with git rebase - an elaboration of the Reversim 2024 Ignite talk
# some git aliases to help out
[alias]
# fetch and merge main/master branch without the need for checkout
fnm = "!f() { WANTED_BRANCH="${1:-master}"; echo "Fetching and merging ${WANTED_BRANCH}";git fetch origin "${WANTED_BRANCH}:${WANTED_BRANCH}"; }; f"
# same as above expect that it fetches from an upstream remote (fork scenario)
fnmu = "!f() { WANTED_BRANCH="${1:-master}"; echo "Fetching and merging ${WANTED_BRANCH}";git fetch upstream "${WANTED_BRANCH}:${WANTED_BRANCH}";git push origin ${WANTED_BRANCH}:${WANTED_BRANCH};}; f"
reset-date = commit --amend --date=now
# git rebase interactively based on branch ($1)
rbm = "!f() { WANTED_BRANCH="${1:-master}"; echo "Rebasing ${WANTED_BRANCH}";git rebase --autostash -i "${WANTED_BRANCH}"; }; f"
# Remove last branch locally and from remote
micdrop = "!f() { PREVIOUS_BRANCH=$(git rev-parse --symbolic-full-name @{-1} | sed 's/refs\\/heads\\///g'); WANTED_BRANCH="${1:-${PREVIOUS_BRANCH}}"; echo "Cleaning up ${WANTED_BRANCH} like a boss!"; git branch -d "${WANTED_BRANCH}";git push origin ":${WANTED_BRANCH}"; }; f"
# Merge with fast forward only, the ONLY way to properly do it!
mergef = merge --ff-only
# Push to a new branch on the remote and track it
pushu = !git push -u origin $(git symbolic-ref --short HEAD)
# push with force (but with lease, manners maketh pusher)
pushf = !git push origin $(git symbolic-ref --short HEAD) --force-with-lease
# checks out the main/master branch
# merges the previous branch with --ff-only (ofc)
# push changes
# and finally, deletes the merged branch.
shipit = "f() { HEAD_BRANCH=${1:-master}; CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD); git checkout ${HEAD_BRANCH}; git merge --ff-only ${CURRENT_BRANCH}; git push; git branch -d ${CURRENT_BRANCH} } f"

Drying up your CI/CD

With git rebase

Whale hello there!

So glad you got to see my talk and even MOAR that you actually wrote down the gist url / scanned the QR!

More gems are available at my dotfiles repo.

Here's the main gist:

git checkout -b my-amazong-feature
# work work work
git fetch origin main:main # change main to master if that's your origin branch
git rebase main --interactive # Squash anything that can be in one logical commit
git push --force-with-lease
# all is ready, it's shipping time!
git checkout main # or your origin branch
git merge --ff-only - # - is the previous branch
git push
# victory coffee!!1

This workflow allows you to remove repeating steps in your CI flow!

In your CI:

Did you download dependencies? Elevate your CI platform capability to cache those!

Did you build an artifact? Save that somewhere with the commit sha as a part of the naming convention!

Did you build a docker image? AMAZONG, tag it with the commit sha!

When creating your artifacts, wether files or docker images, try to make it possible to inject all runtime configurations at, ahem runtime!

In your CD:

Want to deploy that docker image? just use the git commit sha! it's the same! Tag it with something that'll allow you to identify the deploy as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment