In this article we are going to discuss List Of Commonly Used Git Commands. 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. Please visit my previous article List Of Frequently Used Docker Commands.
1) To initialize GitHub Repository in your local machine project folder:1
git init
2) To get the status of files:1
git status
3) To configure Username and Password:1
git config –global user.name “user-name”
git config –global user.email “email-id”
4) To clone the repository to your local machine:1
git clone URL
Note: Here URL is GitHub Repository URL
5) To add a single file to GitHub:1
git add file-name
6) To add all the modified file to GitHub:1
git add .
git add -A
7) To commit the changes to GitHub:1
git commit -m “commit-message”
8) Get the latest code from the main branch:1
git pull
9) To pull the latest code from a particular branch:1
git pull branch-name
10) To list all the branches including local and remote:1
git branch -a
11) To create a new branch name:1
git branch branch-name
12) To delete a branch:1
git branch -d branch-name
13) Delete remote branch:1
git push origin --delete branch-name
14) To create a new branch and switch to the new branch created:1
git checkout -b branch-name
15) Rename branch (local):1
git branch -m old-branch-name new-branch-name
16) To switch a branch:1
git checkout branch-name
17) To discard changes of a particular file:1
git checkout -- file-name.txt
18) Merging branch (active branch):1
git merge branch-name
19) Merging to a target branch:1
git merge source-branch target-branch
20) Stash your changes:1
git stash
21) Remove stash:1
git stash clear
22) Show difference:1
git diff source-branch target-branch
23) To view the log/changes:1
git log
24) Detailed view of log/changes:1
git log --summary
25) Brief view of log/changes:1
git log --online
26) Get the help:1
git help
27) To go back to the previous commit/changes:1
git reset --hard
28) List stashed files:1
git stash list
29) Come out of stash (write working from the top of stash stack):1
git stash pop
30) Discard stashed changes:1
git stash drop
31) Going back to HEAD:1
git reset --soft HEAD
32) Going back to the commit before HEAD:1
git reset --soft HEAD^
33) Equivalent to “^”:1
git reset --soft HEAD~1
34) Going back two commits before HEAD:1
git reset --soft HEAD~2
35) To see the list of available tags:1
git checkout v0.0.1
36) To set the current tag to v0.0.1:1
git tag -a v0.0.3 -m “version 0.0.3”
37) To create a new tag:1
git push –tags
38) To delete the file from your working directory:1
git rm file-name
39) To show the metadata and content changes of the specified commit:1
git show commit-name
40) To delete a branch:1
git branch -d branch-name
41) To delete a branch forcefully:1
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()”:1
git grep "add()"
43) List of branches:1
git branch
git branch --list
44) Undo the changes:1
git log --oneline
git revert commit-id
45) To delete a file forcefully:1
git rm -rf file-name
To get the Git version:1
git --version
Table of Contents
How to add a project to GitHub Repository?
- Create a project on your local machine.
- Create a GitHub Repository
- Open the Git Bash go to the path where the project folder exists and type these commands
git init git remote add origin https://github.com/username/repository-name.git [Note : Make sure you are in correct repository using : git remote -v] git add . git commit -m "commit-message" git push origin master --force [provide your username and password]
Conclusion
Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂
Jayant Tripathy
Coder, Blogger, YouTuberA passionate developer keep focus on learning and working on new technology.