Skip to content

Latest commit

 

History

History
450 lines (340 loc) · 10.1 KB

File metadata and controls

450 lines (340 loc) · 10.1 KB

Connecting Local Code to Remote Repository

A complete step-by-step guide for beginners to understand and use Git with GitHub.

🎯 What is Git and GitHub?

Git

  • Git is a version control system that tracks changes in your code
  • It runs locally on your computer
  • Keeps a history of all your code changes
  • Allows you to revert to previous versions

GitHub

  • GitHub is a cloud service that hosts Git repositories
  • Stores your code online
  • Allows collaboration with others
  • Provides backup for your code

Think of it like: Git is like a diary for your code (local), and GitHub is like a library where you store copies of your diary (online).


🏗️ Basic Concepts

Repository (Repo)

  • A folder that contains your project and its Git history
  • Can be local (on your computer) or remote (on GitHub)

Branch

  • Different versions of your code
  • main or master - the primary branch
  • You can create other branches for features or experiments

Commit

  • A snapshot of your project at a specific time
  • Like saving a checkpoint in a game

Push/Pull

  • Push: Send your local changes to GitHub
  • Pull: Download changes from GitHub to your local computer

🚀 Setting Up Git (One-time Setup)

1. Install Git

Download and install Git from: https://git-scm.com/

2. Configure Git (First Time Only)

# Set your name (will appear in commits)
git config --global user.name "Your Name"

# Set your email (should match your GitHub email)
git config --global user.email "your.email@example.com"

# Check your configuration
git config --list

Example:

git config --global user.name "akashdip2001"
git config --global user.email "akashdipabc@abc.in"

📋 Scenario 1: Starting with Local Code (Your Situation)

When you have code on your computer and want to upload it to GitHub.

Step 1: Create GitHub Repository

  1. Go to https://github.com
  2. Click the "+" button → "New repository"
  3. Enter repository name (e.g., "MyProject")
  4. Choose Public or Private
  5. Don't initialize with README (since you have local code)
  6. Click "Create repository"

Step 2: Initialize Local Git Repository

# Navigate to your project folder
cd "C:\path\to\your\project"

# Initialize Git repository
git init

# Check status (see what files Git is tracking)
git status

Step 3: Create .gitignore File

# Create .gitignore file (tells Git which files to ignore)
# For Python/Django projects:
echo "# Python
__pycache__/
*.py[cod]
*.so
.Python
env/
venv/
*.log
db.sqlite3
.env
.vscode/
.idea/" > .gitignore

Step 4: Add Files to Git

# Add all files to staging area
git add .

# Check what's been added
git status

Step 5: Make First Commit

# Create first commit with a message
git commit -m "Initial commit: Add project files"

Step 6: Connect to GitHub

# Add GitHub repository as remote origin
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.git

# Verify remote was added
git remote -v

Example:

git remote add origin https://github.com/akashdip2001/ParkerHub.git

Step 7: Push to GitHub

# Push your code to GitHub
git push -u origin main

# If your default branch is master:
git push -u origin master

📋 Scenario 2: Starting with GitHub Repository (Clone)

When you want to download an existing GitHub repository to your computer.

Step 1: Clone Repository

# Navigate to where you want the project
cd "C:\Users\YourName\Desktop\Projects"

# Clone the repository
git clone https://github.com/USERNAME/REPOSITORY_NAME.git

# Enter the project folder
cd REPOSITORY_NAME

Example:

git clone https://github.com/akashdip2001/ParkerHub.git
cd ParkerHub

🔄 Daily Git Workflow

Once your repository is set up, here's your daily workflow:

1. Check Status

# See what files have changed
git status

2. Add Changes

# Add specific file
git add filename.py

# Add all changes
git add .

# Add all Python files
git add *.py

3. Commit Changes

# Commit with descriptive message
git commit -m "Add user authentication feature"

# Examples of good commit messages:
git commit -m "Fix login bug"
git commit -m "Update README with installation instructions"
git commit -m "Add contact form to website"

4. Push to GitHub

# Send changes to GitHub
git push

5. Pull Updates (if working with others)

# Get latest changes from GitHub
git pull

🛠️ Essential Git Commands Reference

