Skip to content

Instantly share code, notes, and snippets.

@th3hunt
Last active August 11, 2023 08:50
Show Gist options
  • Save th3hunt/14330f3a61161eeeeddb22eb5e939854 to your computer and use it in GitHub Desktop.
Save th3hunt/14330f3a61161eeeeddb22eb5e939854 to your computer and use it in GitHub Desktop.
Pro Git - A list of Git aliases & recipes to Git like a ๐Ÿ˜Ž

Pro Git

A list of Git aliases & recipes to Git like a ๐Ÿ˜Ž.

Aliases

# ~/.git/config

[alias]
  # List all aliases :)
  la = "!git config -l | grep alias | cut -c 7-"

  # Clean oneline log
  l = log --graph --pretty=format':%C(yellow)%h%C(auto)%d%Creset %Creset%s %C(242)<%an>%Creset'

  # Oneline log with dates
  ll = log --graph --pretty=format':%C(yellow)%h%C(auto)%d%Creset %Creset%s %C(dim 11)(%ar) %C(246)<%an>%Creset'

  # Log with stats
  ls = log --pretty=format':%C(yellow)%h%C(auto)%d%Creset %Creset%s %C(242)<%an>%Creset' --stat --no-merges

  co = checkout
  go = checkout -b
  st = status
  pr = pull --rebase
  br = branch

  # current branch
  cb = rev-parse --abbrev-ref HEAD 
  # find parent branch
  pb = !"git show-branch -a | ack '\\*' | ack -v \"`git cb`\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//'" 

  # Show the last tag
  lt = describe --tags --abbrev=0
  rh = reset --hard
  filediff = diff --name-only
  last = log -1 HEAD
  fixup = commit --fixup
  undo = reset HEAD~1 --mixed

  # Show the history of a file, with diffs
  filelog = log -u

  merge-ignore-space = merge -Xignore-all-space
  unstage = reset HEAD --
  staged = diff --cached
  unstaged = diff

  track = branch -u
  unmerged = branch --no-merged
  merged = branch --merged
  # Number of commits since ref: git distance <ref>
  distance = !git rev-list $1.. --count 
  authors = shortlog -sn --no-merges
  # Search all commits that introduced or removed a string
  seek = log --source --all -S

  # Show verbose output about tags, branches or remotes
  tags = tag -l
  branches = branch -a
  remotes = remote -v

  # delete merged branches
  bclean = "!f() { git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -r git branch -d; }; f"

Bash Prompt & Autocomplete

Recipes

Clean History

How a good mainline looks like ->

* (HEAD) Merge branch feature/b
|\
| * 
| *
| *
|/
* Merge branch hotfix/foo
|\
| *  
|/
* Merge branch feature/a
|\
| * 
| *
| *
| *
|/
*
  1. Pull Rebase
  2. Rebase before merge and merge with --no-ff and --preserve-merges if needed
  3. Use git commit โ€”-fixup instead of โ€œFix the of the fix of ...โ€
  4. Use git rebase -i <branch> --autosquash to autosquash fixups
  5. Use git rebase --onto to rebase a range of commits onto a changed/different base branch

Donโ€™t resolve the same conflicts twice

Enable rerere to have Git keeping a record of all conflict resolutions and to reapply them if needed

git config --global rerere.enabled true
git config --global rerere.autoupdate true

Rerere Your Boatโ€ฆ

Binary search the bug

Been more than 1 hour and still canโ€™t find the cause of the bug? Was working 200 commits back?

git bisect start <bad commit> <good commit>
# got a script you can use to check if something works? i.e. `npm run test`
git bisect run <script>

Revert a branch

git revert -m <parent_number> <merge-commit>
git cat-file -p <merge-commit> to show the numbers of the parent commits

Undoing Merges

Multitask like a pro

git worktree add ../myproject-foobranch foobranch

Hardcore Magic

Use filter_branch to extract a subfolder of your project as a new repo while retaining all its history Shorten your ancient history into a single commit using Replace Kicker

Themes ๐ŸŽจ

iTerm

Tweaked FrontEndDelight theme for iTerm2 -> Grab it from here

Vim

Railscasts colors for Vim

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