DEV Community

Allan MacGregor πŸ‡¨πŸ‡¦
Allan MacGregor πŸ‡¨πŸ‡¦

Posted on • Originally published at allanmacgregor.com on

Git Tips and Tricks

Recently I’ve been collecting a large collection of git shortcuts, tips and tricks. The following commands have been tested on linux and some of them will required a recent version of git.

I expect that many of you will find them useful as much as I do. So let’s get started with some simple but helpful commands to search and review your git repository history.

Show log for only a specific branch

The following command can be used if you are working on a project with a particular dirty history or a log of branches

git log --first-parent {branch_name}
Enter fullscreen mode Exit fullscreen mode

Show the log as a single line

The following command will show the commit has and the first line of the commit comment.

git log --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

A more advanced and nicer version of the command above is:

git log --graph --pretty=oneline --abbrev-commit --date=relative
Enter fullscreen mode Exit fullscreen mode

Search for a string in all commits across the entire git history

This can be really useful when looking for a particular piece of code to see when it was added/removed:

git log -S{text_to_search}
Enter fullscreen mode Exit fullscreen mode

Shows branches that are all merged in to your current branch

git branch --merged
Enter fullscreen mode Exit fullscreen mode

And in the same way we can see all the branches that haven’t yet been merged.

git branch --no-merged
Enter fullscreen mode Exit fullscreen mode

Top comments (0)