10 tips to save time and and sanity with Git

10 tips to save time and and sanity with Git

Here are 10 Git tricks to know and save your time and sanity

In this article, we’re going to discuss the miscellaneous Git tricks you should know as a developer, data scientist, or product manager. We’ll look at inspecting, removing, and tidying with Git.

1. Checkout a single file from another branch

Have you ever destroyed a file and just wished you could have a fresh start? Or needed the changes you made in one file in another branch? This command lets you grab just one file from another branch.

git checkout some-other-branch -- yarn.lock

You can use the same trick to checkout one file from a specific commit.

git checkout 9146367 -- yarn.lock

This is an effective trick if cherry-pick would pick up other files that you don't need.

2. View the log without merge commits

Merge commits annoy some people. In fact, some people would rather never use the merge command because they are so annoyed by merge commits.

Personally, I think they are an important part of the history of a project, and you shouldn't try to circumvent them in your workflow.

That being said, if you want to look at a project's history in at a glance, you can use this flag to filter out merge commits.

git log --oneline --no-merges

3. Rewrite your last commit message

This one comes in handy when you accidentally commit something with a typo or misleading commit message.

git commit -v --amend

The -v is optional, but I like it because it shows a lot of information about the changes which helps me to write a more descriptive commit message.

4. Get rid of all untracked changes

Pretty self-explanatory, but in case you're not familiar with the idea:

If you create a new file that didn't previously exist in the git history, you've made an untracked change. To start tracking that file, you'd need to commit it to the repo.

Sometimes, you change your mind halfway through a commit and really just want to start over without all the changes you've got. Well, git checkout . will get rid of all the tracked changes you've made, but your untracked changes will still be floating around. To remedy that, we've got git clean.

git clean -f -d

5. Print out a cool visualization of your log

This one mostly just makes you look cool. It can be useful, though, to visualize all of your long standing branches.

git log --pretty=oneline --graph --decorate --all

Try it out.

6. Ask git for a changelog

If you're looking for a condensed explanation of what changed, and who changed it, you can ask git to give you something that looks a lot like a changelog.

git shortlog <commit>..HEAD

In this example <commit> should be replaced with the commit you want to target for the beginning of your log. Basically with git shortlog eafbc3c..HEAD you're saying, "Show me what changed between commit eafbc3c and right now."

The shortlog is grouped by commit author and shows the first line of each commit message. If your commit messages are well-written this should give you a solid idea of what each commit actually did.

You can do cool tricks like git shortlog HEAD~20.. to get the shortlog for the last 20 commits.

7. View the log for a specific date range

In a similar vein of thinking, you might need to see what changed in a repo between two days.

Thankfully, git has your back. The git log commit accepts --since and --until as flags.

So if I wanted to see what happened in Solidus between February 10th, 2016 and February 19th, 2016 I could run:

git log --since='FEB 10 2016' --until='FEB 19 2016'

Now, I can see that Murphy was pretty active in mid February.

8. List all git aliases

Sometimes you might alias a few commands and forget them later, or maybe there are some aliases defined by a shared config you use.

This is a trick I found somewhere, and even though it's not exclusively a feature of git, we are taking advantage of the git config command.

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

Try it out, see if you have any forgotten aliases!

9. Search for commits that include a keyword

If you know exactly what piece of code you are looking for, or exactly what keyword you need to find changes on, you can search the log by code.

This will give you a list of commits that somehow affected a line of code or text containing your search string.

git log -S"config.menu_items"

In this example, I'll find a list of commits that somehow manipulated the string config.menu_items.

10. Super secret list of git tutorials

Not that secret, but pretty super:

git help -g

Try it out and see. 🤠

git is a powerful tool, and is full of neat tricks. This list is in no way comprehensive or complete, so you should do some exploring and read the git documentation. Feel free to share any cool tricks you find on this thread, I love learning about the tools that help me write code!

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about Git

How To Use Git: A Reference Guide

Setting up Gridsome with GitLab, NetlifyCMS and Netlify

Basic Git/GitHub Cheat Sheet

Getting Started Faster with Git and GitHub

Top Popular Python open-source projects on GitHub

Build Docker Images and Host a Docker Image Repository with GitLab

5 GitHub Repositories that Every New Developer Must Follow

git github

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Git It? How to use Git and Github

Learn how to use Git and Github 🐙🐱 Contributing to open-source is satisfying on many levels. Not only does it validate your skills for recruiters, but it leads to higher quality software that empowers developers to build better products. But submitting your first pull request on GitHub can be intimidating. The goal of this guide is to give you a clear set of steps for contributing any OS project

Basic Git Commands you need to Master

All basic commands you need to know to run git .Basic Git Commands you need to Master

Git for Beginners - Git & GitHub Fundamentals In Depth

This git tutorial for beginners is designed to teach you the git command line tool and how it works with github. This beginner git tutorial teaches github fu...

Git and GitHub first steps

Hello guys , this is my second post on medium. I am taking a data science bootcamp course at awari.com.br and I intend to write about the topics that I am going to learn. First because I want to share a little of my knowledge and it’s a great way to learn as well. Today I am going to write about Git and Github , explain what they are and describe some basics commands on git.

7 Best Practices in GIT for Your Code Quality

Git plays a significant role in software development. It allows developers to work on the same code base at the same time. Check out 7 best practices for Git.There is no doubt that Git plays a significant role in software development. It allows developers to work on the same code base at the same time. Still, developers struggle for code quality.