Basic Uses
Table of Contents
- Setting Up Your First Repository
- Essential Git Commands
- GitHub CLI Essentials
- Common Workflows
- Best Practices
- Troubleshooting
Setting Up Your First Repository
Method 1: Clone an Existing Repository
# Using HTTPS
git clone https://github.com/username/repository.git
# Using SSH (recommended if configured)
git clone git@github.com:username/repository.git
# Using GitHub CLI
gh repo clone username/repository
# Clone into specific directory
git clone git@github.com:username/repository.git my-projectMethod 2: Create a New Repository
Via GitHub Website:
- Click the “+” icon → “New repository”
- Enter repository name
- Add description (optional)
- Choose public or private
- Initialize with README (recommended)
- Add .gitignore (select template)
- Choose a license
- Click “Create repository”
Via GitHub CLI:
# Create a new repository on GitHub
gh repo create my-project --public --clone
# With more options
gh repo create my-project \
--public \
--description "My awesome project" \
--clone \
--add-readme \
--license mit \
--gitignore PythonMethod 3: Push Existing Local Project
# Navigate to your project
cd my-existing-project
# Initialize git repository
git init
# Add all files
git add .
# Create initial commit
git commit -m "Initial commit"
# Create repository on GitHub
gh repo create my-project --source=. --public --push
# Or manually add remote and push
git remote add origin git@github.com:username/my-project.git
git branch -M main
git push -u origin mainEssential Git Commands
Basic Commands
# Check Git version
git --version
# Get help
git help <command>
git <command> --help
# Initialize repository
git init
# Clone repository
git clone <url>
# Check status
git status
# View commit history
git log
git log --oneline
git log --graph --oneline --allWorking with Changes
# Add files to staging area
git add <file>
git add . # Add all files
git add *.js # Add all JavaScript files
git add -p # Interactive staging
# Remove files from staging
git reset HEAD <file>
git restore --staged <file> # Git 2.23+
# Commit changes
git commit -m "Commit message"
git commit -am "Message" # Add and commit (tracked files only)
git commit --amend # Amend last commit
# View differences
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Changes since last commitBranching and Merging
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create branch
git branch <branch-name>
git checkout -b <branch-name> # Create and switch
git switch -c <branch-name> # Git 2.23+ (create and switch)
# Switch branches
git checkout <branch-name>
git switch <branch-name> # Git 2.23+
# Merge branch
git merge <branch-name>
# Delete branch
git branch -d <branch-name> # Safe delete
git branch -D <branch-name> # Force delete
# Rename branch
git branch -m <old-name> <new-name>Working with Remotes
# View remotes
git remote -v
# Add remote
git remote add <name> <url>
git remote add origin git@github.com:username/repo.git
# Remove remote
git remote remove <name>
# Rename remote
git remote rename <old> <new>
# Fetch changes
git fetch
git fetch origin
# Pull changes
git pull
git pull origin main
# Push changes
git push
git push origin main
git push -u origin main # Set upstream
git push --force # Force push (use with caution!)Stashing Changes
# Save changes temporarily
git stash
git stash save "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply # Apply most recent
git stash apply stash@{0} # Apply specific stash
# Apply and remove stash
git stash pop
# Remove stash
git stash drop stash@{0}
# Clear all stashes
git stash clearUndoing Changes
# Discard changes in working directory
git checkout -- <file>
git restore <file> # Git 2.23+
# Unstage files
git reset HEAD <file>
git restore --staged <file> # Git 2.23+
# Reset to previous commit (keeping changes)
git reset --soft HEAD~1
# Reset to previous commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (creates new commit)
git revert <commit-hash>Tagging
# List tags
git tag
# Create tag
git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0" # Annotated tag
# Push tags
git push origin v1.0.0
git push origin --tags # Push all tags
# Delete tag
git tag -d v1.0.0 # Local
git push origin :v1.0.0 # RemoteGitHub CLI Essentials
Repository Management
# Set default repository
gh repo set-default
# Select from list or specify:
gh repo set-default owner/repo
# View repository
gh repo view
gh repo view owner/repo
# Fork repository
gh repo fork owner/repo
# Create repository
gh repo create my-repo --public --clone
# Delete repository (use with caution!)
gh repo delete owner/repo
# Clone repository
gh repo clone owner/repo
# List repositories
gh repo list
gh repo list ownerPull Request Commands
# Create pull request
gh pr create
gh pr create --title "Feature X" --body "Description"
gh pr create --fill # Use commit messages for title/body
gh pr create --draft # Create as draft
gh pr create --assignee @me --label bug,enhancement
# List pull requests
gh pr list
gh pr list --state all
gh pr list --author @me
# View pull request
gh pr view
gh pr view 123
# Checkout pull request
gh pr checkout 123
# Merge pull request
gh pr merge 123
gh pr merge 123 --merge # Create merge commit
gh pr merge 123 --rebase # Rebase and merge
gh pr merge 123 --squash # Squash and merge
# Close pull request
gh pr close 123
# Review pull request
gh pr review 123 --approve
gh pr review 123 --request-changes
gh pr review 123 --comment
# Check pull request status
gh pr status
gh pr checks 123Issue Management
# Create issue
gh issue create
gh issue create --title "Bug report" --body "Description"
# List issues
gh issue list
gh issue list --assignee @me
gh issue list --label bug
# View issue
gh issue view 123
# Close issue
gh issue close 123
# Reopen issue
gh issue reopen 123
# Comment on issue
gh issue comment 123 --body "This is fixed"Workflow Commands
# List workflows
gh workflow list
# View workflow runs
gh run list
gh run view
# Watch workflow run
gh run watch
# Download artifacts
gh run download
# Trigger workflow
gh workflow run <workflow-name>Gist Management
# Create gist
gh gist create file.txt
gh gist create --public file.txt
# List gists
gh gist list
# View gist
gh gist view <id>
# Edit gist
gh gist edit <id>Common Workflows
Daily Development Workflow
# 1. Start your day - sync with remote
git pull origin main
# 2. Create feature branch
git checkout -b feature/new-feature
# 3. Make changes and commit
git add .
git commit -m "Add new feature"
# 4. Push to remote
git push -u origin feature/new-feature
# 5. Create pull request
gh pr create --fill
# 6. After PR is merged, clean up
git checkout main
git pull origin main
git branch -d feature/new-featureFixing Merge Conflicts
# 1. Pull latest changes
git pull origin main
# 2. If conflicts occur, Git will notify you
# 3. Open conflicted files and resolve manually
# Look for conflict markers:
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> branch-name
# 4. After resolving, add the files
git add <resolved-files>
# 5. Complete the merge
git commit -m "Resolve merge conflicts"
# 6. Push changes
git push origin <branch>Updating Fork
# 1. Add upstream remote (one time)
git remote add upstream https://github.com/original-owner/repo.git
# 2. Fetch upstream changes
git fetch upstream
# 3. Checkout main branch
git checkout main
# 4. Merge upstream changes
git merge upstream/main
# 5. Push to your fork
git push origin main
# Using GitHub CLI
gh repo sync owner/repo -b mainSquashing Commits
# Interactive rebase for last 3 commits
git rebase -i HEAD~3
# In the editor:
# Change 'pick' to 'squash' for commits to combine
# Save and close
# Force push (if already pushed)
git push --force-with-lease origin <branch>Cherry-picking Commits
# Apply specific commit to current branch
git cherry-pick <commit-hash>
# Cherry-pick multiple commits
git cherry-pick <hash1> <hash2> <hash3>
# Cherry-pick range
git cherry-pick <oldest-hash>^..<newest-hash>Best Practices
Commit Messages
The Seven Rules of Great Commit Messages:
- Separate subject from body with blank line
- Limit subject line to 50 characters
- Capitalize the subject line
- Don’t end subject line with period
- Use imperative mood (“Add feature” not “Added feature”)
- Wrap body at 72 characters
- Explain what and why, not how
Example:
Add user authentication feature
Implement OAuth 2.0 authentication using GitHub as provider.
This allows users to sign in with their GitHub credentials
instead of creating separate accounts.
Resolves: #123
See also: #456, #789
Branch Naming Conventions
feature/add-login-page
bugfix/fix-navigation-menu
hotfix/security-patch
release/v2.0.0
docs/update-readme
test/add-unit-tests
refactor/optimize-database.gitignore Best Practices
Create a .gitignore file in your repository root:
# macOS
.DS_Store
.AppleDouble
.LSOverride
# IDE
.vscode/
.idea/
*.swp
*.swo
# Dependencies
node_modules/
vendor/
.env
# Build outputs
dist/
build/
*.log
# Sensitive data
*.pem
*.key
.env.local
config/secrets.ymlSecurity Best Practices
Never commit sensitive data:
- Passwords, API keys, tokens
- Private keys or certificates
- Database credentials
- .env files with secrets
If you accidentally commit secrets:
# Remove from history (requires force push) git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch path/to/file" \ --prune-empty --tag-name-filter cat -- --all # Or use BFG Repo-Cleaner (easier) brew install bfg bfg --delete-files file-with-secrets.txtUse GitHub’s security features:
- Enable Dependabot alerts
- Enable secret scanning
- Use protected branches
- Require PR reviews
Collaboration Best Practices
- Always work in branches - Never commit directly to main
- Keep PRs small - Easier to review and less likely to have conflicts
- Write descriptive PR descriptions - Include what, why, and how
- Review others’ code - Learn and help maintain quality
- Update documentation - Keep README and docs current
- Test before pushing - Run tests locally first
- Communicate - Use issues and PR comments effectively
Troubleshooting
Common Issues and Solutions
1. Permission Denied (SSH)
# Check SSH key is added
ssh-add -l
# Add SSH key
ssh-add ~/.ssh/id_ed25519
# Test connection
ssh -T git@github.com2. Failed to Push (Non-fast-forward)
# Pull first, then push
git pull origin main --rebase
git push origin main
# Or force push (careful!)
git push --force-with-lease3. Accidentally Committed to Wrong Branch
# Create new branch with current commits
git branch new-branch
# Reset original branch
git reset --hard HEAD~3 # Go back 3 commits
# Switch to new branch
git checkout new-branch4. Need to Undo Last Commit
# Keep changes, undo commit
git reset --soft HEAD~1
# Discard changes completely
git reset --hard HEAD~15. Large Files Blocking Push
# Install Git LFS
brew install git-lfs
git lfs install
# Track large files
git lfs track "*.psd"
git add .gitattributes
git add large-file.psd
git commit -m "Add large file with LFS"6. Merge Conflicts in Pull Request
# Update your branch
git checkout main
git pull origin main
git checkout your-branch
git rebase main
# Resolve conflicts, then
git add .
git rebase --continue
git push --force-with-leaseQuick Reference
Git Aliases (Add to ~/.gitconfig)
[alias]
st = status
co = checkout
ci = commit
br = branch
df = diff
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
last = log -1 HEAD
unstage = reset HEAD --
amend = commit --amend
branches = branch -a
remotes = remote -v
contributors = shortlog --summary --numberedEssential Keyboard Shortcuts (VS Code Git Integration)
Cmd + Shift + P→ Git commandsCtrl + Shift + G→ Source control panelCmd + Enter→ Commit staged changesOption + Cmd + Enter→ Commit all changes
Terminal Aliases (Add to ~/.zshrc)
# Git shortcuts
alias g='git'
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'
alias gp='git push'
alias gpl='git pull'
alias gco='git checkout'
alias gb='git branch'
alias glog='git log --oneline --graph --all'
# GitHub CLI shortcuts
alias ghr='gh repo'
alias ghpr='gh pr'
alias ghi='gh issue'