Useful git command for developers

Git is the powerful distributed version control for developers. As of this writing in the middle of 2019 the Git is used in 70% of total repositories. The popularity of Git is increasing day by day. These days almost any developer needs a bit knowledge of Git for development work. Git is a must for all developers.

In day to day development work it is really handy to know some quick commands to do things faster. Developers got intelligent IDE these days. GIT is integrated with most of the IDEs. These IDE has nice user interface to manage GIT which is really helpful for many developers.

GIT can also be used from command line. The command line GIT requires some bash knowledge. Developer who love command line prefers to use GIT from command line. Following are some useful GIT command for developers:

Sort recently modified branch by committer date:

$ git for-each-ref --sort=committerdate refs/heads/

// Output
d7b04c07d3143e04b83dde116830656443233cb0 commit	refs/heads/MCK-133-redesign-homepage
9e6ab565fce909bd40a89f874b1815365cbbbb54 commit	refs/heads/MCK-new-plugin

The above command sort the recent changed branch. The branch: MCK-new-plugin is the last changed branch in the local repository. 

git for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname)' refs/heads refs/remotes
// Output 
2021-03-02 refs/remotes/origin/MDP-premium-top-table-list-secret-adjustment
2021-03-02 refs/heads/MDP-premium-top-table-list-secret-adjustment

Remove remote branch:

Lets say we have a branch that is merged with master and the branch is no longer needed. We can easily remove the branch from remote repository using the following command:

$ git push <remote_name> --delete <branch_name>

// Output
To https://domain.git
 - [deleted]             <branch name>

Clone a private repo:

In case we need to clone a private repo using git clone, it can be done by providing the user credential with the command like below:

$ git clone

Rename branch:

If we want to rename the branch name or adjust the branch name in git use can use the following ways:

// If we want to rename a branch but we are currently in a different branch
$ git branch -m <oldname> <newname>
// If we want to rename the current branch
$ git branch -m <newname>

Restore a deleted folder:

A directory is added to the git index and it is removed somehow, we can very easily restore it by using following command:

$ git reset -- path/to/folder
$ git checkout -- path/to/folder

Amending/Changing the most recent commit message:

If we write a commit message that has a typo or has other issues we can change that by using following command:

$ git commit --amend -m "New commit message"

Change history of a file:

View the change history of a file using Git

$ git log -p filename