Git and GitHub Basics
Definition:
• Git is a distributed version control system.
• Tracks changes in files and folders over time.
• Allows you to revert back to previous versions.
Why Git?
• Saves a history of your work.
• Enables multiple people to work on the same project without
conflicts.
• Helps avoid the “final_final_final.docx” problem!
What is GitHub?
Definition:
• GitHub is a platform for hosting Git repositories in the cloud.
• Makes it easy to share your code and collaborate with others.
Key Features:
• Centralized code storage.
• Collaboration tools like pull requests and issues.
• Project management integrations.
Why Use Git and GitHub?
• For Individuals:
• Backup your work.
• Undo mistakes.
• Experiment safely with new ideas.
• For Teams:
• Collaborate on the same project efficiently.
• Track who made changes and why.
• Avoid overwriting each other’s work.
Installing Git
• Step 1: Download Git
Go to git-scm.com and download the version for your OS.
• Step 2: Install Git
Follow the setup wizard (default options are fine for beginners).
• Step 3: Verify Installation
Open a terminal or command prompt and run:
git --version
Configuring Git
git config --global user.name "Your Name"
git config --global user.email youremail@example.com
git config --list
Key Concepts
• Repository: A project folder tracked by Git.
• Commit: A snapshot of your changes.
• Branch: A separate line of development.
• Remote: A version of your repository hosted on a server like
GitHub.
Basic Git Commands
Initialize a repository:
git init
Check the status of your files: git status
Stage your changes:
git add <filename> # Stage a specific file
git add . # Stage all changes
Commit your changes:
git commit -m "Write a clear message about what you changed"
Commiting more changes: (push and pull)
git status
git push origin <branch-name>
git push origin main
git status
git pull origin <branch-name>
When to Use git pull
• Before making new changes locally to ensure you're working with the latest version.
• When collaborating with others and new changes are pushed to the remote branch.
Working with GitHub
• Step 1: Create a GitHub Account
Sign up at github.com.
• Step 2: Create a Repository
• Click New Repository.
• Name your repository (e.g., my-first-repo).
• Choose visibility (Public/Private).
• Step 3: Link your local repository to GitHub:
Cloning a Repository
• To copy a remote repository to your computer:
git clone <repository-URL>
git clone https://github.com/your-username/my-first-repo.git
Branching in Git
• Why use branches?
• Keep your main code stable.
• Experiment with new features.
• Commands:
• Create a branch:
• git branch new-feature
• Switch to the branch:
▪ git checkout new-feature
• Merge the branch into main:
• git checkout main
▪ git merge new-feature
Collaborating on GitHub
• Forking:
Make your own copy of someone else’s repository.
• Pull Requests:
Propose your changes to the original project.
• Issues:
Use issues to track bugs or suggest new features.
Common Mistakes
• Forgetting to commit regularly.
• Writing vague commit messages ( "fixed stuff").
• Overwriting changes during a merge.
Best Practices
• Commit often with meaningful messages.
• Use branches for experiments.
• Sync your work with git pull before making changes.
• Review your changes before pushing.