Basic git commands
This guide includes the basic git commands which are useful for all the GitHub users.
🔗 : If you want the pdf version then Download the PDF
◨◧ How to initialize git for directory as local git repository?
1
2
~$ git init
~$ git init <repo-name>
👉: repo-name = specify directory for local git repository
◨◧ How to configure git to user ?
1
2
3
4
5
~$ 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
◨◧ How to show tracking of files ?
1
~$ git status
◨◧ How to clone git repository from remote server?
1
~$ git clone <repo-url>
👉: repo-url = remote repository url
◨◧ How to staged changes in files (i.e. ready to commit) ?
1
2
~$ git add <file-name>
~$ git add .
👉: file-name = specify file name or use “.” to specify all files
◨◧ How to commit staged changes ?
1
~$ git -commit -m "message"
👉: message = use present tenses recommended
◨◧ How to create main branch ?
1
2
~$ git branch -M main
~$ git remote add origin <repo-url>
👉: repo-url = specify repository url to create main branch
◨◧ How to push commits to main branch ?
1
~$ git push -u origin main
◨◧ How to create new branch ?
1
~$ git branch -b <branch-name>
👉: branch-name = specify name of branch
◨◧ How to push commits to specific branch ?
1
~$ git push -u origin <branch-name>
◨◧ How to change branch (i.e. hopping between branches) ?
1
~$ git checkout <branch-name>
◨◧ How to check all git commits which are not in local branch ?
1
~$ git fetch origin
◨◧ How to pull all changes to local branch ?
1
~$ git pull origin <branch-name>
◨◧ How to initialize and checkout branch ?
1
~$ git checkout -b <branch-name>
◨◧ How to merge another branch into current branch ?
1
~$ git merge <branch-name>
◨◧ How to show all conflicts between branches before merging them ?
1
~$ git diff
◨◧ How to mark specific commits ?
1
~$ git tag <commit-tag>
👉: commit-tag = specify tag to recognize commit
◨◧ How to undo changes ?
1
~$ git revert
◨◧ How to reset branch to last commited state ?
1
~$ git reset --hard HEAD
◨◧ How to remove file from current repository ?
1
~$ git rm <file-name>
◨◧ How to keep commit to side tray for further use ?
1
~$ git stash
◨◧ How to use side tray commit into current branch?
1
~$ git stash pop
