Skip to content

SumanAddi/Git-Learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🧱 Git Fundamentals & Workflow (Deep Dive)

This section explains core Git concepts, commands, and real-time usage with practical examples.


🧠 Stages of Git (Core Concept)

Working Directory β†’ Staging Area β†’ Local Repository β†’ Remote Repository
     (edit)            (add)            (commit)            (push)

πŸ“Œ Explanation

Stage Description
πŸ“ Working Directory Where you write/edit code
πŸ“¦ Staging Area Where changes are prepared (git add)
πŸ—‚ Local Repository Stored in .git folder (commit history)
☁️ Remote Repository GitHub / remote storage

βš™οΈ Setup Git Environment (RHEL / Linux)

sudo -s
mkdir gitproject
cd gitproject

yum install git -y
git --version

πŸš€ Initialize Repository

git init

πŸ‘‰ Creates .git directory (stores history + metadata)

git status

πŸ“‚ First File Workflow

touch index.html
vi index.html
git status

πŸ”΄ Untracked β†’ not added yet

git add index.html

🟒 Tracked (staged)

git commit -m "my first commit"

🧩 Git Flow Diagram

index.html
   ↓
Working Directory
   ↓ git add
Staging Area
   ↓ git commit
Local Repository (.git)

πŸ“„ Multiple File Commits

touch hello.txt
git add hello.txt
git commit -m "second commit"

touch test.txt
git add test.txt
git commit -m "third commit"

πŸ“Œ File Modes in Git

100644 β†’ Normal file
100755 β†’ Executable file

πŸ“œ Git Logs & History

git log
git log --oneline
git log --oneline -2
git log -p -2
git log --stat

Custom Format

git log --pretty=format:"%h - %an, %ar : %s"

πŸ” Inspect Changes

git show
git show <commit-id>

🧾 Git Blame (Line Tracking)

git blame index.html

πŸ‘‰ Shows who changed each line


πŸ”„ Git Diff (Compare Changes)

Working vs Staging

git diff

Staging vs Repository

git diff --staged

Working vs Last Commit

git diff HEAD

Between Commits

git diff <commit1>..<commit2>

🧩 Diff Visual

Working Dir β‰  Staging β†’ git diff
Staging β‰  Repo       β†’ git diff --staged
Working β‰  Repo       β†’ git diff HEAD

πŸ‘€ Git User Configuration

git config user.name "your-name"
git config user.email "your-email"

Check:

git config --list

Global config:

git config --global --edit

πŸ” Filter Logs by Author

git log --author="User"

πŸ“¦ Add Multiple Files

touch python{1..5}
git add .
git commit -m "multiple files"

🧠 Important Observations

  • .git folder stores entire history
  • git add = move to staging
  • git commit = save snapshot
  • git status = current state
  • git diff = compare changes

🎯 Summary

git init      β†’ initialize repo
git add       β†’ stage changes
git commit    β†’ save snapshot
git log       β†’ view history
git diff      β†’ compare changes
git blame     β†’ track line changes

⭐ Pro Tips

  • Always check git status before commit
  • Use git diff to avoid mistakes
  • Configure username before committing
  • Keep commits small and meaningful


πŸš€ Git Learning Notes (Hands-on + Visual Guide)

Welcome to my Git Practical Learning Repository πŸ“˜ This guide includes real-world Git commands with clear explanations and diagrams.


🧠 Git Basics (How Git Works)

Working Directory  β†’  Staging Area  β†’  Repository
     (edit)            (git add)         (commit)

πŸ“‚ 1. Basic Workflow

vi index.html
git add index.html
git commit -m "1st commit"

πŸ‘‰ Repeat changes and commits multiple times to build history.


✏️ 2. Git Amend (Modify Last Commit)

git commit --amend -m "updated message"

🧩 Diagram

Before:
A β†’ B β†’ C (HEAD)

After:
A β†’ B β†’ C' (HEAD)

πŸ” 3. Git Rebase (Modify Multiple Commits)

git rebase -i HEAD~3
  • Opens interactive mode
  • Replace pick with reword
  • Modify commit messages

🧩 Diagram

Before:
A β†’ B β†’ C β†’ D (HEAD)

After:
A β†’ B' β†’ C' β†’ D' (HEAD)

🧩 4. Squashing Commits

git rebase -i HEAD~3

🧩 Before

A β†’ B β†’ C β†’ D (HEAD)

🧩 After (Squash)

A β†’ B β†’ D' (HEAD)

πŸ‘‰ Multiple commits β†’ One clean commit


⚠️ 5. Git Reset (Move HEAD)

Soft Reset

git reset --soft HEAD^
A β†’ B β†’ C (HEAD)

After:
A β†’ B (HEAD)
Changes still in staging

Hard Reset

git reset --hard HEAD^
A β†’ B β†’ C (HEAD)

After:
A β†’ B (HEAD)
Changes ❌ deleted

πŸ”„ 6. Git Checkout (Restore Files)

git show <commit-id>:index.html
git checkout <commit-id> -- index.html

Restore all files:

git checkout <commit-id> -- *

Return to latest:

git checkout master -- index.html

πŸ†• 7. Git Restore (Modern Alternative)

Undo local changes

git restore file.txt

Unstage file

git restore --staged file.txt

OR

git rm --cached file.txt

πŸ“Š Restore vs Reset

Feature git restore git reset
Scope Working dir / staging Full repo
Affects history ❌ No βœ… Yes
Use case Undo changes Move HEAD
Safety Safe Risky

🧠 8. HEAD Explained

HEAD β†’ current branch β†’ latest commit

Example

HEAD β†’ master β†’ A β†’ B β†’ C

Detached HEAD

HEAD β†’ C
master β†’ B

πŸ“¦ 9. Git Stash (Temporary Work Save)

Flow Diagram

Working Directory
     ↓
 git stash
     ↓
[ Saved in stash ]
     ↓
Working Directory Clean

πŸ§ͺ Real Scenario

Step 1: Work on Story1

vi story1
git add story1
git stash

Step 2: Work on Story2

vi story2
git add story2
git commit -m "story2"

Step 3: Restore Story1

git stash apply

πŸ“‹ Useful Commands

git stash list
git stash apply
git stash clear

Stash Specific Files

git stash push -m "message" -- story1 story2

🧹 10. Cleanup Repo

rm -rf *
git add .
git commit -m "clean"

🎯 Summary Cheat Sheet

git add        β†’ stage changes
git commit     β†’ save changes
git amend      β†’ fix last commit
git rebase     β†’ edit history
git reset      β†’ move HEAD
git restore    β†’ undo changes
git stash      β†’ temporary save

⭐ Pro Tips

  • βœ… Use git restore instead of checkout
  • ⚠️ Avoid git reset --hard unless sure
  • 🧠 Understand HEAD before using rebase
  • πŸš€ Keep commit history clean using squash

πŸ§‘β€πŸ’» Author

Git Practice & Learning Notes Built for DevOps & Interview Preparation πŸš€

About

πŸš€ Git Learning Notes (Hands-on + Visual Guide)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors