Git is a distributed version control system that allows you to track and control source code changes.
Basic configuration
| Action | Command |
|---|
| Set username | git config --global user.name "John Doe" |
| Set email | git config --global user.email "john@mail" |
| View configuration | git config --list |
| Initialize a repository | git init |
| Clone a remote repository | git clone https://github.com/user/repo.git |
File management
| Action | Command |
|---|
| View file status | git status |
| Add a file | git add <file> |
| Add all files | git add . |
| Remove from staging | git reset <file> |
| Delete a file | git rm <file> |
| View differences | git diff |
| View staged files | git diff --cached |
Commits and history
| Action | Command |
|---|
| Commit with message | git commit -m "Message" |
| Add + commit at once | git commit -am "Message" |
| Amend last commit | git commit --amend |
| View history | git log |
| Condensed history | git log --oneline |
| Graphical history | git log --graph --oneline |
| View specific commit | git show <commit-id> |
Branch management
| Action | Command |
|---|
| List branches | git branch |
| Create a branch | git branch <branch-name> |
| Switch branch | git checkout <branch-name> |
| Create and switch | git checkout -b <branch-name> |
| Switch (modern) | git switch <branch-name> |
| Create and switch (modern) | git switch -c <branch-name> |
| Delete a branch | git branch -d <branch-name> |
| Merge a branch | git merge <branch-name> |
Remote repositories
| Action | Command |
|---|
| List remotes | git remote -v |
| Add a remote | git remote add origin <url> |
| Push to remote | git push origin <branch> |
| Pull from remote | git pull origin <branch> |
| Download without merging | git fetch origin |
| Push tags | git push --tags |
| Push new branch | git push -u origin <branch> |
Troubleshooting and rollbacks
| Action | Command |
|---|
| Undo modifications | git checkout -- <file> |
| Revert to last commit | git reset --hard HEAD |
| Revert to specific commit | git reset --hard <commit-id> |
| Save temporarily | git stash |
| Restore stash | git stash pop |
| List stashes | git stash list |
| Search in history | git log --grep="keyword" |
| Clean files | git clean -fd |
.gitignore file
The .gitignore allows you to exclude files from versioning
Basic example
# System files
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Dependencies
node_modules/
vendor/
# Local configuration
.env
config.local.json
Useful patterns
*.ext - All files with extension
folder/ - An entire folder
!exception.ext - Exception (don’t ignore)
**/cache - Cache folder anywhere
Workflow
Simple workflow
# 1. Clone or initialize
git clone https://github.com/user/repo.git
cd repo
# 2. Create a branch
git checkout -b my-feature
# 3. Work and commit
git add .
git commit -m "Add my feature"
# 4. Push the branch
git push -u origin my-feature
# 5. Merge (after review)
git checkout main
git merge my-feature
git push origin main
File states
Working Directory → Staging Area → Repository → Remote
(modified) add (staged) commit (committed) push (remote)