This guide explains the basic Git commands for connecting to a GitHub repository and managing your code changes.
First-time setup on your computer:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"Choose one of these methods:
# Create and enter your project folder
mkdir your-project
cd your-project
# Initialize Git
git initgit clone https://github.com/username/repository-name.gitSee which files have been changed:
git status# Add specific file
git add filename.ext
# Add multiple files
git add file1.ext file2.ext
# Add all changes
git add .# Commit with a message
git commit -m "Your descriptive commit message"
# Add and commit in one command (for tracked files only)
git commit -am "Your descriptive commit message"# First time push
git push -u origin main
# Subsequent pushes
git push# Add a remote
git remote add origin https://github.com/username/repository-name.git
# View remote repositories
git remote -v# Create and switch to a new branch
git checkout -b branch-name
# Switch to an existing branch
git checkout branch-name# See uncommitted changes
git diff
# See commit history
git log# Undo changes in a file
git checkout filename.ext
# Reset staged changes
git reset filename.ext
# Reset all changes to last commit
git reset --hard# Fetch and merge changes
git pull
# Fetch changes without merging
git fetch- Commit Often: Make small, focused commits
- Write Clear Messages: Use descriptive commit messages
- Pull Before Push: Always pull before pushing to avoid conflicts
- Use Branches: Create branches for new features
- Review Changes: Check
git statusandgit diffbefore committing
# Fix last commit message
git commit --amend -m "New message"# Undo commit but keep changes
git reset --soft HEAD~1
# Undo commit and discard changes
git reset --hard HEAD~1- Go to GitHub Settings
- Select Developer Settings
- Generate Personal Access Token
- Use token instead of password when pushing
# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# Add SSH key to GitHub account
# Copy the public key (.pub file) to GitHub settings- View command help:
git help command - View command options:
git command -h - Check Git documentation: Git Documentation
- GitHub Guides: GitHub Guides