10 Useful Git Commands

Today, we will go through some useful git commands you should know as a developer.

Let us get started.

Changing Editor in git

You can change your editor on git with the command below:

Assuming you have Notepad++ installed in your system -

git config –global core.editor “c:/program files/Notepad++/nodepad++.exe”

You can give a path for your .exe bundle.

Revert Changes in git

git reset, git checkout & git revert can be used to revert the changes in your repository

Whereas git reset and git checkout can be done on both; i.e., individual files and commit, but git revert can only be done on commit.

Suppose you are working in your local environment and have some changes committed, but not yet pushed to remote. Then, you can use git reset and git checkout. However, in case you have changes pushed to remote then you have git revert

Let us have some examples:

Git Checkout
It is best for local undo operations. It does not mess up the remote branch hampering collaborators.

Use - git checkout [commit]

Git Reset
It discards staged and unstaged changes from the most recent commit. In the below example HEAD specified that both staged and unstaged changes need to be discarded. If you want to discard a particular commit than you can specify that commit instead of HEAD

Use - git reset –hard HEAD

Git Revert

Git revert is safe for collaborators because it does not override the history that other users might depend on. If you want to undo the changes in a commit, then you can use git revert [commit]

Creating Shortcuts in git

If you're bored typing long git syntax, you can create your own shortcuts with git bash profile.

Open your git bash editor, by default you will land on  your home directory

You can then edit your .bash_profile file and add your shortcut syntax.

It can be done as,

alias gc= ’git commit’
alias go= ’git checkout’
alias gr= ’git revert’

Add staged changes to your most recent commit

git commit –amend this command lets you add staged changes to your most recent commit. In case, nothing is staged then this command will allow you to edit that commit.

Use this command if the commit has not been integrated into the remote branch.

Creating Tags in git

Tags are the references that point to specific points in the git history. It is generally used to make a version release like v1.0.1 etc.

We can create a tag with the command git tag [tagname]. Usually, git supports two types of tags that are annotated and lightweight tags. Annotated Tags are stored as full objects in the git database. They store metadata such as tagger name, date, email.

u can use Annotated tag as,

git tag -a v1.2 This command will create a new annotated tag identified with 2

Lightweight tags create new checksum and store it in the .git/ directory of project repo.

u can use Lightweight tag as,

git tag v1.2-lw This command will create a lightweight tag identified as v1.2-lw

Storing Username & Password in git

Did you come across a scenario where you have to repeatedly keep entering git credentials for push, pull & commit? – This mostly happens when you use Tortoise Git.

You can get rid of it by the steps below,

  • Navigate to your root directory through CMD.
  • Trigger command git config credential.helper store then
  • Just pull the repo with git pull

It will ask for username and password, once given it will store the credentials and will not ask for it again.