As I’ve been learning to program, Git has mostly been a means to an end. For the uninitiated, Git is a version control system used by developers to improve workflow.
There are a core group of commands I use to organize, track, save, and publish my work, but I’ve always known there’s much more that Git has to offer. I thought it would be nice to take a closer look at some of the commands beyond
git push, and
git pull, and I will go over some of the most interesting ones I came across in this blog post.
You may be familiar with
git status, which shows you any files in your work that contain changes you made locally that have not yet been staged for commit. For example:
This shows me that my
main.css file contains changes that have not yet been staged for commit. However, let’s say I ran
git status and saw the name of a large file that I didn’t remember altering, and I didn’t feel like combing through the file to find the change. With
git diff, I can see the actual changes that were made, like in the example below:
There are plenty of cases where this would be more detail than is necessary and
git status will suffice, but I was excited to find out this extra level of detail is available as an option.
I’ve usually found myself on either of extreme of “I want to keep all of these changes” or “thank god I’m on a separate branch because I need to scrap all of this,” but there’s a very helpful option that splits the difference in the form of
Let’s say you make some changes that cause you to realize there’s a more urgent bug to fix, and you want to address that with a clean slate but don’t want to part with the changes you’ve made. This is where
git stash comes into play. Running the command will take all the changes you have in your current tree that have not been staged for commit, and will save them somewhere else and give you a clean working tree again. Then, when you’re ready to look at them again, you can run
git stash apply and put the changes back into your code.
If you anticipate that you might have trouble merging the work you want to stash with the work you’re about to do on the clean tree, you can create a new branch with
git stash branch <new branchname>.
git grep essentially lets you search your entire codebase for a phrase from the command line, and then it will print the lines that contain the phrase and highlight which files contain it. Here’s what it looks like in action:
I had been unfamiliar with this command due to the global search feature in Atom, but it’s helpful to know there’s a way to perform such a search directly from the command line.
I hope you found this to be a helpful look at a few Git commands you might not know. If you’re feeling inspire to continue diving deeper into Git, I encourage you to check out the following resources below.