GitHub recently went free for teams which is great news for all devs. We all have our everyday Git commands that we use over and over. But what if I told you there are some lesser known Git commands that may make your life easier. In this article, we are going to go over the top 7 lesser-know Git commands. Some of these you may have used, some may be new to you, and some you might think there’s no way this is going to work. 😳 But you’ll never know until you try them out!
We may have used the command git add <file>
or git add .
many times in the past. But what does git add -p
do?
Ever wanted to commit just a section of a file and not the entire thing?
Well, we’re in luck because this command does just that. It allows for a more concise commit. Once this command runs, we’ll be asked about “hunks”. “Hunks” are the chunks of code that we’ll decide what to do with.
You’ll see the question, “Stage this hunk [y, n, g, a, d, e, ?]?” at the bottom of the terminal.
The letters mean the following things:
When finished with all the “hunks”, we will be able to see that our commit is ready and we can continue to the push!
When committing, sometimes we can hit “Enter” too quickly and have a typo somewhere in the commit message. Rest assured!
There is a command out there that allows for us to fix that typo or mislabeled commit message.
In our example, let’s say we run git commit -m "fixing a typeo"
and hit “Enter”. Quickly we realize that not only were we trying to fix a typo in the application, but now we need to fix the “typeo” in the commit message.
Running the command git commit —amend -m "fixing a typo"
does the trick. Now when our push our commit, our co-workers won’t see that our “typo” was original spelled “typeo”.
The command git reset
has a variety of things you can trail onto it, let’s chat about the “soft” trailing.
Remove the last commit from the current branch, but keep the changes!
When using the command git reset —soft HEAD~1
, it’s going to remove the last commit from the current branch. But the cool thing about this command is that the changes won’t disappear! All the changes will stay in the working tree!
In our example, we committed and then ran the command git reset —soft HEAD~1
. Trying to push
this commit will not work, so when git push origin master
was ran, there was nothing there to push. It indeed took our last commit and kept our changes in the file!
This command can help so much when there’s a lot going on in a commit and you would rather not commit the entire thing. It’s kinda like git cherry-pick
(which we’ll talk about next) but rather than “cherry-picking” an entire commit, we are diving even deeper into a branch and picking just a particular file that we want merged. Let’s see how it’s done!
Pick a specific file from a branch and bring it into the current branch.
While staying in master
the entire time, we are able to run the command git checkout kapehe-test index.html
to grab that particular committed file in that particular branch and bring it over to master to eventually push. That’s pretty neat when a particular branch’s changes are many and we’re only looking for one file to test or push.
In this command, we can pick a particular branch that has a commit and pull it into another. Let’s walk through this.
Let’s say we have a branch kapehe-test
and we have committed all of our changes. There’s another branch, we’ll just use master
but this can be any branch, and we want to pull in kapehe-test
’s commit. By using the command, git cherry-pick kapehe-test
we can get that entire commit into another branch.
Bring an entire commit from one branch into another.
This command is for uncommitted changes in a file. Let’s say we have a bunch of changes to a file but need to go back and test something out before we commit and push this or we’re just not ready to commit our changes. That’s where git stash
comes into play.
Save uncommitted changes and stash them for later. Really useful for when you want to commit and push, but aren’t ready to commit the changes since the last commit.
When we run the command git stash
it takes those saved, uncommitted changes and “stashes” them for later. We can then move around different branches, pull changes into master, etc. Once we have finished testing or doing whatever we needed to do, we can go back into our branch, run the command git stash pop
and our changes will come back! It’s like we never even left!
There are so many different git commands out there. And before we start running commands without knowing exactly what it’s going to do to our code is important. If we were to run just git help <command>
then we would get an explanation of the command right there in our terminal.
Want to take it a step further?
Running the command git help -w <command>
takes us directly to the website that we can read up on all the things with the command in question.
Git is very powerful and some of us may use it every day. I hope these 7 hidden gems help you be a little more productive in your Git commands. What’s your favorite lesser-known Git command? Let us know in the comments below! 🎉
Originally published by Kapehe at https://scotch.io
#git #github #web-development #javascript #node-js