Verzování s Gitem
Git Version Control Guide
This guide covers essential Git commands, workflows, and best practices for effective version control in software development projects.
Git Configuration
Local Configuration
# View local configuration
git config --local --list
git config --local user.email
git config --local user.name
# Set local configuration
git config --local user.name "Ludek Kvapil"
git config --local user.email "kvapilludek@gmail.com"
Global Configuration
# Set global configuration
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default editor
git config --global core.editor "vim"
# Enable colored output
git config --global color.ui true
Repository Operations
Initialize Repository
# Initialize new repository
git init
# Initialize bare repository (for shared repositories)
git init --bare
Clone Repository
# Clone via SSH
git clone ssh://user@host:/path/to/repo
# Clone specific branch
git clone -b branch-name https://github.com/user/repo.git
# Shallow clone (limited history)
git clone --depth 1 https://github.com/user/repo.git
Basic Workflow
Staging and Committing
# Check status
git status
# Add files to staging
git add file.txt
git add . # Add all files
git add -A # Add all files including deleted
# Commit changes
git commit -m "Commit message"
git commit -a -m "Commit all tracked files"
# Amend last commit
git commit --amend
git commit --amend --author="Author Name <email@example.com>"
Viewing History
# View commit history
git log
git log --oneline
git log --graph --decorate
# View specific commit
git show commit-hash
git cat-file -p commit-hash
# View differences
git diff
git diff --staged
git diff HEAD~1
Branching
Branch Operations
# List branches
git branch
git branch -a # Include remote branches
git branch -v # Show last commit
# Create branch
git branch new-branch
git checkout -b new-branch # Create and switch
# Switch branches
git checkout branch-name
git switch branch-name # Git 2.23+
# Delete branch
git branch -d branch-name # Safe delete
git branch -D branch-name # Force delete
Remote Branches
# Push new branch
git push -u origin new-branch
git push --set-upstream origin new-branch
# Delete remote branch
git push origin --delete branch-name
Merging and Rebasing
Merge
# Merge branch into current
git merge branch-name
# Merge with strategy
git merge -X theirs branch-name # Prefer their changes
git merge -X ours branch-name # Prefer our changes
# Abort merge
git merge --abort
Rebase
# Rebase current branch onto another
git rebase target-branch
# Interactive rebase
git rebase -i HEAD~3
# Rebase with strategy
git rebase -X theirs target-branch
# Advanced rebase (move commits)
git rebase --onto <new-base> <old-base> <branch>
git rebase --onto baad48ff6211cf9411b748a92162bc72cdefe1cd^ baad48ff6211cf9411b748a92162bc72cdefe1cd HEAD
Remote Operations
Remote Management
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin new-url
# Remove remote
git remote remove origin
Pushing and Pulling
# Push commits
git push
git push origin branch-name
# Pull changes
git pull
git pull origin branch-name
# Fetch without merge
git fetch
git fetch origin
Tagging
Tag Operations
# Create lightweight tag
git tag tag-name
# Create annotated tag
git tag -a tag-name -m "Tag message"
# List tags
git tag
git tag -l "v1.*"
# Push tags
git push origin tag-name
git push origin --tags
# Delete tag
git tag -d tag-name
git push origin --delete tag-name
Advanced Operations
Stashing
# Stash changes
git stash
git stash save "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply
git stash apply stash@{1}
# Drop stash
git stash drop
git stash pop # Apply and drop
Reset and Revert
# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (unstage changes)
git reset HEAD~1
git reset --mixed HEAD~1
# Hard reset (discard changes)
git reset --hard HEAD~1
# Revert commit
git revert commit-hash
Cherry Picking
# Apply specific commit to current branch
git cherry-pick commit-hash
git cherry-pick commit-hash1 commit-hash2
# Cherry pick with edit
git cherry-pick -e commit-hash
Git Flow Workflow
Branch Naming Convention
main/master: Production-ready codedevelop: Integration branchfeature/*: New featuresrelease/*: Release preparationhotfix/*: Production bug fixes
Workflow Commands
# Start feature
git checkout -b feature/new-feature develop
# Finish feature
git checkout develop
git merge --no-ff feature/new-feature
git branch -d feature/new-feature
git push origin develop
# Start release
git checkout -b release/1.0 develop
# Finish release
git checkout main
git merge --no-ff release/1.0
git tag -a 1.0
git checkout develop
git merge --no-ff release/1.0
git branch -d release/1.0
Troubleshooting
Common Issues
Merge Conflicts:
# During merge conflict
git status
# Edit conflicted files
git add resolved-file
git commit
Detached HEAD:
# Create branch from detached HEAD
git checkout -b temp-branch
git checkout main
git merge temp-branch
Lost Commits:
# Find lost commits
git reflog
git checkout commit-hash
Performance Optimization
# Clean repository
git gc
git gc --aggressive
# Repack repository
git repack -a -d --depth=250 --window=250
Git Hooks
Pre-commit Hook Example
#!/bin/sh
# .git/hooks/pre-commit
# Run tests
npm test
# Lint code
npm run lint
# Exit with error if tests fail
if [ $? -ne 0 ]; then
echo "Tests failed. Commit aborted."
exit 1
fi
Post-commit Hook Example
#!/bin/sh
# .git/hooks/post-commit
# Send notification
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"New commit pushed"}' \
https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK
Best Practices
- Commit Often: Make small, focused commits
- Write Good Commit Messages: Use imperative mood, be descriptive
- Use Branches: Keep main branch clean, use feature branches
- Pull Regularly: Stay up-to-date with remote changes
- Review Before Push: Test changes before pushing
- Use .gitignore: Exclude unnecessary files
- Learn Rebasing: Keep clean, linear history
- Backup Regularly: Don’t rely solely on local repositories
Git Configuration Files
.gitignore Examples
# Dependencies
node_modules/
vendor/
# OS generated files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/
# Logs
*.log
logs/
# Environment files
.env
.env.local
.gitattributes
# Normalize line endings
* text=auto
# Mark certain files as binary
*.jpg binary
*.png binary
*.pdf binary
# Language-specific settings
*.php diff=php
This guide provides comprehensive coverage of Git operations, from basic commands to advanced workflows and best practices for effective version control.