List Of Commonly Used GitHub Commands

 

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:

git init
Git

2) To get the status of files:

git status
Git

3) To configure Username and Password:

git config –global user.name “FirstName LastName”
git config –global user.email “email@email.com”
Git

4) To clone the repository to your local machine:

git clone ssh://john@example.com/path/to/my-project.git 
Git

Note: Here URL is GitHub Repository URL

5) To add a single file to GitHub:

git add file-name
Git

6) To add all the modified file to GitHub:

git add .
git add -A
Git

7) To commit the changes to GitHub:

git commit -m “commit-message”
Git

8) Get the latest code from the main branch:

git pull
Git

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

git pull branch-name
Git

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

git branch -a
Git

11) To create a new branch name:

git branch branch-name
Git

12) To delete a branch:

git branch -d branch-name
Git

13) Delete remote branch:

git push origin --delete branch-name
Git

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

git checkout -b branch-name
Git

15) Rename branch (local):

git branch -m old-branch-name new-branch-name
Git

16) To switch a branch:

git checkout branch-name
Git

17) To discard changes of a particular file:

git checkout -- file-name.txt
Git

18) Merging branch (active branch):

git merge branch-name
Git

19) Merging to a target branch:

git merge source-branch target-branch
Git

20) Stash your changes:

git stash
Git

21) Remove stash:

git stash clear
Git

22) Show difference:

git diff source-branch target-branch
Git

23) To view the log/changes:

git log
Git

24) Detailed view of log/changes:

git log --summary
Git

25) Brief view of log/changes:

git log --online
Git

26) Get the help:

git help
Git

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

git reset --hard
Git

28) List stashed files:

git stash list
Git

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

git stash pop
Git

30) Discard stashed changes:

git stash drop
Git

31) Going back to HEAD:

git reset --soft HEAD
Git

32) Going back to the commit before HEAD:

git reset --soft HEAD^
Git

33) Equivalent to "^":

git reset --soft HEAD~1
Git

34) Going back two commits before HEAD:

git reset --soft HEAD~2
Git

35) To see the list of available tags:

git checkout v0.0.1
Git

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

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

37) To create a new tag:

git push –tags
Git

38) To delete the file from your working directory:

git rm file-name
Git

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

git show commit-name
Git

40) To delete a branch:

git branch -d branch-name
Git

41) To delete a branch forcefully:

git branch -D branch-name
Git

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

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

git grep "add()"
Git

43) List of branches:

git branch
git branch --list
Git

44) Undo the changes:

git log --oneline
git revert commit-id
Git

45) To delete a file forcefully:

git rm -rf file-name
Git

To get the Git version:

git --version
Git

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]
    Git
  • Now go to GitHub Repository and check your 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

Comments

Popular posts from this blog

What is Gini Impurity?

Artificial Neural Network Interview Questions

20+ Questions to Test your Skills on Logistic Regression