GIt
The most popular version control system, for tracking changes to code.
Concepts
- Repository: Project folder
- Commit: Saved snapshot of changes
- Branch: Independent line of development
- Main branch: Holds the official project history. Usually
mainormaster. - Remote: Copy of repo on another machine / server
- Stash: Temporary storage for uncommitted changes
- Tag: Named reference to a specific commit
- Cherry-pick: Apply a specific commit to another branch
Initial Setup
git config --global user.name 'Madusha Prasanjith'
git config --global user.email 'hi@madusha.me'
Managing Repositories
# Initialize a new repo
git init
# Clone existing
git clone <repo-url>
# Add Remote
git remote add <remote-name> <remote-url>
Managing Branches
# List all branches
git branch
# Create a branch
git branch <branch-name>
# Checkout a branch
git checkout -b <branch-name> # Create + checkout branch
git checkout <branch-name> # Checkout existing branch
# Delete a branch
git branch -d <feature-name> # Delete locally (only works if branch merged)
git branch -D <feature-name> # Delete locally (force delete unmerged branch)
git push origin --delete <feature-name> # Delete remotely
Managing Changes
# List changes
git status
# Show unstaged changes
git diff
# Add a file or folder
git add <files>
# Add all changes
git add -A
Creating Commits
# Add a file or folder
git commit -m "Descriptive message"
# Add current changes to the last commit
git commit --amend
Merging Branches
Git merging combines changes from two different branches into a single branch.
Types:
- Fast-forward merge: When target branch is behind source branch
- Three-way merge: When both branches have diverged, requiring a merge commit
Both are handled through the same commands:
# 1. Checkout destination branch
git checkout main
# 2. Merge source branch to destination
git merge <feature-name>
Handling conflicts explained below in Handling Merge Conflicts.
Syncing with Remote
# Fetch remote branch
git fetch <remote> <branch-name>
git fetch <remote> # Fetch all remote branches
# Fetch current branch from remote and merge it with local copy
git pull <remote>
# Push to remote
git push # Push to already setup remote branch
git push -u <remote-name> <feature-name> # Setup a remote branch and push
git push <remote-name> --delete <feature-name> # Delete remote
# CAUTION
# replaces remote changes with your local changes.
# This is destructive and remove all remote changes you don't have locally!
git push --force <remote> <branch>
# A more safer force option, which will abort if there are changes in remote which you don't have locally.
git push --force-with-lease <remote> <branch>
Stashing
# Stash changes
git stash
# Apply stash
git stash pop # Apply and remove
git stash apply # Apply and keep
# List all stashes
git stash list
# Remove a specific stash
git stash drop stash@{n}
Tagging
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Push tag to remote
git push origin v1.0.0
# Deleting tags
git tag -d v1.0.0 # Delete local tag
git push origin --delete v1.0.0 # Delete remote tag
Undoing changes
# Remove staged file but keep changes
git reset <file>
# Revert a commit
git revert <commit>
Cherry-picking
# Cherry-pick a commit
git cherry-pick <commit-hash> # Cherry-pick and commit
git cherry-pick -n <commit-hash> # Cherry-pick and stage without committing
# Abort cherry-pick
git cherry-pick --abort