Useful Git Commands

| | 2 min read

Git is a free software and distributed version control system. We can maintain and track our project files in a git repository. It is a modern version control system that replaces the legacy centralized systems like  CVS, Subversion etc.

We can call Git a 'social network' for programmers or developers, which gives speed and efficiency in development. Different commands are used to handle the operations in git.

Some useful git commands are listed below.

Modify Commits

Git is a secure version control system; every commit you create will have a unique SHA2 hash, making it difficult or nearly impossible to update once it is created. It is always possible to make changes to the commit - what it does is remove your old commit and make a new commit with the changes. Here are some useful git commands to change your git version tree.



Modify the Previous Commit Message

If you want to update the commit message of your last commit, you can use the amend option.

git commit --amend

Undo a Commit

Revert will create a new commit with the required changes to undo your last commit.

git revert HEAD

You can also use revert to undo a specific commit. It creates a new commit that removes all lines added and adds all lines removed in that commit.

git revert <commit-id>

Temporarily Stash Changes, Restore the changes later.

Think of a scenario where you are working on a new feature, and a new bug has been identified in production. You are not ready to commit yet to the new feature. Git Stash comes to your rescue here. You can temporarily save your changes, switch to any other branch, do the hot fix, and return to restore the state where you left your code.

git stash

Do some other stuff here, like switching branches, merge other changes, etc. Again re-apply the changes.

git stash pop



Tagging, Deleting, and Pushing Tags.

Create a Tag

git tag <tagName>

Delete a tag

git tag -d <tagName>

Push Tags

git push --tags


It is possible to search the git logs, a handy command to find a commit from the information written in the logs.

grep log --grep="your search word"

Git Grep can be used to grep through your code.

grep grep "your search word"

Merging new branches to the current branch

git merge <newbranch>

Graphical interface to a local repository

Gitk is a useful utility to browse through your git repository using a GUI.


GIT Branch

Delete branch local branch

Deleting a branch will not remove any commits. You safely remove a branch and create it from the commit you want as required.

git branch -D <branchName>

Delete remote branch.

git push origin --delete <branchName>

Search branch by keyword

git branch -a | grep <keyword>

Check for differences

To view the difference between the two branches

git diff branch_1..branch_2

To View changes to a file relative to the index

git diff <path/to/modified/file/name>

If you use git as your version control system in development, this will help you. You may also look into other solved issues related to Git over here.

Please get in touch to know more.