🐙 Git Tricks 😜

Katerina Sand
CheckiO Blog
Published in
6 min readSep 4, 2020

--

Git is an amazing tool with rich open-source architecture used by almost every developer these days. But as popular and useful this version control system is, as hard it can be. You can easily make a lot of mistakes and get stuck figuring them out. Also, it’s always a good idea to have a few tricks up your sleeve to make your experience and work that much smoother.

In this article we’ve gathered some of those magical tricks. Of course, we expecting you to know git basics. Hope, you’ll find our list useful!

Aliases 🎭

I think everyone likes, when there are some things that can speed up your work. For developers, aliases are just those things. They allow you to save the commands that you use the most. You can try creating the alias for those tricks that you will consider interesting.

For example, you have liked the command git log --oneline.

git config --global alias.log1 “log --oneline”

Using the indicated above you will save that command as the alias log1 and you can use it as:

git log1

However, as we stop applying a command as frequently, as we have used to, the generated aliases tend to be forgotten. Forgotten, but never lost. J There is a way to see all those aliases by using the git config command below:

git config -l | grep alias

Make your own aliases and you will reduce the pressure on your keyboard.

Log tricks 📝

  • graph

The --graph option allows you to see your log as graphs, which demonstrate the history of the commits and their addition to the branches.

If you would like to make a nice graph in the terminal for a git repo:

git log --graph --decorate --pretty=oneline --abbrev-commit

You can make it even more interesting:

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
  • shortlog

The command git shortlog shows you the grouped commits from the logs by their authors and headers.

You want to see the statistics of changes made to your project? No problem! Git can give you the number of commits by their author. Just run:

git shortlog -sn
852 Kassandra Newland
56 Phill
6 oduvan
  • whatchanged

The command git whatchanged gives you the opportunity to see the logs of commits and the changes that they have added.

Using it you can simply and quickly see the changes that happen to your project in a specific period.

Example:

git whatchanged --since="2 weeks ago" -- path/to/file.py
  • no merges

The--no-merges option helps you to see your logs containing commits with only one pattern.

No matter how important the merge commits might be, you are able to look at the history of your project without them. So, filter them out just use the following:

git log --oneline --no-merges
  • reflog

The git reflog command administrates the data saved in the reference logs, which stores the history of changes to the references.

One of the most common way of using reflog is to see where the HEAD reference have been recently, in case you forget on which branch you were working.

git reflog show
  • fsck

The command git fsck checks the completeness of the database and its objects.

If you deleted or lost the commits (like most of us must have done once or twice 😂), you can use this great command that can help you find them.

Example:

$ git fsck --full
Checking object directories: 100% (256/256), done.
Checking objects: 100% (234/234), done.
dangling commit b1907246e8611138975c6b00af7d9430579616f0
dangling commit 9d02a4cf5e6c9166dd65ac2f661d631c202c0d11
dangling blob d0674d6ceac78697ee471d6564b9ebd296a19326

The--full tag will allow you to see all the unreachable objects from other objects. And you will see your lost commit after ‘dangling commit’.

  • grep

The grep function of the git log command will present you with specific commits that include the indicated pattern in their commit messages. This way you are able to search for a desired phrase or words across the commit messages.

Example:

git log --grep="fixes things"

You will get a list of commits that have ‘fixes things’ phrase in their messages.

Command git log -S actually searches the code in the repository. So, if you want to find something specific in your code, just indicate it. The log will provide you with the exact commits that include your keyword.

Example:

git log -S"window.alert"

You will get a list of commits that had anything to do with the string ‘window.alert’.

Tricks with commits 📤

Git – Koukia
  • undoing

Command git revert creates one more commit that reverts changes for one or more given commits:

git revert HEAD

It reverts the previous commit.

The other option here is using reset command. For example, you can undo last two changes using the following command:

git reset --soft HEAD~2

In that case 2 previous commits swill be removed, but changes of those commits will stay, so you can recommit those, or fix changes of those commits.

But If you want to delete changes together with commits use --hard option instead of --soft

  • rewriting

If you have noticed that tip or comment for the last commit needs some improvement, it’s very easy to do with git commit --amend.

  • safe overwriting

You can easily overwrite the remote branch with your local one with the git push --force command. But using the--force flag can be quite destructive. It can delete from the history all the commits that have not been fetched till that moment. So, the command does its work with no regard to the tracking branch state. You can do a lot of damage to other participates’ changes. To avoid that from happening, use a much better choice:

git push --force-with-lease

This way you will not overwrite nobody else’s code. The remote references will be updated only if your local copy is the most recent one. The remote-tracking branch should point to the same commit reference as the remote branch. You can even specify to which commit, branch or ref to compare to.

  • reuse-message

Not to commit every little thing you have done to the remote repo, you can reuse the commit message:

git commit -a --reuse-message=HEAD --reset-author

The current changes with the updated author and time stamp will be committed.

When all is done, you can also squash the commits with the same message and push to the remote.

  • squashing

When you have finished working on your branch, you may want to merge it with the main one via one commit. Go to the main branch and use the fast way of squashing commits together:

git merge --squash your_branch
  • cherry-picking

Despite having a pretty cool name, this command can help you a lot. It allows you to apply a chosen commit from one branch into another. It is very convenient, when branch, you are working on, is not merged yet into the master, but you need to merge only one commit from it now.

Wrap up 🤗

These were the Git tricks we wanted to highlight for your handily use.

What are your favorite Git tricks that we might have missed to mention here? We’d be glad to here from you!

--

--