Git

Git

Essential Git commands for version control, branching, and collaboration.

37 commands
Windows MacOS Linux
#version-control #devops #Popular

Setup & Config

Set global username
git config --global user.name "Your Name"
Set global email
git config --global user.email "[email protected]"
List all configuration settings
git config --list
Initialize a new Git repository
git init
Clone a remote repository
git clone <url>

Basic Operations

Show working tree status
git status
Stage a specific file
git add <file>
Stage all changes
git add .
Commit staged changes with message
git commit -m "message"
Remove file from working tree and index
git rm <file>
Rename or move a file
git mv <old> <new>

Branching

List local branches
git branch
Create a new branch
git branch <name>
Switch to a branch
git checkout <branch>
Create and switch to a new branch
git checkout -b <name>
Merge a branch into current branch
git merge <branch>
Delete a merged branch
git branch -d <name>

Remote Operations

List remote repositories with URLs
git remote -v
Add a remote repository
git remote add origin <url>
Download objects from remote
git fetch origin
Fetch and merge from remote branch
git pull origin main
Push commits to remote branch
git push origin main
Push and set upstream tracking
git push -u origin <branch>

Stashing

Stash current working directory changes
git stash
List all stashed changes
git stash list
Apply and remove most recent stash
git stash pop
Apply a specific stash without removing
git stash apply stash@{0}
Remove a specific stash entry
git stash drop stash@{0}

History & Diff

Show compact commit history
git log --oneline
Show branch history as graph
git log --graph --all
Show unstaged changes
git diff
Show staged changes
git diff --staged
Show who changed each line
git blame <file>
Show details of a specific commit
git show <commit>

Quick Commands

Show working tree status
git status
Commit staged changes with a message
git commit -m "message"
Push commits to remote main branch
git push origin main