Introduction

In this article, we will see what are the list of commonly used commands in Git. These are the commands which are really useful while working on any project. Hope this will be useful.

1) To initialize GitHub Repository in your local machine project folder:

Code
git init

2) To get the status of files:

Code
git status

3) To configure Username and Password:

Code
git config –global user.name “user-name” 
git config –global user.email “email-id”

4) To clone the repository to your local machine:

Code
git clone URL

Note: Here URL is GitHub Repository URL

5) To add a single file to GitHub:

Code
git add file-name

6) To add all the modified file to GitHub:

Code
git add .
git add -A

7) To commit the changes to GitHub:

Code
git commit -m “commit-message”

8) Get the latest code from the main branch:

Code
git pull

9) To pull the latest code from a particular branch:

Code
git pull branch-name

10) To list all the branches including local and remote:

Code
git branch -a

11) To create a new branch name:

Code
git branch branch-name

12) To delete a branch:

Code
git branch -d branch-name

13) Delete remote branch:

Code
git push origin --delete branch-name

14) To create a new branch and switch to the new branch created:

Code
git checkout -b branch-name

15) Rename branch (local):

Code
git branch -m old-branch-name new-branch-name

16) To switch a branch:

Code
git checkout branch-name

17) To discard changes of a particular file:

Code
git checkout -- file-name.txt

18) Merging branch (active branch):

Code
git merge branch-name

19) Merging to a target branch:

Code
git merge source-branch target-branch

20) Stash your changes:

Code
git stash

21) Remove stash:

Code
git stash clear

22) Show difference:

Code
git diff source-branch target-branch

23) To view the log/changes:

Code
git log

24) Detailed view of log/changes:

Code
git log --summary

25) Brief view of log/changes:

Code
git log --online

26) Get the help:

Code
git help

27) To go back to the previous commit/changes:

Code
git reset --hard

28) List stashed files:

Code
git stash list

29) Come out of stash (write working from the top of stash stack):

Code
git stash pop

30) Discard stashed changes:

Code
git stash drop

31) Going back to HEAD:

Code
git reset --soft HEAD

32) Going back to the commit before HEAD:

Code
git reset --soft HEAD^

33) Equivalent to "^":

Code
git reset --soft HEAD~1

34) Going back two commits before HEAD:

Code
git reset --soft HEAD~2

35) To see the list of available tags:

Code
git checkout v0.0.1

36) To set the current tag to v0.0.1:

Code
git tag -a v0.0.3 -m “version 0.0.3”

37) To create a new tag:

Code
git push –tags

38) To delete the file from your working directory:

Code
git rm file-name

39) To show the metadata and content changes of the specified commit:

Code
git show commit-name

40) To delete a branch:

Code
git branch -d branch-name

41) To delete a branch forcefully:

Code
git branch -D branch-name

Note: This will force deletion of the branch, even if it contains unmerged/unpushed commits.

42) Search the working directory for "add()":

Code
git grep "add()"

43) List of branches:

Code
git branch
git branch --list

44) Undo the changes:

Code
git log --oneline
git revert commit-id

45) To delete a file forcefully:

Code
git rm -rf file-name

To get the Git version:

Code
git --version

How to add a project to GitHub Repository?

Conclusion

In this article, we have seen the list of commonly used Git commands. Hope you find this helpful. In addition to these commands, there are a lot of other commands including different options. Comment below with the commands you frequently use. Thank you.