Git and GitHub for Beginners

Learn Git and GitHub Basics from Scratch

Git and GitHub for Beginners

Version control is an essential skill for every developer, and Git is the most popular version control system used worldwide. Combined with GitHub, it allows for seamless collaboration and efficient code management. In this guide, we'll explore Git and GitHub from the ground up, covering installation, basic commands, collaboration, and best practices.


1. What is Git?

Git is a distributed version control system that tracks changes in source code, allowing multiple developers to collaborate efficiently. Unlike centralized systems, Git allows developers to work on a local copy of the project and sync changes when needed.

Why Use Git?

✅ Tracks changes in code history
✅ Enables collaboration without conflicts
✅ Allows code rollback and recovery
✅ Works offline and integrates with GitHub


2. What is GitHub?

GitHub is a cloud-based platform that hosts Git repositories, enabling developers to share and manage their code online. It facilitates open-source contributions, issue tracking, and collaboration through pull requests.

Git vs. GitHub

FeatureGitGitHub
TypeVersion control systemCloud-based hosting service
Works offline?YesNo
CollaborationLocal repositoriesRemote repositories
AccessCommand-lineWeb interface

3. Installing Git

Windows

  1. Download Git from git-scm.com

  2. Install with default settings

  3. Open Git Bash and verify installation using:

     git --version
    

Mac

  1. Install using Homebrew:

     brew install git
    
  2. Verify installation:

     git --version
    

Linux

  1. Install Git using package manager:

     sudo apt install git  # Debian/Ubuntu
     sudo yum install git  # Fedora/RHEL
    
  2. Verify installation:

     git --version
    

4. Configuring Git

Once installed, configure Git with your name and email:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

Verify your configuration:

git config --list

5. Basic Git Commands

Initialize a Repository

To start tracking a project with Git, navigate to the project folder and run:

git init

This creates a hidden .git directory.

Check the Status

To see the current state of your repository:

git status

Adding Files to Staging

Before committing, add files to the staging area:

git add filename.txt  # Add a specific file
git add .             # Add all files

Committing Changes

To save changes locally:

git commit -m "Your commit message"

Viewing Commit History

git log

6. Working with GitHub

Creating a GitHub Repository

  1. Go to GitHub and log in.

  2. Click on New Repository.

  3. Enter a repository name and click Create Repository.

  4. Copy the repository URL.

Connecting Local Repo to GitHub

git remote add origin <repository-URL>
git branch -M main
git push -u origin main

Cloning a Repository

To copy an existing repository:

git clone <repository-URL>

Pulling Latest Changes

git pull origin main

Pushing Changes to GitHub

git push origin main

7. Branching in Git

Branches allow developers to work on different features without affecting the main project.

Creating a Branch

git branch feature-branch

Switching Branches

git checkout feature-branch

Merging Branches

git checkout main
git merge feature-branch

Deleting a Branch

git branch -d feature-branch

8. Collaborating with GitHub

Forking a Repository

Forking allows you to copy someone else’s repository to your GitHub account.

  1. Open the repository on GitHub.

  2. Click Fork (top right).

  3. Clone the forked repo:

     git clone <your-forked-repo-URL>
    

Creating a Pull Request (PR)

  1. Make changes in a new branch.

  2. Push changes to your forked repository:

     git push origin feature-branch
    
  3. Open a Pull Request on GitHub.

Resolving Merge Conflicts

  1. Open the conflicting file.

  2. Look for <<<<<<< HEAD and >>>>>>> markers.

  3. Manually edit the file to keep the correct version.

  4. Add and commit the resolved file:

     git add .
     git commit -m "Resolved merge conflict"
    
  5. Push the changes.


9. Undoing Changes in Git

Undo Unstaged Changes

git checkout -- filename.txt

Undo Staged Changes

git reset filename.txt

Undo Last Commit

git reset --soft HEAD~1

10. Best Practices for Using Git

✅ Commit often with meaningful messages.
✅ Keep branches small and focused on one feature.
✅ Always pull the latest changes before pushing.
✅ Use .gitignore to exclude unnecessary files.
✅ Review and test code before merging PRs.


Final Thoughts

By now, you should have a solid understanding of Git and GitHub. Whether you're managing personal projects or collaborating on large-scale applications, these tools will make your development process efficient and organized.

🚀 Start using Git & GitHub today and contribute to open-source projects! Happy coding! 😃