Basic git commands

This guide includes the basic git commands which are useful for all the GitHub users.

Q. How to initialize git for directory as local git repository ?

~$ git init
~$ git init <repo-name>

👉: repo-name = specify directory for local git repository

Q. How to configure git to user ?

~$ git config --global user.name <user-name>
~$ git config --global user.email <user-email>

~$ git config --local user.name <user-name>
~$ git config --local user.email <user-email>

👉: flag {global} for all repositories

👉: flag {local} for specific repository

👉: user-name = specify user name

👉: user-email = specify user email address

Q. How to show tracking of files ?

~$ git status

Q. How to clone git repository from remote server ?

~$ git clone <repo-url>

👉: repo-url = remote repository url

Q. How to staged changes in files (i.e. ready to commit) ?

~$ git add <file-name>
~$ git add .

👉: file-name = specify file name or use "." to specify all files

Q. How to commit staged changes ?

~$ git -commit -m "message"

👉: message = use present tenses recommended

Q. How to create main branch ?

~$ git branch -M main
~$ git remote add origin <repo-url>

👉: repo-url = specify repository url to create main branch

Q. How to push commits to main branch ?

~$ git push -u origin main

Q. How to create new branch ?

~$ git branch -b <branch-name>

👉: branch-name = specify name of branch

Q. How to push commits to specific branch ?

~$ git push -u origin <branch-name>

Q. How to change branch (i.e. hopping between branches) ?

~$ git checkout <branch-name>

Q. How to check all git commits which are not in local branch ?

~$ git fetch origin

Q. How to pull all changes to local branch ?

~$ git pull origin <branch-name>

Q. How to initialize and checkout branch ?

~$ git checkout -b <branch-name>

Q. How to merge another branch into current branch ?

~$ git merge <branch-name>

Q. How to show all conflicts between branches before merging them ?

~$ git diff

Q. How to mark specific commits ?

~$ git tag <commit-tag>

👉: commit-tag = specify tag to recognize commit

Q. How to undo changes ?

~$ git revert

Q. How to reset branch to last commited state ?

~$ git reset --hard HEAD

Q. How to remove file from current repository ?

~$ git rm <file-name>

Q. How to keep commit to side tray for further use ?

~$ git stash

Q. How to use side tray commit into current branch ?

~$ git stash pop

✍️ Author - SWAPNIL MALI

GitHub Profile

👨🏻‍💻CS Engineer, AWS & DevOps Specialist -🎯focused on building reliable, observable, and scalable systems.