Skip to main content
โšก Calmops

Git Usage: Complete Guide to Version Control

Duplicating a Repository

To duplicate a repository without forking it, you can run a special clone command, then mirror-push to the new repository.

# Clone with mirror flag
git clone --mirror https://github.com/exampleuser/old-repository.git
cd old-repository.git

# Push mirror to new repository
git push --mirror https://github.com/exampleuser/new-repository.git

This creates a full copy, including all branches and history.

Basic Git Commands

Initialize a Repository

# Initialize in current directory
git init

# Initialize with specific branch
git init -b main

Clone a Repository

# Clone standard repository
git clone https://github.com/user/repo.git

# Clone to specific folder
git clone https://github.com/user/repo.git my-folder

# Clone with shallow history
git clone --depth 1 https://github.com/user/repo.git

# Clone specific branch
git clone -b develop https://github.com/user/repo.git

Check Status

# View working tree status
git status

# Short format
git status -s

# View staging area
git status --short

Add Files

# Add specific file
git add file.txt

# Add all changes
git add .

# Add by pattern
git add *.js
git add src/

# Add interactive
git add -i

# Add portions of files
git add -p

Commit Changes

# Commit with message
git commit -m "Commit message"

# Add and commit in one
git commit -am "Message"

# Amend last commit
git commit --amend

# Amend with new message
git commit --amend -m "New message"

View History

# View commit log
git log

# Compact format
git log --oneline

# Show differences
git log -p

# View specific file history
git log --follow filename

# Statistics
git log --stat

# Graph
git log --graph --oneline --all

Branching

Create and Switch Branches

# Create branch
git branch new-branch

# Switch branch
git checkout new-branch

# Or use switch (Git 2.23+)
git switch new-branch

# Create and switch in one
git checkout -b new-branch
git switch -c new-branch

List Branches

# Local branches
git branch

# Remote branches
git branch -r

# All branches
git branch -a

# Show last commit on each branch
git branch -v

Merge Branches

# Switch to main branch
git checkout main
# or git switch main

# Merge feature branch
git merge feature-branch

# Merge without fast-forward
git merge --no-ff feature-branch

# Squash merge
git merge --squash feature-branch

Delete Branches

# Delete local branch
git branch -d branch-name

# Force delete
git branch -D branch-name

# Delete remote branch
git push origin --delete branch-name

# Delete merged branches
git branch --merged main | xargs git branch -d

Remote Repositories

Add Remote

# Add origin remote
git remote add origin https://github.com/user/repo.git

# Add upstream
git remote add upstream https://github.com/original/repo.git

# View remotes
git remote -v

Push Changes

# Push to origin
git push origin main

# Push to specific branch
git push origin feature-branch

# Set upstream branch
git push -u origin feature-branch

# Force push (use carefully)
git push --force origin main

Pull Changes

# Pull from origin
git pull origin main

# Pull with rebase
git pull --rebase origin main

# Fetch and merge
git fetch origin
git merge origin/main

Fetch Updates

# Fetch from origin
git fetch origin

# Fetch all remotes
git fetch --all

# Prune deleted remote branches
git fetch --prune

Stashing Changes

# Stash changes
git stash

# Stash with message
git stash push -m "Work in progress"

# List stashes
git stash list

# Apply latest stash
git stash pop

# Apply specific stash
git stash apply stash@{0}

# Drop stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Undoing Changes

Reset

# Unstage files
git reset HEAD file.txt

# Reset to previous commit (keep changes)
git reset --soft HEAD~1

# Reset to previous commit (discard changes)
git reset --hard HEAD~1

# Reset to specific commit
git reset --hard abc123

Revert

# Revert specific commit
git revert abc123

# Revert without auto-commit
git revert -n abc123

Checkout Files

# Discard changes in file
git checkout -- file.txt

# Checkout from specific commit
git checkout abc123 -- file.txt

Rebasing

Basic Rebase

# Rebase onto main
git rebase main

# Interactive rebase
git rebase -i HEAD~5

# Continue after resolving conflicts
git rebase --continue

# Skip problematic commit
git rebase --skip

# Abort rebase
git rebase --abort

Useful Rebase Operations

# Rebase with autosquash
git rebase -i --autosquash main

# Preserve merges
git rebase -p main

Working with Tags

# Create annotated tag
git tag -a v1.0.0 -m "Release 1.0.0"

# Create lightweight tag
git tag v1.0.0

# Push tags
git push origin v1.0.0
git push origin --tags

# Delete local tag
git tag -d v1.0.0

# Delete remote tag
git push origin --delete v1.0.0

Ignoring Files

Create .gitignore:

# Comments
node_modules/
dist/
*.log
.env
.DS_Store
*.swp

Advanced Ignore

# Ignore tracked file temporarily
git update-index --assume-unchanged filename

# Stop ignoring
git update-index --no-assume-unchanged filename

Advanced Tips

Collaboration Workflow

  1. Fork the repository
  2. Clone your fork
  3. Add upstream remote
  4. Create feature branch
  5. Make changes
  6. Keep fork updated
  7. Create pull request

Useful Aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

Bisect for Bug Finding

# Start bisect
git bisect start

# Mark current commit as bad
git bisect bad

# Mark known good commit
git bisect good v1.0.0

# When found, end bisect
git bisect reset

Best Practices

  • Commit often with clear, descriptive messages
  • Use branches for features and bug fixes
  • Pull before pushing to avoid conflicts
  • Review changes before committing
  • Write good commit messages: First line < 50 chars, detailed description after blank line
  • Use .gitignore to exclude build artifacts and dependencies
  • Don’t commit secrets - Use environment variables or .gitignore
  • Learn rebase vs merge - Rebase for clean history, merge for preserving context

References

Comments