Git Commands You Should Know

Here’s a list of the most important Git commands you should know, using the latest Git version (as of Git 2.23+).
These commands cover most of the day-to-day tasks you'll encounter when working with Git repositories.
Setting Up Git
Set your username:
git config --global user.name "Your Name"
Set your email:
git config --global user.email "[email protected]"
Check your configuration:
git config --list
Repository Basics
Initialize a new repository:
git init
Clone an existing repository:
git clone <repository-url>
Checking Repository Status
Check the status of your repository:
git status
Working with Files
Track a new file:
git add <file-name>
Track all changes:
git add .
Unstage a file:
git restore --staged <file-name>
Discard changes in a file (unstaged):
git restore <file-name>
Checking Repository Status
Check the status of your repository:
git status
Commit staged changes:
git commit -m "Your commit message"
Amend the last commit (e.g., to fix the message):
git commit --amend
Branching
Create and switch to a new branch:
git switch -c <branch-name>
Switch to an existing branch:
git switch <branch-name>
List all branches:
git branch
Delete a local branch:
git branch -d <branch-name>
Force delete a local branch:
git branch -D <branch-name>
Merging
Merge a branch into the current branch:
git merge <branch-name>
Abort a merge (if there are conflicts):
git merge --abort
Working with Remote Repositories
Add a remote repository:
git remote add origin <repository-url>
Push an existing repository to Github
git remote add origin [email protected]:username/repository.git
git branch -M main
git push -u origin main
List remote repositories:
git remote -v
Push changes to the remote repository:
git push origin <branch-name>
Pull changes from the remote repository:
git pull origin <branch-name>
Fetch changes from the remote repository (without merging):
git fetch origin
Viewing History
View commit history:
git log
View a simplified commit history:
git log --oneline
View changes in the working directory:
git diff
View changes in staged files:
git diff --staged
Undoing Changes
Undo unstaged changes in a file:
git restore <file-name>
Unstage changes:
git restore --staged <file-name>
Undo the last commit (keep changes):
git reset --soft HEAD~1
Undo the last commit (discard changes):
git reset --hard HEAD~1
Revert a specific commit:
git revert <commit-hash>
Tagging
Create a tag:
git tag <tag-name>
Push a tag to the remote repository:
git push origin <tag-name>
List all tags:
git tag
Cleaning Up
Remove untracked files:
git clean -f
Remove untracked files and directories:
git clean -fd
Collaboration
Rebase your branch onto another branch:
git rebase <branch-name>
Stash your changes:
git stash
Apply stashed changes:
git stash apply
List stashes:
git stash list
Deleting Remote Branches
Delete a branch from the remote repository:
git push origin --delete <branch-name>
Checking Help
Get help for a Git command:
git help <command>
#git
#coding