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 --listInitialize a repository git initClone a remote repository git clone https://github.com/user/repo.git
File management
Action Command View file status git statusAdd 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 diffView 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 --amendView history git logCondensed history git log --onelineGraphical history git log --graph --onelineView specific commit git show <commit-id>
Branch management
Action Command List branches git branchCreate 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 -vAdd 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 originPush tags git push --tagsPush new branch git push -u origin <branch>
Troubleshooting and rollbacks
Action Command Undo modifications git checkout -- <file>Revert to last commit git reset --hard HEADRevert to specific commit git reset --hard <commit-id>Save temporarily git stashRestore stash git stash popList stashes git stash listSearch 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)