Git Cheat Sheet

Philosophies

Conventional Commit guide: Follow the guidelines of formatting your commits, so you can generate release notes, plus makes the commit history cleaner.

Atomic Commits argue that there are 3 major problems with large sized commits:

  1. obfuscate the source of bugs
  2. difficult to revert
  3. overwhelming to manage I like the idea, however when doing refactoring, many times it's virtually impossible to do this, as many times you fix and simplify multiple interfaces at once, and there is no added value in remembering exactly which commit has been the one to blame.

Another principle is that always commit working code. It's usually either-or. For incremental feature changes, I really like the Atomic Commits way, as it's making it very clean and clear what a feature actually touches.

Some times it makes sense to separate new features to two phases:

  1. make the code READY to take the new feature (e.g. refactor interfaces, how they should be)
  2. do the actual feature It is an overhead, but this is the step that we usually save on in order to get it done fast.

Another approach can be to do it quick an dirty in a work-in-progress branch (so code is backed up in the cloud) - then when it's working, you usually have a good idea where the line is between refactoring of old code vs the feature you're adding, so just do it again on a fresh branch.

Practicalities

Seeing all the files changed, when seemingly nothing changed?

Ignore File Permission changes in Linux

git config core.fileMode false

Getting stuck with pulls?

When pulling, if you have unstaged changes:

git pull --rebase --autostash

Delete all local branches

git branch --merged | grep -v \* | xargs git branch -D 

Do not want to see line endings?

Just set them as-is.

git config --global core.autocrlf input

... or for windows:

git config --global core.autocrlf true
#### Delete GIT history of a branch

Pushed secrets to the repo? Have to delete ALL git history? This is how:

Create a new branch

git checkout --orphan latest_branch

Add all the filesHaving problem with

git add .

Commit the changes

git commit -m "commit message"

Delete the branch

git branch -D main

Rename the current branch to main

git branch -m main

Finally, force update your repository

git push -f origin main

Remember GIT credentials

git config --global credential.helper store
git config --global credential.helper cache

Get latest commit's data

git log -1 --format="%H %s %ae"

Generate customizable git diffs

https://github.com/orhun/git-cliff