Skip to content

Instantly share code, notes, and snippets.

@gbroques
Last active November 14, 2020 20:25
Show Gist options
  • Save gbroques/850cc34fea5e0b6bfbdbfcf54346034f to your computer and use it in GitHub Desktop.
Save gbroques/850cc34fea5e0b6bfbdbfcf54346034f to your computer and use it in GitHub Desktop.
Lesser known Git tips and tricks.

Git Tips

Git Grep

Print lines matching a pattern.

Reference: https://git-scm.com/docs/git-grep

git grep -n "foo"

-n, --line-number show line numbers

Git Log

Reference: https://www.atlassian.com/git/tutorials/git-log

Graph

Draw a text-based graphical representation of the commit history on the left hand side of the output.

git log --graph

Commonly combined with --oneline.

By Range

master..feature range contains all of the commits that are in the feature branch, but aren’t in the master branch.

In other words, how far feature has progressed since it forked off of master.

git log master..feature

Switching the order of the range (feature..master), shows all of the commits in master, but not in feature.

git log `feature..master`

If git log outputs commits for both versions, then this means the history has diverged.

By Message

Show all commit with log messages matching a given pattern:

git log --grep="message"

By File

Show all commits related to a particular file:

git log -- foo.py

By Content

Show all commits that introduced a particular string:

git log -S "Hello World"

Show all commits that introduced a particular pattern:

git log -G "Hello [0-9]+"

Git Notes

Reference: https://git-scm.com/docs/git-notes

Add additional meta-data to commits without changing the commit object.

Git Bisect

Use binary search to find the commit that introduced a bug.

References:

Git Merge-Base

Find common ancestors.

Reference: https://git-scm.com/docs/git-merge-base

Fork Point

Find the point at which a branch forked from another branch.

git merge-base --fork-point master branch

Git Instaweb

Git web interface (web frontend to Git repositories).

References::

git instaweb --httpd=webrick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment