Git Commands You Should Know
Git commands you should know, using the latest Git commands (as of Git 2.23+). These commands cover the essential tasks for 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 "your.email@example.com"Check your configuration:
git config --listRepository Basics
Initialize a new repository:
git initClone an existing repository:
git clone <repository-url>Checking Repository Status
Check the status of your repository:
git statusWorking 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 statusCommit staged changes:
git commit -m "Your commit message"Amend the last commit (e.g., to fix the message):
git commit --amendBranching
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 branchDelete 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 --abortWorking with Remote Repositories
Add a remote repository:
git remote add origin <repository-url>Push an existing repository to Github
git remote add origin git@github.com:username/repository.git
git branch -M main
git push -u origin mainList remote repositories:
git remote -vPush 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 originViewing History
View commit history:
git logView a simplified commit history:
git log --onelineView changes in the working directory:
git diffView changes in staged files:
git diff --stagedUndoing 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~1Undo the last commit (discard changes):
git reset --hard HEAD~1Revert 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 tagCleaning Up
Remove untracked files:
git clean -fRemove untracked files and directories:
git clean -fdCollaboration
Rebase your branch onto another branch:
git rebase <branch-name>Stash your changes:
git stashApply stashed changes:
git stash applyList stashes:
git stash listDeleting 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>