From basic to advanced professional usage
A comprehensive guide to Git commands for developers of all skill levels. Master everything from basic version control to advanced professional workflows.
git init
Initialize a new Git repository in the current directory
git clone <url>
Clone an existing repository from a remote URL
git clone --depth 1 <url>
Shallow clone (only recent history, saves space)
git config --global user.name "Your Name"
Set your global username for commits
git config --global user.email "your@email.com"
Set your global email for commits
git config --global core.editor "code --wait"
Set VS Code as your default Git editor
git config --list
List all current Git configurations
git remote add origin <url>
Add a new remote repository
git remote -v
List all remote repositories with URLs
git remote set-url origin <new-url>
Change the URL of a remote repository
git remote show origin
Show detailed information about a remote
git branch
List all local branches
git branch <branch-name>
Create a new branch
git checkout <branch-name>
Switch to a branch
git checkout -b <branch-name>
Create and switch to a new branch
git branch -d <branch-name>
Delete a branch (safe)
git branch -D <branch-name>
Force delete a branch (even if not merged)
git branch -m <new-name>
Rename the current branch
git branch --set-upstream-to=origin/<branch>
Set upstream branch for tracking
git merge <branch-name>
Merge a branch into the current branch
git merge --no-ff <branch-name>
Merge and always create a merge commit
git rebase <branch-name>
Rebase current branch onto another branch
git rebase -i HEAD~5
Interactive rebase of last 5 commits
git status
Show the working tree status
git add <file>
Add a file to the staging area
git add .
Add all changes to the staging area
git commit -m "message"
Commit staged changes with a message
git log
Show commit history
git log --oneline --graph --all
Compact history with branch visualization
git log --since="2023-01-01"
Show commits since a specific date
git log -p <file>
Show change history for a specific file
git restore <file>
Discard changes in working directory
git restore --staged <file>
Unstage a file while keeping changes
git commit --amend
Modify the most recent commit
git revert <commit>
Create a new commit that undoes a previous commit
git fetch
Download objects and refs from remote
git pull
Fetch from and integrate with remote branch
git pull --rebase
Pull with rebase instead of merge
git push
Push local commits to remote
git push -u origin <branch>
Push and set upstream tracking
git push --force-with-lease
Force push safely (won't overwrite others' work)
git push origin --delete <branch>
Delete a remote branch
git fetch --prune
Remove remote-tracking references that no longer exist
git cherry-pick <commit>
Apply a specific commit to current branch
git stash
Temporarily stash changes
git stash pop
Apply stashed changes and remove from stash
git bisect start
Binary search to find which commit introduced a bug
git blame <file>
Show who last modified each line of a file
git show <commit>
Show various types of objects (commits, tags, etc.)
git diff
Show changes between commits, commit and working tree, etc.
git reflog
Show a log of where HEAD and branch references have been
git submodule add <url> <path>
Add a submodule to the repository
git submodule update --init --recursive
Initialize and update submodules
git subtree add --prefix=<dir> <repo> <ref>
Add a subtree to the repository
git subtree pull --prefix=<dir> <repo> <ref>
Update a subtree with latest changes
git gc
Cleanup unnecessary files and optimize repository
git fsck
Verify the integrity of the Git file system
git repack -ad
Repack repository for better performance
git filter-repo --path <file> --invert-paths
Remove sensitive data from history (requires git-filter-repo)
git init
Initialize a new Git repository in the current directory
git clone <url>
Clone an existing repository from a remote URL
git clone --depth 1 <url>
Shallow clone (only recent history, saves space)
git config --global user.name "Your Name"
Set your global username for commits
git config --global user.email "your@email.com"
Set your global email for commits
git config --global core.editor "code --wait"
Set VS Code as your default Git editor
git config --list
List all current Git configurations
git remote add origin <url>
Add a new remote repository
git remote -v
List all remote repositories with URLs
git remote set-url origin <new-url>
Change the URL of a remote repository
git remote show origin
Show detailed information about a remote
git branch
List all local branches
git branch <branch-name>
Create a new branch
git checkout <branch-name>
Switch to a branch
git checkout -b <branch-name>
Create and switch to a new branch
git branch -d <branch-name>
Delete a branch (safe)
git branch -D <branch-name>
Force delete a branch (even if not merged)
git branch -m <new-name>
Rename the current branch
git branch --set-upstream-to=origin/<branch>
Set upstream branch for tracking
git merge <branch-name>
Merge a branch into the current branch
git merge --no-ff <branch-name>
Merge and always create a merge commit
git rebase <branch-name>
Rebase current branch onto another branch
git rebase -i HEAD~5
Interactive rebase of last 5 commits
git status
Show the working tree status
git add <file>
Add a file to the staging area
git add .
Add all changes to the staging area
git commit -m "message"
Commit staged changes with a message
git log
Show commit history
git log --oneline --graph --all
Compact history with branch visualization
git log --since="2023-01-01"
Show commits since a specific date
git log -p <file>
Show change history for a specific file
git restore <file>
Discard changes in working directory
git restore --staged <file>
Unstage a file while keeping changes
git commit --amend
Modify the most recent commit
git revert <commit>
Create a new commit that undoes a previous commit
git fetch
Download objects and refs from remote
git pull
Fetch from and integrate with remote branch
git pull --rebase
Pull with rebase instead of merge
git push
Push local commits to remote
git push -u origin <branch>
Push and set upstream tracking
git push --force-with-lease
Force push safely (won't overwrite others' work)
git push origin --delete <branch>
Delete a remote branch
git fetch --prune
Remove remote-tracking references that no longer exist
git cherry-pick <commit>
Apply a specific commit to current branch
git stash
Temporarily stash changes
git stash pop
Apply stashed changes and remove from stash
git bisect start
Binary search to find which commit introduced a bug
git blame <file>
Show who last modified each line of a file
git show <commit>
Show various types of objects (commits, tags, etc.)
git diff
Show changes between commits, commit and working tree, etc.
git reflog
Show a log of where HEAD and branch references have been
git submodule add <url> <path>
Add a submodule to the repository
git submodule update --init --recursive
Initialize and update submodules
git subtree add --prefix=<dir> <repo> <ref>
Add a subtree to the repository
git subtree pull --prefix=<dir> <repo> <ref>
Update a subtree with latest changes
git gc
Cleanup unnecessary files and optimize repository
git fsck
Verify the integrity of the Git file system
git repack -ad
Repack repository for better performance
git filter-repo --path <file> --invert-paths
Remove sensitive data from history (requires git-filter-repo)
Create aliases for frequently used Git commands to save time. For example:
git config --global alias.co checkout lets you use git co instead of git checkout.
Always use git push --force-with-lease instead of --force.
It will fail if the remote has new commits that you haven't fetched,
preventing you from overwriting your teammates' work.
Create aliases for frequently used Git commands to save time. For example:
git config --global alias.co checkout lets you use git co instead of git checkout.
Always use git push --force-with-lease instead of --force.
It will fail if the remote has new commits that you haven't fetched,
preventing you from overwriting your teammates' work.