Initialize a Repository
Create a new Git repository in the current directory.
git init
This creates a hidden .git directory that tracks all changes.
Clone a Repository
Download a repository from a remote URL.
git clone <url>
Example with specific directory name:
git clone https://github.com/user/repo.git my-project
Check Repository Status
View the status of files in your working directory and staging area.
git status
Short format status:
git status -s
Add Files to Staging
Stage specific files for commit.
git add <filename>
Stage all modified and new files:
git add .
Stage all files including deleted ones:
git add -A
Add only modified files, not new ones:
git add -u
Commit Changes
Save staged changes to the repository history.
git commit -m "Your commit message"
Commit with detailed message in editor:
git commit
Stage all tracked files and commit:
git commit -am "Your message"
Amend the last commit:
git commit --amend
View Commit History
Display commit history.
git log
Compact one-line format:
git log --oneline
Show last N commits:
git log -n 5
Show Changes
View changes in working directory.
git diff
View staged changes:
git diff --staged
Compare specific commits:
git diff commit1 commit2
Remove Files
Remove a file from working directory and stage the deletion.
git rm <filename>
Remove from Git but keep in working directory:
git rm --cached <filename>
Move or Rename Files
Move or rename a file and stage the change.
git mv <old-name> <new-name>
Ignore Files
Create a .gitignore file to exclude files from version control.
# Ignore node_modules directory
node_modules/
# Ignore all .log files
*.log
# Ignore specific file
config.env
View File Content at Specific Commit
Show file content from a specific commit.
git show <commit>:<filename>
Useful Aliases
Create shortcuts for common commands.
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit