Useful Git commands.

Useful Git commands.

Lets discuss some of the most common and useful Git commands.

Git is a version control system that lets us manage our source code. GitHub is a cloud-based hosting service that lets us manage Git repositories.

Hey readers if you want to know more about GitHub. Checkout my blog :[imsushant.hashnode.dev/github]

Below is a list of some of the most frequently used commands of Git (git bash):

  1. To initiate a folder for a new or existing project : git init

  2. To add a file to the staging area : git add <file_name>

  3. To add all the files to the staged area : git add . or git add -A

  4. To check status of the files : git status

  5. To commit changes to the head repository (local) : git commit -m "commit message"

  6. To view all the commits made : git log

  7. To view desired number of commits : git log -p -10---> Note : Here we have entered 10 but it can be your desired number.

  8. To remove a file from the staging area : git rm --cached <file_name>

  9. to create a new branch : git branch <branch_name>

  10. To create a branch and move to that branch : git checkout -b <branch_name>

  11. To switch between branches : git checkout <branch_name>

  12. To know all the branches : git branch

  13. To add a file in staged area as well as commit : git -am "commit message" or git -a -m "commit message"

  14. To merge a branch into the current branch : git merge <branch_name>

  15. To go to the last commit made : git checkout <file_name> or git checkout -- <file_name>

  16. To go to the last commit of all the files: git checkout -f

  17. To compare current file with the staged version of that file : git diff

  18. To compare staged file with the last commit version of that file : git diff --staged

  19. To remove a file from both staging area as well as working folder/directory : git rm <file_name>

  20. To check short status/ overview of a file : git status -s

  21. To delete a git repository : rm -rf .git

  22. To rename a file as well as stage the file : git mv <file_name> <new_file_name>

  23. To know all the commits in one line : git log --pretty=oneline

  24. To know all the commits in short/ comprised form : git log --pretty=short

  25. To know all the commits in long form : git log --pretty=long

  26. To know all the commits for a desired time period : git log --since=number.desired-time-period ---> Example : git log --since=2.weeks

  27. To un-stage a file : git restore --staged <file_name>

  28. To set a new name (alias) for a command : git config --global alias.<new_name> <original_name> ---> Example : git config --global alias.ct commit

  29. To discard all the changes made n a file : git restore <file_name>

  30. To connect local and remote repository (example - connect git with GitHub) : git remote add origin <url of the remote repository> ---> Note : origin is now the alias name for the remote repository link. We can mention any desired name in the place of origin.

  31. To know the remote repositories : git remote

  32. To push master branch to the remote repository (origin) : git push -u origin master

  33. To change link (url) of the origin (remote repository) : git remote set-url origin <new_link>

  34. To clone a repository from GitHub : git clone <repository clone url>

  35. To pull a branch from remote repository : git pull

  36. To push files to the master branch (remote repository) directly: git push

  37. To push a branch to the GitHub with alias name : git push <branch>:<new_branch_name> ---> Example : We have a branch name feature in our local repository and we want to push that branch with name div1 → command : git push feature:div

  38. To delete a merged branch from local repository : git branch -d <branch_name>

  39. To delete a branch which in un-merged from local repository : git branch -D <branch_name>

  40. To delete a branch from remote repository : git push -d origin <branch_name>