Getting Started with Git and GitHub: A Beginner's Version Control Guide

SdeepSdeep
3 min read

1. Introduction to Version Control

Version control is a system that records changes to files over time, allowing you to track modifications, revert to previous versions, and collaborate efficiently. It’s essential for developers, data scientists, and anyone working on code-based projects.

Why Use Version Control?

  • Track Changes: See who made what changes and when.

  • Collaborate Easily: Multiple people can work on the same project without conflicts.

  • Recover Mistakes: Revert to previous versions if something breaks.

  • Branching & Experimentation: Test new features without affecting the main code.

Popular version control systems include Git, Mercurial, and Subversion, with Git being the most widely used.


2. Setting Up Git and GitHub

Installing Git

  1. Windows: Download from git-scm.com

  2. Mac: Use brew install git

  3. Linux: sudo apt install git (Debian/Ubuntu) or sudo yum install git (Fedora)

Configuring Git

Set your username and email (used in commits):

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

Creating a GitHub Account

GitHub is a cloud-based platform for hosting Git repositories. Sign up at github.com.

Linking Git and GitHub

Generate an SSH key for secure authentication:

ssh-keygen -t ed25519 -C "your.email@example.com"

Add the key to GitHub under Settings → SSH and GPG keys.


3. Working with Git: Commits, Branches, and Merges

Basic Git Commands

  • git init – Initialize a new Git repo

  • git clone <repo-url> – Clone an existing repo

  • git status – Check changes in your working directory

  • git add <file> – Stage changes for commit

  • git commit -m "message" – Save changes with a message

  • git log – View commit history

Branching & Merging

  • git branch – List branches

  • git checkout -b <branch-name> – Create & switch to a new branch

  • git merge <branch> – Merge a branch into the current one

Example workflow:

git checkout -b feature/new-login  
# Make changes  
git add .  
git commit -m "Add new login feature"  
git checkout main  
git merge feature/new-login

4. Collaborative Workflows with GitHub

Forking & Cloning Repos

  1. Fork a repo on GitHub (creates your copy).

  2. Clone it locally:

     git clone https://github.com/your-username/repo.git
    

Syncing with Upstream

To keep your fork updated:

git remote add upstream https://github.com/original-repo/repo.git  
git fetch upstream  
git merge upstream/main

Push & Pull

  • git push origin <branch> – Upload changes to GitHub

  • git pull – Fetch and merge remote changes


5. Managing Pull Requests and Code Reviews

Creating a Pull Request (PR)

  1. Push your branch to GitHub.

  2. Open a PR from your branch to the target branch (e.g., main).

  3. Add a description and request reviews.

Reviewing Code

  • Leave comments on specific lines.

  • Approve, request changes, or merge the PR.


6. Automating Workflows with GitHub Actions

GitHub Actions allows automation of CI/CD (Continuous Integration/Deployment).

Example Workflow (.github/workflows/test.yml)

name: Run Tests  
on: [push, pull_request]  
jobs:  
  test:  
    runs-on: ubuntu-latest  
    steps:  
      - uses: actions/checkout@v3  
      - run: npm install  
      - run: npm test

This runs tests automatically on every push or PR.


7. Q&A and Hands-On Practice

Common Git Issues & Fixes

  • Undo a commit: git reset --soft HEAD~1

  • Fix a merge conflict: Edit conflicted files, then git add and git commit.

Try It Yourself

  1. Create a repo on GitHub.

  2. Clone it, make changes, and push.

  3. Create a branch, make a PR, and merge it.


8. Conclusion and Resources

Git and GitHub are powerful tools for version control and collaboration. By mastering commits, branches, PRs, and automation, you can streamline your workflow and work effectively in teams.

Further Learning

Happy coding! 🚀

Would you like a more detailed breakdown of any section? Let me know in the comments!

0
Subscribe to my newsletter

Read articles from Sdeep directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sdeep
Sdeep

👋 Hello! I'm passionate about DevOps and I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!