8 Git commands you may not know

Mehul Patel
6 min readApr 25, 2020

--

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 git commit --amend -m "Corrected the typo" 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 git stash comes into picture.

3. git reset — soft HEAD~1

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

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

The command git reset —soft HEAD~1, 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 git reset —soft HEAD~1. Trying to push this commit will not work, so when git push origin master 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 git add <file> or git add . in the past. But what does git add -p 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, git cherry-pick rowdymehul-test, 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 git cherry-pick (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 master the whole time, we can run the command git checkout demo checkout.html 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.

% /usr/bin/git help -w merge
fatal: HTML documentation is not provided by this distribution of git.
%

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. :)

--

--

Mehul Patel

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