# Git Fundamentals Guide: Commands & Concepts Explained

## What is Git?

Git is a **distributed version control system** that tracks changes in your files over time. Think of it as a sophisticated "undo" system for your code that also lets you collaborate with others without overwriting each other's work.

Imagine you're writing a book. Git lets you save snapshots of your work at different stages, go back to any previous version, and even create alternate storylines (branches) to experiment without affecting your main draft.

**Key characteristics:**

* **Distributed:** Every developer has a complete copy of the project history on their machine
    
* **Fast:** Most operations work locally, so they're lightning quick
    
* **Free and open-source:** Created by Linus Torvalds in 2005 for Linux kernel development
    

---

## Why Git is Used

Git solves several critical problems in software development:

**Collaboration:** Multiple developers can work on the same project simultaneously without conflicts. Git intelligently merges changes and highlights conflicts when they occur.

**Version History:** Every change is recorded with who made it, when, and why. You can revert to any previous state if something breaks.

**Experimentation:** Create separate branches to try new features without risking your stable code. If the experiment works, merge it back; if not, delete the branch.

**Backup and Recovery:** Your entire project history is distributed across multiple machines, making data loss nearly impossible.

**Professional Standard:** Git is the industry standard for version control, used by companies from startups to tech giants like Google, Microsoft, and Facebook.

---

## Git Basics and Core Terminologies

Understanding these concepts is essential to working with Git effectively:

**Repository (Repo):** A repository is your project folder that Git tracks. It contains all your files plus a hidden `.git` folder where Git stores its tracking data. You can have local repositories on your computer and remote repositories on platforms like GitHub or GitLab.

**Working Directory:** This is your actual project folder where you create, edit, and delete files. It's what you see in your file explorer.

**Staging Area (Index):** Think of this as a preparation zone. When you finish working on some files, you add them to the staging area before committing. This lets you control exactly what changes go into each commit.

**Commit:** A commit is a snapshot of your project at a specific moment. Each commit has a unique ID (hash), a message describing what changed, and metadata like author and timestamp. Commits form the history of your project.

**Branch:** A branch is an independent line of development. The default branch is usually called `main` or `master`. You can create new branches to work on features, then merge them back when complete.

**HEAD:** HEAD is a pointer that shows you which commit you're currently viewing or which branch you're working on. Usually, HEAD points to the latest commit on your current branch.

**Remote:** A remote is a version of your repository hosted elsewhere (like GitHub). You can push your commits to remotes and pull others' commits from them.

**Clone:** Creating a complete copy of a remote repository on your local machine.

**Merge:** Combining changes from different branches into one branch.

**Conflict:** When Git can't automatically merge changes because the same lines were modified differently in two branches.

## Visual Flow: How Git Work

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769590076469/9ccb82e7-6eda-4e51-8b34-6a2eeafe28d6.png align="center")

## Common Git Commands

**Setting Up Git**

```javascript

git config --global user.name "Awdhesh kumar"
git config --global user.email "awdheskumar@gmail.com"

//Check your settings
git config --list
```

**Starting a Repository**

```javascript
//Create a new repository in current directory
git init

// Clone an existing repository
git clone https://github.com/Awdhesh9860
```

**Checking Status and History**

```javascript
// See which files are modified, staged, or untracked
git status

//View commit history
git log

//View condensed log (one line per commit)
git log --oneline

//View log with branch graph
git log --graph --oneline --all
```

**Making Changes**

```javascript
//Stage a specific file
git add filename.txt

//Stage all modified files
git add .

// Commit staged changes with a message
git commit -m "Add user authentication feature"

// Stage and commit in one step (only for tracked files)
git commit -am "Fix navigation bug"
```

**Working with Branches**

```javascript
//List all branches
git branch

//Create a new branch
git branch feature-login

//Switch to a branch
git checkout feature-login

//Create and switch to a new branch in one command
git checkout -b feature-dashboard

// Modern alternative to checkout (Git 2.23+)
git switch feature-login
git switch -c feature-new-branch

//Merge a branch into current branch
git merge feature-login

//    Delete a branch
git branch -d feature-login
```

**Syncing with Remote Repositories**

```javascript
// View remote repositories
git remote -v

// Add a remote repository
git remote add origin https://github.com/Awdhesh9860

//Push commits to remote
git push origin main

// Pull changes from remote
git pull origin main

// Fetch changes without merging
git fetch origin
```

**Undoing Changes**

```javascript
// Unstage a file (keep changes in working directory)
git reset filename.txt

// Discard changes in working directory
git checkout -- filename.txt

// Undo last commit but keep changes staged
git reset --soft HEAD~1

// Undo last commit and unstage changes
git reset HEAD~1

//Undo last commit and discard changes (dangerous!)
git reset --hard HEAD~1

// View what changed in a file
git diff filename.txt
```

## Basic Developer Workflow

**Starting a New Project**

```bash
# Create project folder
mkdir my-website
cd my-website

# Initialize Git
git init

# Create your first file
echo "# My Website" > README.md

# Check status
git status
# Output: README.md is untracked

# Stage the file
git add README.md

# Commit with a message
git commit -m "Initial commit: Add README"

# View history
git log
```

**Working on a Feature**

```bash
# Create a new feature branch
git checkout -b add-homepage

# Create and edit files
echo "<h1>Welcome</h1>" > index.html

# Stage and commit
git add index.html
git commit -m "Add homepage with welcome message"

# Switch back to main branch
git checkout main

# Merge the feature
git merge add-homepage

# Delete the feature branch (optional)
git branch -d add-homepage
```

**Collaborating with Others**

```bash
# Add remote repository
git remote add origin https://github.com/yourusername/my-website.git

# Push your code
git push -u origin main

# Later, when working with a team...
# Pull latest changes before starting work
git pull origin main

# Make your changes, commit them
git add .
git commit -m "Update navigation menu"

# Push your changes
git push origin main
```
