This section explains core Git concepts, commands, and real-time usage with practical examples.
Working Directory β Staging Area β Local Repository β Remote Repository
(edit) (add) (commit) (push)
| 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 |
sudo -s
mkdir gitproject
cd gitproject
yum install git -y
git --versiongit initπ Creates .git directory (stores history + metadata)
git statustouch index.html
vi index.htmlgit statusπ΄ Untracked β not added yet
git add index.htmlπ’ Tracked (staged)
git commit -m "my first commit"index.html
β
Working Directory
β git add
Staging Area
β git commit
Local Repository (.git)
touch hello.txt
git add hello.txt
git commit -m "second commit"
touch test.txt
git add test.txt
git commit -m "third commit"100644 β Normal file
100755 β Executable file
git log
git log --oneline
git log --oneline -2
git log -p -2
git log --statgit log --pretty=format:"%h - %an, %ar : %s"git show
git show <commit-id>git blame index.htmlπ Shows who changed each line
git diffgit diff --stagedgit diff HEADgit diff <commit1>..<commit2>Working Dir β Staging β git diff
Staging β Repo β git diff --staged
Working β Repo β git diff HEAD
git config user.name "your-name"
git config user.email "your-email"Check:
git config --listGlobal config:
git config --global --editgit log --author="User"touch python{1..5}
git add .
git commit -m "multiple files".gitfolder stores entire historygit add= move to staginggit commit= save snapshotgit status= current stategit diff= compare changes
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
- Always check
git statusbefore commit - Use
git diffto avoid mistakes - Configure username before committing
- Keep commits small and meaningful
Welcome to my Git Practical Learning Repository π This guide includes real-world Git commands with clear explanations and diagrams.
Working Directory β Staging Area β Repository
(edit) (git add) (commit)
vi index.html
git add index.html
git commit -m "1st commit"π Repeat changes and commits multiple times to build history.
git commit --amend -m "updated message"Before:
A β B β C (HEAD)
After:
A β B β C' (HEAD)
git rebase -i HEAD~3- Opens interactive mode
- Replace
pickwithreword - Modify commit messages
Before:
A β B β C β D (HEAD)
After:
A β B' β C' β D' (HEAD)
git rebase -i HEAD~3A β B β C β D (HEAD)
A β B β D' (HEAD)
π Multiple commits β One clean commit
git reset --soft HEAD^A β B β C (HEAD)
After:
A β B (HEAD)
Changes still in staging
git reset --hard HEAD^A β B β C (HEAD)
After:
A β B (HEAD)
Changes β deleted
git show <commit-id>:index.html
git checkout <commit-id> -- index.htmlRestore all files:
git checkout <commit-id> -- *Return to latest:
git checkout master -- index.htmlgit restore file.txtgit restore --staged file.txtOR
git rm --cached file.txt| Feature | git restore | git reset |
|---|---|---|
| Scope | Working dir / staging | Full repo |
| Affects history | β No | β Yes |
| Use case | Undo changes | Move HEAD |
| Safety | Safe | Risky |
HEAD β current branch β latest commit
HEAD β master β A β B β C
HEAD β C
master β B
Working Directory
β
git stash
β
[ Saved in stash ]
β
Working Directory Clean
vi story1
git add story1
git stashvi story2
git add story2
git commit -m "story2"git stash applygit stash list
git stash apply
git stash cleargit stash push -m "message" -- story1 story2rm -rf *
git add .
git commit -m "clean"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
- β
Use
git restoreinstead of checkout β οΈ Avoidgit reset --hardunless sure- π§ Understand HEAD before using rebase
- π Keep commit history clean using squash
Git Practice & Learning Notes Built for DevOps & Interview Preparation π