Command Purpose Example
git init Initialize Git in folder git init
git status Check current status git status
git add Stage files for commit git add .
git commit Save changes locally git commit -m "message"
git push Upload to GitHub git push
git pull Download from GitHub git pull
git clone Copy repository locally git clone <url>
git branch List branches git branch
git checkout Switch branches git checkout main
git log View commit history git log --oneline

🌟 Branch Management

Create and Switch Branches

# Create new branch
git branch feature-login

# Switch to branch
git checkout feature-login

# Create and switch in one command
git checkout -b feature-login

Merge Branches

# Switch to main branch
git checkout main

# Merge feature branch into main
git merge feature-login

# Delete branch after merging
git branch -d feature-login

📝 Complete Example Walkthrough

Let's say you have a Python project called "MyWebApp":

# 1. Navigate to your project
cd "C:\Users\akash\Desktop\MyWebApp"

# 2. Initialize Git
git init

# 3. Create .gitignore
echo "__pycache__/
*.pyc
.env
venv/" > .gitignore

# 4. Add all files
git add .

# 5. First commit
git commit -m "Initial commit: Add MyWebApp project"

# 6. Connect to GitHub (create repo on GitHub first)
git remote add origin https://github.com/akashdip2001/MyWebApp.git

# 7. Push to GitHub
git push -u origin main

# 8. Later, when you make changes:
# Edit some files...
git add .
git commit -m "Add user registration feature"
git push

⚠️ Common Issues and Solutions

Issue 1: "fatal: not a git repository"

Problem: You're not in a Git-initialized folder Solution:

git init

Issue 2: "Updates were rejected"

Problem: Remote has changes you don't have locally Solution:

git pull origin main
# Then push again
git push

Issue 3: "No upstream branch"

Problem: Local branch not connected to remote Solution:

git push --set-upstream origin main

Issue 4: Merge conflicts

Problem: Same file edited in different ways Solution:

  1. Open conflicted files
  2. Look for <<<<<<<, =======, >>>>>>> markers
  3. Choose which version to keep
  4. Remove conflict markers
  5. git add . and git commit

🔐 Authentication

HTTPS (Recommended for beginners)

  • Uses your GitHub username and password/token
  • GitHub now requires Personal Access Token instead of password

Create Personal Access Token:

  1. GitHub → Settings → Developer settings → Personal access tokens
  2. Generate new token
  3. Select scopes (repo permissions)
  4. Use token as password when prompted

SSH (Advanced)

  • More secure, no need to enter credentials repeatedly
  • Requires SSH key setup

📊 Best Practices

Commit Messages

Good:

  • "Add user login functionality"
  • "Fix database connection error"
  • "Update README with setup instructions"

Bad:

  • "fix"
  • "update"
  • "asdf"

When to Commit

  • After completing a feature
  • After fixing a bug
  • Before switching branches
  • At the end of each work session

.gitignore Essentials

Always ignore:

  • Build files (__pycache__/, node_modules/)
  • Environment files (.env, venv/)
  • IDE files (.vscode/, .idea/)
  • OS files (.DS_Store, Thumbs.db)
  • Database files (*.sqlite3)

📚 Quick Start Template

For future projects, here's your quick setup:

# 1. Create project folder
mkdir MyNewProject
cd MyNewProject

# 2. Initialize Git
git init

# 3. Create basic files
echo "# MyNewProject" > README.md
echo "*.log
__pycache__/
.env" > .gitignore

# 4. First commit
git add .
git commit -m "Initial commit: Setup project structure"

# 5. Create GitHub repo (on GitHub website)
# 6. Connect and push
git remote add origin https://github.com/yourusername/MyNewProject.git
git push -u origin main

🎓 Learning Path

Beginner (You are here)

  • Understand Git basics
  • Connect local code to GitHub
  • Basic add, commit, push workflow

Intermediate (Next steps)

  • Branching and merging
  • Resolving conflicts
  • Using GitHub Issues and Pull Requests
  • Collaborating with others

Advanced (Future goals)

  • Git workflows (GitFlow, GitHub Flow)
  • Rebasing and advanced Git commands
  • CI/CD with GitHub Actions
  • Code reviews and team collaboration

🔗 Helpful Resources


Remember: Git seems complex at first, but with practice, these commands become second nature. Start with the basic workflow (add → commit → push) and gradually learn more advanced features.

Created: September 20, 2025