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.
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.
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
This one comes in handy when you accidentally commit something with a typo or misleading commit message.
git commit -v --amend
-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.
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 -f -d
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.
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.
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
--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.
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!
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
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!
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
All basic commands you need to know to run git .Basic Git Commands you need to Master
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...
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.
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.