Git Advanced

Git: Cheat Sheet (advanced)

 Originally published at maxpou.fr Updated on  ・3 min read

If you find git confusing, I created this little cheat sheet! Please, note that I voluntary skipped the basic commands like git commitgit pull/push… This cheat sheet is intended for an “advanced” usage of git.

Git Cheat Sheet

🧭 Navigation – Go to the previous branch

git checkout -

🔍 Get the history

# Log in one line
git log --oneline

# Retrieve all commits by message
# Here all commit that contain 'homepage'
git log --all --grep='homepage'

# Retrieve all commit by author
git log --author="Maxence"

🙈Ooops #1: I reseted an unwanted commit. How to rollback?

# Get everything you did
git reflog

# then reset to the desired commit (i.e. HEAD@{4})
git reset HEAD@{4}
# ...or...
git reset --hard <commit-sha1>

🤦‍♀️Ooops #2: I mixed-up with my local repo. How to clean it?

git fetch origin
git checkout master
git reset --hard origin/master
# You're now up-to-date with master!

🕵🏻‍♂️Difference between my branch and master

git diff master..my-branch

✔ Custom commits

# Edit last commit
git commit --amend -m "A better message"

# Add something to the last commit without writing message again
git add . && git commit --amend --no-edit

# empty commit - can be useful to re-trigger CI build...
git commit --allow-empty -m "chore: re-trigger build"

If you don’t know what to put in your commit messages, I wrote a post about conventional commits.

♻️ Squash commits

Let say I want to rebase the last 3 commits:

  1. git rebase -i HEAD~3
  2. Leave the first “pick” and replace the rest by “squash” (or “s“)
  3. Tidy up the commit message and save (:wq in vi).

Was this article helpful?

Related Articles

Leave A Comment?