GitHub CLI Setup

Install GitHub CLI (gh)

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo snap install gh

# macOS
brew install gh

# Windows
winget install GitHub.cli

GitHub CLI Authentication

1
2
3
4
5
# Login to GitHub
gh auth login

# View authentication status
gh auth status

Git Configuration

User Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Set username and email for local repository
git config user.name "Your Name"
git config user.email "your.email@example.com"

# Set global username and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# View current configuration
git config --list

Credential Management

1
2
3
4
5
6
7
8
9
# Disable git credential prompt
export GIT_TERMINAL_PROMPT=0  # Linux/macOS
set GIT_TERMINAL_PROMPT=0     # Windows

# Store credentials (cached)
git config --global credential.helper cache

# Store credentials (permanent)
git config --global credential.helper store

Basic Git Commands

Repository Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Initialize new repository
git init

# Clone repository
git clone <repository-url>

# Add remote repository
git remote add origin <repository-url>

# View remote repositories
git remote -v

Daily Workflow Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Check status
git status

# Add files
git add <file>          # Add specific file
git add .               # Add all changes

# Commit changes
git commit -m "message" # Commit with message
git commit -am "message" # Add and commit tracked files

# Push and Pull
git push origin <branch>
git pull origin <branch>

Branch Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 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

# Switch branches
git checkout <branch-name>
git switch <branch-name>       # Git 2.23+

# Delete branch
git branch -d <branch-name>    # Local
git push origin --delete <branch-name>  # Remote

History and Differences

1
2
3
4
5
6
7
8
9
# View history
git log
git log --oneline      # Compact view
git log --graph        # Graphical view

# View changes
git diff              # Working directory vs staging
git diff --staged     # Staging vs last commit
git diff <commit1> <commit2>  # Between commits

Undo Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Discard changes in working directory
git restore <file>
git checkout -- <file>  # Old syntax

# Unstage files
git restore --staged <file>
git reset HEAD <file>   # Old syntax

# Modify last commit
git commit --amend

# Reset to specific commit
git reset --soft HEAD~1  # Keep changes staged
git reset --hard HEAD~1  # Discard changes

Stash Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Save changes temporarily
git stash

# List stashes
git stash list

# Apply stashed changes
git stash apply        # Keep stash
git stash pop         # Remove stash after applying

# Remove stash
git stash drop        # Remove latest stash
git stash clear       # Remove all stashes

Advanced Git Operations

Merging and Rebasing

1
2
3
4
5
6
7
8
# Merge branch
git merge <branch-name>

# Rebase branch
git rebase <branch-name>

# Interactive rebase
git rebase -i HEAD~3  # Rebase last 3 commits

Tags

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Create tag
git tag <tag-name>
git tag -a <tag-name> -m "message"  # Annotated tag

# List tags
git tag

# Push tags
git push origin <tag-name>
git push origin --tags  # Push all tags

Submodules

1
2
3
4
5
6
7
8
9
# Add submodule
git submodule add <repository-url>

# Initialize submodules
git submodule init
git submodule update

# Update all submodules
git submodule update --remote

Git Best Practices

  1. Write clear, descriptive commit messages
  2. Commit early and often
  3. Use branches for new features
  4. Keep your repository clean
  5. Don’t commit sensitive information
  6. Regularly pull changes from upstream
  7. Review changes before committing