Git Commands
Search Results
Git Commands
Basics
git init
Initialize a new Git repository.
git init my_project
git clone <repo_url>
Clone a remote repository.
git clone https://github.com/user/repo.git
git status
Show the current status of the working directory.
git status
git add <file>
Add changes to the staging area.
git add README.md
git commit -m "message"
Commit staged changes with a message.
git commit -m "Initial commit"
Staging
git restore --staged <file>
Remove file from staging area.
git restore --staged README.md
git restore <file>
Discard changes in working directory.
git restore config.js
git reset <file>
Remove file from staging area (legacy).
git reset HEAD file.txt
Branching
git branch
List all branches.
git branch --all
git checkout <branch_name>
Switch to a different branch.
git checkout develop
git checkout -b <branch_name>
Create and switch to a new branch.
git checkout -b feature/new-feature
git merge <branch_name>
Merge a branch into the current branch.
git merge feature/new-feature
git branch -d <branch_name>
Delete a branch.
git branch -d feature/old-feature
git switch <branch>
Switch to a different branch (modern alternative to checkout).
git switch develop
git stash
Temporarily save uncommitted changes.
git stash push -m "work in progress"
git stash pop
Restore and remove most recent stashed changes.
git stash pop
Remote
git remote -v
Show remote repositories.
git remote -v
git push origin <branch_name>
Push changes to a remote repository.
git push origin main
git pull origin <branch_name>
Pull changes from a remote repository.
git pull origin main
git fetch
Fetch changes from remote without merging.
git fetch origin
git branch -u origin/<branch>
Set upstream tracking for current branch.
git branch -u origin/main
git fetch --prune
Fetch changes and remove stale remote branches.
git fetch --prune origin
Analysis
git show <commit>
Display details and changes of a specific commit.
git show abc123
git blame <file>
Show who last modified each line of a file.
git blame src/main.js
git tag
List all tags in the repository.
git tag --sort=-version:refname
git tag <name>
Create a new tag at current commit.
git tag v1.0.0
History
git log
Show commit history.
git log --oneline
git diff
Show changes between commits or working directory.
git diff HEAD~1
git reset <commit>
Reset to a specific commit.
git reset --hard HEAD~1
git revert <commit>
Revert a specific commit.
git revert abc123