7 Git Tricks to Save You Time

From searching for commits by keyword to listing forgotten aliases, and more

W3docs
W3docs
Feb 24 · 3 min read
Image for post
Image for post
Photo by Wayne Low on Unsplash

Over the past few years, Git has become one of the most widespread version control systems and the best friend of programmers. Among other VCSs, Git is by far the most popular one, providing a variety of tools to manage projects, from small sizes to huge codebases.

Git is about more than just creating branches and pushing changes. The basic commands followed by various arguments can create magical things, saving your time and nerves. There are always great things to learn and improve your workflow. Possessing the right knowledge, you can master Git and become a great expert!

Here are a few superb tricks suggested by Git you might not know. Explore, try, and share them with colleagues.

1. Rewrite Last Commit Message

The git commit — amend is a handy tip that comes to the rescue for modifying the most recent git commit. The best part is that it combines staged changes with the previous commit instead of creating a brand new commit. You can edit the previous commit message without changing the snapshot:

git commit — amend

2. Search For Commits by Keywords

How do you find the exact piece of code you’re searching for? The git log command will find it for you. Followed by the -S option, it lists the commits based on the given string:

git log -S”config.menuItems”

3. View Log for a Specific Date Range

It seems the git log command can do much more to make your work easier. Another useful feature the command recommends is showing what changed in a Git Repository on a specified date. Assuming you want to see what changes were made between December 20, 2019, and January 20th, 2020, simply attach the — since and — until flags and voilà:

git log — since=’DEC 20 2019' — until=’JAN 20 2020'

4. View the Log Without Merge Commits

Though the git merge command is a crucial part of your workflow. The merge commits annoy every time. For this reason, you can isolate them by using this option:

git log — oneline — no-merges

5. List Forgotten Aliases

A git alias is configured to simplify the work of developers. It demands fewer keystrokes to run a command. However, sometimes you might alias commands and forget them. As direct git alias command doesn’t exist, but is created through the git config command and the git configuration files.

Here’s how you can list git aliases you have forgotten:

git config -l | grep alias | sed ‘s/^alias\.//g’

6. Undo Shared History

One of the golden rules that exist in Git is that you should never modify a history that is already shared. Changing a shared history can lead to huge problems, as other team members are not aware of the changes. However, you can use the git revert command to undo a pushed commit. This will create a new git commit that reverts the changes of the specific commit, but not the commits coming after it:

git revert <commit hash>

7. Stash Your Changes

Surely there are times when you are implementing a feature, and your working directory is messed up with changes. To stash your changes, you can simply create a new branch and commit your changes to that branch. Yet, there is another très magnifique solution, namely git stash. This command takes both uncommitted staged and unstaged changes, saves them for further use, and cleans your working directory and staging area. Run git stash, and give your directory a tidy look:

git stash

Git provides lots of unrevealed features that are must-knows for every programmer, to speed up work and get things done right. With Git, almost everything is possible. The command-option combination can do magical things, and this is peace of mind. Check out more git commands and snippets, and get into the Git world to explore new ideas for yourself!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store