skip to content

Search

Syspirit
EN

Git

Essential Git commands and workflow!

Git is a distributed version control system that allows you to track and control source code changes.

Basic configuration

ActionCommand
Set usernamegit config --global user.name "John Doe"
Set emailgit config --global user.email "john@mail"
View configurationgit config --list
Initialize a repositorygit init
Clone a remote repositorygit clone https://github.com/user/repo.git

File management

ActionCommand
View file statusgit status
Add a filegit add <file>
Add all filesgit add .
Remove from staginggit reset <file>
Delete a filegit rm <file>
View differencesgit diff
View staged filesgit diff --cached

Commits and history

ActionCommand
Commit with messagegit commit -m "Message"
Add + commit at oncegit commit -am "Message"
Amend last commitgit commit --amend
View historygit log
Condensed historygit log --oneline
Graphical historygit log --graph --oneline
View specific commitgit show <commit-id>

Branch management

ActionCommand
List branchesgit branch
Create a branchgit branch <branch-name>
Switch branchgit checkout <branch-name>
Create and switchgit checkout -b <branch-name>
Switch (modern)git switch <branch-name>
Create and switch (modern)git switch -c <branch-name>
Delete a branchgit branch -d <branch-name>
Merge a branchgit merge <branch-name>

Remote repositories

ActionCommand
List remotesgit remote -v
Add a remotegit remote add origin <url>
Push to remotegit push origin <branch>
Pull from remotegit pull origin <branch>
Download without merginggit fetch origin
Push tagsgit push --tags
Push new branchgit push -u origin <branch>

Troubleshooting and rollbacks

ActionCommand
Undo modificationsgit checkout -- <file>
Revert to last commitgit reset --hard HEAD
Revert to specific commitgit reset --hard <commit-id>
Save temporarilygit stash
Restore stashgit stash pop
List stashesgit stash list
Search in historygit log --grep="keyword"
Clean filesgit 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)