Git Notes with Commands, Diagrams &
Examples
1. What is Git?
Git is a distributed version control system used to track code changes, collaborate with
teams, and manage project versions.
- Local Repository (your system)
- Remote Repository (GitHub, GitLab, Bitbucket, etc.)
2. Git Workflow Diagram
Working Directory → Staging Area → Local Repository → Remote Repository
- Working Directory: Your actual project files
- Staging Area: Changes marked for commit
- Local Repository: Your commits stored locally
- Remote Repository: Central repo (e.g., GitHub)
📘 Git Notes with Diagrams & Examples
1. What is Git?
Git is a distributed version control system used to track code changes, collaborate with
teams, and manage project versions.
Local Repository (your system)
Remote Repository (GitHub, GitLab, Bitbucket, etc.)
2. Git Workflow Diagram
Working Directory → Staging Area → Local Repository → Remote Repository
Working Directory: Your actual project files
Staging Area: Changes marked for commit
Local Repository: Your commits stored locally
Remote Repository: Central repo (e.g., GitHub)
3. Common Git Commands
(a) git init
📌 Creates a new Git repository.
git init
Diagram:
Folder → (now becomes) → Git Repository (.git folder created)
(b) git clone
📌 Copy remote repo into your local system.
git clone [Link]
Diagram:
Remote Repo → Local Repo (exact copy)
(c) git status
📌 Shows the state of working directory & staging area.
git status
(d) git add
📌 Moves files from working directory → staging area.
git add [Link]
git add .
Diagram:
Working Directory → [git add] → Staging Area
(e) git commit
📌 Save changes to local repository.
git commit -m "Added new feature"
Diagram:
Staging Area → [git commit] → Local Repository
(f) git log
📌 Show commit history.
git log
(g) git remote add origin
📌 Connects your local repo to remote repo.
git remote add origin [Link]
(h) git push
📌 Upload local commits → remote repo.
git push origin main
Diagram:
Local Repo → Remote Repo
(i) git pull
📌 Get latest changes from remote repo → local repo.
git pull origin main
Diagram:
Remote Repo → Local Repo
(j) git branch
📌 List, create, or delete branches.
git branch # list branches
git branch dev # create branch
(k) git checkout / git switch
📌 Move between branches.
git checkout dev
# or
git switch dev
(l) git merge
📌 Combine changes from another branch into current branch.
git merge dev
Diagram:
(main) ← merge ← (dev)
(m) git diff
📌 Show changes in files.
git diff
(n) git reset
📌 Undo changes (different levels).
git reset --soft HEAD~1 # move commit back, keep changes staged
git reset --hard HEAD~1 # delete commit & changes
(o) git stash
📌 Save uncommitted changes temporarily.
git stash
git stash pop
4. Full Git Workflow Example
git init
git add .
git commit -m "First commit"
git branch -M main
git remote add origin [Link]
git push -u origin main
Diagram:
Working Dir → add → Staging → commit → Local Repo → push → Remote Repo
📘 Extended Git Notes with Commands, Examples & Diagrams
🔹 1. Configuration Commands
(a) git config
📌 Set up username, email, or editor.
git config --global [Link] "Shubham"
git config --global [Link] "shubham@[Link]"
git config --list
Diagram:
Git Config File ← [set values] ← User
🔹 2. Branch Management
(a) Create & Delete Branch
git branch feature # create branch
git branch -d feature # delete branch (safe)
git branch -D feature # force delete
(b) Rename Branch
git branch -m oldName newName
🔹 3. Checkout & Switch
git checkout feature # switch branch
git switch feature # (newer command)
git checkout -b dev # create + switch to new branch
Diagram:
(main) → [checkout -b dev] → (dev branch)
🔹 4. Merge vs Rebase
(a) git merge
Combine commits of another branch.
git merge dev
Diagram:
A---B---C (main)
\
D---E (dev)
Merged → A---B---C---D---E
(b) git rebase
Reapply commits on top of another branch.
git checkout dev
git rebase main
Diagram:
A---B---C (main)
\
D---E (dev)
Rebase → A---B---C---D'---E'
🔹 5. Undoing Changes
(a) git restore (new command)
git restore [Link] # discard changes
git restore --staged [Link] # unstage file
(b) git reset
git reset --soft HEAD~1 # undo last commit, keep staged
git reset --mixed HEAD~1 # undo last commit, unstaged
git reset --hard HEAD~1 # undo last commit + changes
(c) git revert
📌 Safely undo commit (creates a new commit).
git revert <commit-id>
Diagram:
commitX → commitY → commitZ
(revert commitZ) → commitX → commitY → commitZ → revertZ
🔹 6. Stashing
git stash save "work in progress"
git stash list
git stash pop
git stash drop stash@{0}
Diagram:
Working changes → stash (hidden storage) → pop → back to working dir
🔹 7. Remote Management
(a) View & Add Remote
git remote -v
git remote add origin [Link]
(b) Change Remote URL
git remote set-url origin [Link]
(c) Fetch Changes
git fetch origin
Diagram:
Remote Repo → fetch → Local Repo (but not merged)
🔹 8. Tagging Versions
git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0 release"
git push origin v1.0.0
git tag --list
Diagram:
commitA → commitB (tag v1.0)
🔹 9. Cherry-Pick
📌 Apply a specific commit from one branch into another.
git cherry-pick <commit-id>
Diagram:
Branch A: commit1 → commit2 → commit3
Branch B: commitX
cherry-pick commit2 → Branch B: commitX → commit2
🔹 10. Clean Untracked Files
git clean -n # show what will be deleted
git clean -f # delete untracked files
🔹 11. Show & Log Commands
git show <commit-id> # show commit details
git log --oneline # compact log
git log --graph --oneline --all
Diagram: (log graph example)
* commitZ (main)
| * commitY (dev)
|/
* commitX
🔹 12. Collaboration Commands
git fork # (done via GitHub UI, not CLI)
git pull request # (done via GitHub/GitLab UI)
📘 Git Commands – Theory with Explanation
🔹 Basic Commands
1. git init
Used to initialize a new Git repository in the current folder.
Creates a hidden .git folder that tracks version history.
After this, the folder is officially a Git project.
2. git clone
Creates a copy of an existing remote repository on your local system.
Useful when working with GitHub, GitLab, or Bitbucket projects.
3. git status
Shows the state of working directory and staging area.
Tells you which files are untracked, modified, or staged for commit.
4. git add
Moves files from working directory → staging area.
Staging area acts as a preparation zone before committing changes.
5. git commit
Records the changes from staging area into the local repository history.
Each commit gets a unique ID (hash) and message for tracking.
6. git log
Displays the history of commits in the repository.
Includes commit ID, author, date, and message.
7. git remote add origin <url>
Connects your local repository to a remote repository (like GitHub).
origin is the default name given to remote repo.
8. git push
Uploads commits from local repository → remote repository.
Keeps the remote repo updated with your changes.
9. git pull
Fetches changes from remote repository and merges them into local branch.
Equivalent to git fetch + git merge.
10. git branch
Used to create, list, or delete branches.
Branches allow development of new features without disturbing main code.
11. git checkout / git switch
Moves between different branches or commits.
git checkout -b <branch> creates and switches in one step.
git switch is a modern alternative for branch switching.
12. git merge
Combines two branches together.
Creates a new commit that represents merged changes.
13. git diff
Shows differences between working directory, staging, and commits.
Helps in reviewing changes before committing.
14. git reset
Used to undo commits or unstage changes.
Variants:
--soft: keeps changes staged
--mixed: keeps changes but unstages them
--hard: discards everything
15. git stash
Temporarily saves uncommitted changes into a “stash” storage.
Useful when switching branches without committing work-in-progress.
🔹 Advanced Commands
16. git config
Manages Git configuration settings.
Example: set username, email, editor.
17. git branch -d / -D
Deletes branches.
-d = safe delete (if branch is merged).
-D = force delete (ignores unmerged changes).
18. git rebase
Moves or re-applies commits from one branch onto another.
Keeps history linear (unlike merge which creates extra commits).
19. git restore
Modern way to discard changes or unstage files.
Safer alternative to git reset for un-staging.
20. git revert
Safely undoes a commit by creating a new commit that reverses changes.
Maintains history without deleting commits.
21. git fetch
Downloads changes from remote repository without merging.
Lets you review updates before applying them.
22. git tag
Marks a specific commit as a version (v1.0, v2.0, etc.).
Used for release management.
23. git cherry-pick
Applies a specific commit from one branch to another.
Useful when only a single bug fix needs to be copied.
24. git clean
Removes untracked files from working directory.
Prevents clutter from temporary or generated files.
25. git show
Displays details of a commit or tag.
Shows diff, author, message, and date.
26. git log --graph
Displays commit history in a graph/tree structure.
Useful to visualize branching and merging.
3. Common Git Commands
a) git init
Creates a new Git repository.
Example: git init
b) git clone
Clone remote repo locally.
Example: git clone <url>
c) git status
Show current status.
Example: git status
d) git add
Add files to staging.
Example: git add [Link] | git add .
e) git commit
Save staged changes to local repo.
Example: git commit -m "message"
f) git log
Show commit history.
Example: git log
g) git remote add origin
Connect local repo to remote.
Example: git remote add origin <url>
h) git push
Push commits to remote repo.
Example: git push origin main
i) git pull
Pull changes from remote repo.
Example: git pull origin main
j) git branch
Manage branches.
Example: git branch | git branch dev
k) git checkout / git switch
Switch branches.
Example: git checkout dev | git switch dev
l) git merge
Merge changes.
Example: git merge dev
m) git diff
Show changes.
Example: git diff
n) git reset
Undo commits.
Example: git reset --soft HEAD~1
o) git stash
Save uncommitted changes.
Example: git stash | git stash pop
4. Advanced Git Commands
a) git config
Set username & email.
Example: git config --global [Link] "Shubham"
b) git branch -d / -D
Delete branches.
Example: git branch -d feature
c) git rebase
Reapply commits on top of another branch.
Example: git rebase main
d) git restore
Discard changes.
Example: git restore [Link]
e) git revert
Safely undo commit.
Example: git revert <commit-id>
f) git stash save, list, pop, drop
Manage stash.
g) git fetch
Fetch changes from remote.
Example: git fetch origin
h) git tag
Create tags.
Example: git tag v1.0.0
i) git cherry-pick
Apply commit from another branch.
Example: git cherry-pick <commit-id>
j) git clean
Remove untracked files.
Example: git clean -f
k) git show
Show commit details.
Example: git show <commit-id>
l) git log --graph
Visual commit history.
Example: git log --graph --oneline --all