7 Git Tricks to Save You Time
From searching for commits by keyword to listing forgotten aliases, and more
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
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
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
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 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!