8 Git commands you may not know

Mehul Patel
Apr 25, 2020 · 6 min read

From programmers to IT professionals, all software developer these days is using Git, the most famous version control system around. In this blog, you’ll learn Eight (8) Git commands that you may have not used it before.

There may be possibility that 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!

Few days back GitHub went free for teams which is great news for all developers.🥳

1. git commit — amend

Mostly when committing, we hit “Enter” so quickly, and sometimes that leads a typo somewhere in the commit message. Let’s fix this.

Don’t be wonder there is a command out there, which allows for us to fix that typo or correct the commit message.

In this example, let’s say we run git commit -m “Corrected the typeo” and hit “Enter.” Immediately we realize that we mislabeled the commit message, so now we need to fix the “typeo” in the commit message..

Running the command does the magic. Now when we push our commit, our fellow team won’t see that our "typo" misspelled.

2. git stash

This command is for uncommitted changes in a file. The stash operation takes your revised tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time in the future.

Suppose we have a bunch of changes to a file but demand 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 comes into picture.

3. git reset — soft HEAD~1

The command is a complex and handy tool for undoing changes. It has three primary forms of invocation. These forms correspond to command-line arguments , let’s chat about the "soft" trailing.

Remove the last commit from the current branch, but keep the changes!

The command , is going to remove the last commit from the current branch. But the best thing about this command is that the changes won’t fade! All the changes will stay in the working tree!

In this example, we committed and then ran the command . Trying to this commit will not work, so when ran, there was nothing there to push. It certainly took our last commit and kept our changes in the corresponding file!

4. git add -p

We have often used the command or in the past. But what does do? 🤔

Ever wanted to commit just a section of a file and not the whole thing?

Well, here this command does just that. It provides 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:

  • y: stage this hunk
  • n: do not stage this hunk
  • g: select a different hunk to go to
  • a: stage this hunk and all the other hunks in this file
  • d: do not stage this hunk or any of the other hunks in this file
  • e: edit the current hunk manually
  • ?: print help

When completed with all the “hunks,” we will be able to see that our commit is ready, and we can proceed to the push!

5. git cherry-pick

Cherry-picking in Git stands for attempting some commit from one branch into another branch. In this command, we can choose a particular branch that has a commit and pull it into another. Let’s walk through this.

Suppose you are working on a project where you are making changes in a branch called new-features. You have already made a few commits but want to move just one of them into the master branch.

By using the command, , we can get that entire commit into another branch.

Now, Checkout the branch where you want to cherry-pick the specific commits. In this case master branch:

Bring a whole commit from one branch into another.

6. git checkout

This command can benefit so much when there’s a many going on in a commit, and you would rather not commit the entire thing. It’s like (which we just talked above), but rather than “cherry-picking” an entire commit, we are dipping even deeper into a branch and picking just a particular file that we want to be merged. Let’s see how magic’s done!

Do one thing, just pick a specific file from a branch and bring it into the current branch.

While sitting in the whole time, we can run the command to grab that particular committed file in that specific branch and bring it over to master to push eventually. That’s pretty clear when a specific branch’s changes are many, and we’re only looking for one file to test or push.

7. git shortlog

We mostly use the ‘git log’ which will show you all commit logs. No doubt that it will give complete details about our commit actions but what when we need short information about our activities.

Here, ‘git shortlog’ will summarize the ‘git log’ output and give you the details in short. Please refer to the following output of both operations.:

Output of ‘git log’ :

Output of ‘git shortlog’ :

8. git help

There are so numerous different git commands out there. If we were to run just git help <command>, then we would get an explanation of the command right there in our terminal.

Let’s take it a step further. 😎

Running the command git help -w <command> takes us directly to the browser that we can read up on all the things with the command in question. It will open a web page where you can read the complete details about the specific command that you have mentioned in the command.

If you get the above error, then try running `hash -r` to refresh your shell’s command hash table, so that `git` refers to /opt/local/bin/git. It will fix the error, and you will be able to browse the help commands in the browser.

Looking for common git commands? Here you go👇

Conclusion:

Git is robust version control, and I am sure most of us may use it every day. I hope these 8 secret gems help you be a little more productive in your Git commands. What’s your favorite Git command, which you recently come across? Just comment below and let us know! 🎉

Thank you for reading. :)

TechnoPanti

Absorb the future tech!

TechnoPanti

TechnoPanti is an online platform focusing on the tech industry and posts the useful out-of-the-box articles for aspirants like you and me.

Mehul Patel

Written by

Tech Speaker | Linux Engineer | DevOps | OpenSource Enthusiast | Independent Researcher | Technical Writer | Explorer

TechnoPanti

TechnoPanti is an online platform focusing on the tech industry and posts the useful out-of-the-box articles for aspirants like you and me.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store