HomeCheatSheetsGit Commands every Developer should know

Git Commands every Developer should know

We all developers use Git Commands on our everyday work over and over again to save, share, maintain history of our work. In this journal entry, we will see some git commands every developer should know and which are rarely used but are very helpful in making the developers life easy.

GitHubĀ recently wentĀ free for teamsĀ 

Some of these commands may be new to you or you might be aware of these commands. If you are new to these commands then its a new learning set under your belt, but if you are already aware of these commands then take this as a re-learning process.

#1 git help

git has a lot of commands under its belt. And before running any command its important to understand what that command is supposed to do our code. Running the following command git help <command> would simply display an explanation right there on the terminal.

However, you can this a step further by running the following command git help -w <command> . This command takes you directly to the website where you can read up all the things related to the command.

#2 git stash

git stash Ā temporarily stashes the changes that you’ve made to your working copy so that you can continue work on something else, and then come back and re-apply them later on.

Save uncommitted changes and stash them for later. Useful command when you want to commit and push, but aren’t ready to commit the changes you have made since the last commit.

Once you are done with the changes or work that needs to be committed first, then you can go back into the branch and run the following command git stash pop . This command will bring back all of your changes like it never left your workspace.

#3 git cherry-pick

This is a very powerful command. Using this command git cherry-pick we can pick any commit to a branch and pull it to another branch. This command is very useful for undoing a commit which accidentally was made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

Let’s try to understand this using an example. There are two branches of your code “branch-dev” and “branch-qa”. You have to commit your changes to the “branch-dev” but have accidentally committed the changes to “branch-qa”. Now you have to move this commit from “branch-qa” to “branch-dev”. Using the below command you can bring an entire commit from one branch to another.

git cherry-pick branch-qa

The above command will bring entire commit. However, if you are looking to cherry-pick a specified commit, then you can simply run the following command:

git cherry-pick <commit-id>

You can retrieve the <commit-id> by using the command git log .

#4 git commit –amend

When committing the changes we sometimes have a typo in the commit message. git commit --amend command allows us to fix the typos or mislabeled commit messages.

Lets understand this using an example:

git commit -m "there sis a typeoo"

As you can see in the above command, we have a typo in the commit message. Now lets try to fix this typo.

git commit --amend -m "there is a typo"

The above command does the magic for us. Now whenever we push this commit, other co-workers working on this repository will never see the typos that were saved earlier.

#5 git add -p

Everyone may have used the commandĀ  git add <file> Ā orĀ  git add . Ā many times. But what exactly does the commandĀ  git add -p Ā do? This command helps in committing just the section of the file and not the entire file.

Once we execute this command, this command will ask us about the hunks. “Hunks” are the chunks or parts of the file that we decide what to do with it.

This commands shows a question at the bottom of the screen “Stage this hunk [y, n, g, a, d, e, ?]?”.

The meaning of letters for your quick reference:

  • 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 we are done with all the hunks, our commit is ready and we can continue to the push.

Wrapping Up

Git is a very powerful tool and most of us use it on every day basis. We hope these simple tricks or commands help you be more productive.

Do let us know about your favorite Git commands in the comments section below?

Previous article
RELATED ARTICLES

Most Popular