Comprehensive Git Guide for Beginners

Comprehensive Git Guide for Beginners

Published on: September 7, 2025

I wrote this guide to quickly save knowledge for reference and to teach friends and colleagues who are new to Git.

First, as a developer, you should get familiar with the CLI (Command Line Interface) instead of GUI (Graphical User Interface). The CLI helps you work faster, customize better, and understand Git mechanics.


1. Installing Git & Setting Up Your Account

Download Git

  • Git for Linux
  • Follow the installer instructions and keep default settings unless you know what you're doing.

    After installation, restart your terminal or command prompt.

    git --version
    git config --global user.name "Your GitHub Name"
    git config --global user.email "your.email@example.com"
    git config --list
    • --global: applies to all repositories on your machine.
    • Git uses the name and email to attach to commits. If it doesn't match GitHub, the commit author may not display correctly.

    2. Git Basic Concepts

    • Commit: A snapshot of your code at a specific point in time.
    • Branch: Separate line of development for a feature.
    • Remote: Repository on a server (GitHub, GitLab, etc.).
    • Staging Area / Stage: Area where changes are prepared for commit.
    • Push: Send commits from local to remote.
    • Pull: Retrieve new commits from remote to local.
    • Merge: Combine changes from another branch into your current branch.
    • Rebase: Move your branch commits onto the latest commit of another branch, keeping a linear history.
    • Branches help develop features separately without affecting the main branch (main).

    3. Checking Repository Status

    git status       # Check file status: changed, staged, untracked
    git branch       # List local branches, * indicates current branch
    • If you're already on the correct branch, you don't need to create or checkout again.
    • Create a new branch: git branch <name> or git checkout -b <name> if the branch doesn't exist.
    • Switch branch: git checkout <name> or git switch <name>
    • Avoid running git branch repeatedly on the same branch.

    4. Add / Commit / Push / Pull

    Adding files to the staging area

    git add .
    • Stage = files ready for commit.

    Commit

    git commit -m "Describe your changes"
    • Saves a snapshot of staged files.

    Push

    git push origin main
    • First push of a new branch: git push -u origin <branch-name> → sets upstream.

    Pull

    git pull origin main
    • Pull = Fetch + Merge
    • Keeps code synchronized and avoids conflicts.

    5. Working with Branches

    Creating a new branch (if not exists)

    git checkout -b feature-login
    • Develop features without affecting main.

    Switching to an existing branch

    git checkout feature-login   # Or git switch feature-login
    • No action needed if already on the correct branch.

    Commit & Push on a branch

    git add .
    git commit -m "Add login feature"
    git push origin feature-login

    6. Rebase vs Merge Workflow

    Suppose commits on main and feature-x branch:

    main:     A──B──C──G
    feature-x:    B──D──E──F

    Rebase

    git checkout feature-x
    git fetch origin
    git rebase origin/main
    Before rebase:
    main:      A──B──C──G
    feature-x: B──D──E──F
    
    After rebase:
    main:      A──B──C──G
    feature-x:          D'──E'──F'
    • D’-E’-F’ = commits D-E-F placed on top of the latest main commit
    • History is linear and clean, no merge commit

    Merge

    git checkout feature-x
    git merge origin/main
    Before merge:
    main:      A──B──C──G
    feature-x: B──D──E──F
    
    After merge:
    main:      A──B──C──G
    feature-x: B──D──E──F
                                \
                                 M
    • Merge creates commit M, preserves original commits
    • Rebase: clean linear history
    • Merge: preserves full original history

    7. Pull Request & Merge Branch

    • Push branch to remote:
    git push origin feature-login
    • Create Pull Request on GitHub → review → merge
    • Delete branch after merge:
    git branch -d feature-login
    git push origin --delete feature-login

    8. Adding Existing Projects, Creating New Repo, Pulling from GitHub

    Adding an existing project

    cd /path/to/project
    git init
    git add .
    git commit -m "Initial commit"
    git branch -M main
    git remote add origin git@github.com:username/repo-name.git
    git push -u origin main

    Creating a new project and push

    mkdir my-project
    cd my-project
    git init
    echo "# My Project" >> README.md
    git add README.md
    git commit -m "Add README"
    git branch -M main
    git remote add origin git@github.com:username/my-project.git
    git push -u origin main

    Pulling an existing GitHub project

    git clone git@github.com:username/repo-name.git
    cd repo-name
    git add .
    git commit -m "Update"
    git push origin main

    9. Updating Feature Branch from Main

    • Rebase:
    git checkout feature-login
    git fetch origin
    git rebase origin/main
    • Merge:
    git checkout feature-login
    git merge origin/main

    10. Tips to Avoid Conflicts

    1. Pull before coding
    2. Make small, clear commits
    3. Use separate branches for each feature/bugfix
    4. Update feature branch from main before PR
    5. Resolve conflicts:
    git add <file>
    git rebase --continue      # if rebase
    git commit                 # if merge

    6\. Use clear branch names: feature/login, bugfix/cart-error

    7\. Delete branches after merge


    11. Visual Workflow Diagram

    main:     A──B──C──G
    feature-x:    B──D──E──F
    
    Rebase:
    main:     A──B──C──G
    feature-x:          D'──E'──F'
    
    Merge:
    main:     A──B──C──G
    feature-x:    B──D──E──F
                                \
                                 M
    • A→G: main commits
    • D→F: feature branch commits before update
    • D’→F’: after rebase
    • M: merge commit