Git Tutorial
Introduction to Git
Git is a distributed version control system that tracks changes in source code during software development. It was created by Linus Torvalds in 2005 for development of the Linux kernel.
Key Concepts
- Repository: A directory containing your project and its version history
- Commit: A snapshot of your project at a specific point in time
- Branch: A separate line of development
- Remote: A copy of your repository hosted on a server (like GitHub)
- Clone: A copy of a remote repository on your local machine
Getting Started
Installation
# Ubuntu/Debian
sudo apt-get install git
# CentOS/RHEL
sudo yum install git
# macOS (using Homebrew)
brew install git
Initial Configuration
# Set your name and email
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"
# Check your configuration
git config --list
Creating Your First Repository
# Initialize a new repository
git init
# Or clone an existing repository
git clone <repository-url>
Basic Git Commands
Checking Status and History
# Check repository status
git status
# View commit history
git log
git log --oneline
git log --graph --oneline --all
# Show changes in working directory
git diff
git diff --staged
Making Changes
# Add files to staging area
git add <filename>
git add . # Add all files
git add *.txt # Add all .txt files
# Commit changes
git commit -m "Your commit message"
git commit -am "Add and commit tracked files"
# Remove files
git rm <filename>
git rm --cached <filename> # Remove from tracking but keep file
Undoing Changes
# Discard changes in working directory
git checkout -- <filename>
git restore <filename> # Git 2.23+
# Unstage changes
git reset HEAD <filename>
git restore --staged <filename> # Git 2.23+
# Amend last commit
git commit --amend
# Revert a commit
git revert <commit-hash>
Working with Branches
Branch Management
# List branches
git branch
git branch -a # Show all branches (local + remote)
# Create a new branch
git branch <branch-name>
git checkout -b <branch-name> # Create and switch to new branch
# Switch between branches
git checkout <branch-name>
git switch <branch-name> # Git 2.23+
# Delete a branch
git branch -d <branch-name>
git branch -D <branch-name> # Force delete
Merging and Rebasing
# Merge a branch into current branch
git merge <branch-name>
# Rebase current branch onto another
git rebase <branch-name>
# Resolve merge conflicts
# Edit conflicted files, then:
git add <resolved-files>
git commit
# Abort merge/rebase
git merge --abort
git rebase --abort
Remote Repositories
Working with Remotes
# Add a remote repository
git remote add origin <repository-url>
# List remotes
git remote -v
# Fetch changes from remote
git fetch origin
# Pull changes from remote
git pull origin <branch-name>
git pull --rebase origin <branch-name>
# Push changes to remote
git push origin <branch-name>
git push -u origin <branch-name> # Set upstream
# Clone a specific branch
git clone -b <branch-name> <repository-url>
Remote Branch Management
# Create and push a new branch
git push -u origin <new-branch-name>
# Delete remote branch
git push origin --delete <branch-name>
# Track remote branch
git checkout -b <local-branch> origin/<remote-branch>
Advanced Git Operations
Stashing
# Save changes temporarily
git stash
git stash push -m "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply
git stash pop # Apply and remove
git stash apply stash@{n} # Apply specific stash
# Drop stash
git stash drop stash@{n}
git stash clear # Remove all stashes
Tagging
# Create a lightweight tag
git tag <tag-name>
# Create an annotated tag
git tag -a <tag-name> -m "Tag message"
# List tags
git tag
git tag -l "v1.*" # List tags matching pattern
# Push tags
git push origin <tag-name>
git push origin --tags # Push all tags
# Delete tag
git tag -d <tag-name>
git push origin --delete <tag-name>
Cherry-picking and Reflog
# Apply a specific commit to current branch
git cherry-pick <commit-hash>
# View reflog (history of HEAD)
git reflog
# Reset to a previous state
git reset --hard HEAD@{n}
Git Plugin in ZSH
plugins=(
git
# ...
)
Alias | Full Command | Description |
---|---|---|
g |
git |
Main git command |
gst |
git status |
Show the working tree status |
gaa |
git add --all |
Add all new, modified, and deleted files to the staging area |
gcam |
git commit --all --message |
Commit all staged changes with a message |
gp |
git push |
Push committed changes to the remote repository |
grm |
git rm |
Remove files from the working tree and the index |
grmc |
git rm --cached |
Remove files from the index while keeping them in the working tree |
gco |
git checkout |
Switch branches or restore working tree files |
gl |
git pull |
Fetch and merge changes from the remote repository |
gcm |
git checkout main (or master ) |
Switch to the main branch |
ggpull |
git pull origin "$(git_current_branch)" |
Pull changes from the remote tracking branch of the current branch |
ohmyzsh/plugins/git at master · ohmyzsh/ohmyzsh
🙃 A delightful community-driven (with 2,400+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, pyth…