Sam Johnston - Senior Software Engineer

Sam Johnston

Senior Software Engineer
AWS, NodeJS, React

Thursday, 12 August 2021

Home » Articles » Gitfoo - git commands

Gitfoo - git commands

Gitfoo - git commands

Useful commands I need once a month

Rewriting Git History

Additional git command alias for your .gitconfig file

[alias]
  co = checkout
  ci = commit
  cm = commit
  st = status
  unstage = reset HEAD
  undo = reset HEAD~1
  staged = diff --cached
  unstaged = diff

  # Simplified git logs
  lol = log --pretty=oneline --abbrev-commit --graph --decorate
  lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
  # Usage: git track origin/feature-123-login-form
  track = checkout -t
  praise = blame
  rename = branch -m
  # Delete all branches except master/main
  cleanup = ! git branch | grep -v "master" | grep -v "main" | xargs git branch -D

Completely rewrite the commit history of a branch by renaming or rewording the commits, whilst keeping their contents intact (if you want them).

# get the SHA of the commit you want to revert to
git lol

git rebase COMMIT_SHA -i
# edit the commits and complete the rebase
push -f

Remove the last commit from history

Remove the very last commit, and its contents from a given branch

# get the SHA of the commit you want to revert to
git lol

git reset --hard COMMIT_SHA
push -f

Restore a file to its original state

git checkout -- package-lock.json

Cherry-pick a commit into your branch

# get the SHA of the commit you want to copy
git lol

git cherry-pick COMMIT_SHA