Git Handbook – Quick Reference
A complete collection of Git commands and notes, covering setup, commits, branching, merging, conflicts, and advanced workflows. This handbook is designed as a simple, step‑by‑step guide for beginners and professionals to quickly learn and apply Git in real projects.
Git is a tool that helps you track changes in your code, like a history book for your project. it is one of the most important skills for modern developers. Whether you’re a student learning coding basics or a professional managing large projects, knowing these commands will save time, prevent mistakes, and make collaboration easier.
1. Initialize a Git Repository
git init
This command initialize a local git repository in the current directory.
How does Git know it’s a repository?
→ Git creates a hidden folder named .git inside directory.
To view the hidden files →ls -a
Example – .git
README.md
src/
The .git folder contains all Git metadata, commit history, branches, and configuration.
2. Configure Git User Details – Set your user name, email globally
git config --global user.name "Your Name"git config --global user.email "your.email.com"
3. Git Workflow:-
Git mainly work in 3 areas.
Working Directory
↓
git add
↓
Staging Area
↓
git commit
↓
Local Repository → git push → Remote repo
Add changes to staging area
→ git add
→ This does not save changes permanently.
It only moves changes to the staging area, preparing them for commit.
4. Commit changes
git commit -m "added login feature here"
A commit stores the staged changes in the local git repository along with a message.
Git will track future modification and maintain the complete history.
How to view commit History –
git loggit log --oneline→ better option.
5. Branching →
Branching allow parallel development without affecting the main codebase.
- Create a Branch →
git branch <branch name> - Switch to a Branch →
git switch <branch name>/git checkout <branch name> - Create and switch to new branch in one command →
git checkout -b <branch name>
6. How to merging Branches:–
Suppose: you have two branches
main
feature-login
You want to merge feature-login branch into main
- Step 1: Switch to destination branch →
git switch main - Step 2: Merge Source branch →
git merge feature-login
Always switch to the destination branch before running git merge.
Syntax: git merge <Source branch>
Git merges the specified source branch into the current branch.
7. Compare Branches
Check differences b/w two branches:
→ git diff <source-branch>
Example –
- step1 →
git switch main - step2 →
git diff feature-login
This shows what changes exist in feature-login compared to main.
8. Git Reset (Undo commit)
→ Use carefully, especially on shared branches
-
Soft Reset –
git reset --soft <commit-id>
Removes commit history after the specified commit but keeps changes staged. -
Default Reset (Mixed Reset) –
git reset <commit-id>orgit reset --mixed <commit-id>
It removes commit history and unstages changes, but keeps files.
Hard Reset – git reset --hard <commit-id>
It removes commits and permanently delete file changes. Use with caution.
Reset Using HEAD –
- Remove last commit →
git reset --hard HEAD^ - Remove last two commits →
git reset --hard HEAD^^
Alternative command →git reset --hard HEAD~2
9. Cherry pick
It is used when you want to move only specific commits from one branch to another.
- Step 1: Switch to destination branch →
git switch main - Step 2: Apply required commit →
git cherry-pick <commit-id>
By “1st 4-6 digits”
10. Tags
Tags are useful to mark important releases.
- Tag latest commit →
git tag V1.0.0 - Tag specific commit →
git tag V1.0.0 <commit-id> - List all tags →
git tag - List all branches →
git branch -a
11. Working with remote repositories
- To view remote repositories →
git remote -v
Shows all configured remote repositories. - Add remote repository →
git remote add origin <repository url>
12. Clone Repository
git clone <repository url>
This download entire source code, complete commit history, branches, tags to your local system.
13. Git Fetch vs Git Pull
-
git fetch→ Downloads latest changes from remote repository but does not merge them into your current branch.
Remote Repo -----> Local git database
You can review the changes before merging. -
git pull→ It performs both git fetch + git merge.
Git fetches the latest changes and immediately merges them into the current branch.
14. SSH setup for Git (Azure Repos/GitHub)
→ Generate SSH key pair → ssh-keygen -t rsa
Press Enter for default values.
i) Go to SSH directory → cd ~/.ssh
ii) List files → ls -l
You should see something like this
- id_rsa
- id_rsa.pub
iii) Display public key → cat id_rsa.pub
Copy the entire output.
iv) Add Public Key to Azure Repos
- Open Azure DevOps
- Go to user settings
- Select SSH public keys
- Click Add
- Paste the public key
- Save
Now your local machine can securely authenticate and push/pull code without entering credentials every time.
15. How to Resolve Merge Conflicts in Git?
→ A merge conflict occurs when git cannot automatically decide which changes to keep because the same lines of code were modified in different branches.
Example Scenario –
Suppose:
main → app.txt
feature-login → app.txt
Both branch modified the same line.
When merging:
git switch maingit merge feature-login
Git may display:
Auto-merging app.text
CONFLICT (content): merge conflict in app.text
Automatic merge failed
Step:1 → check Conflicts files → git status
Output: Unmerged paths: both modified: app.text
Step 2: Open the conflicted file
Git marks the conflict code like this:
<<<<<<< HEAD
welcome to Application
=======
welcome to login Application
>>>>>>> feature-login
Meaning:
<<<<<<< HEAD(current branch / destination branch)=======(separator)>>>>>>>(incoming changes from source branch)
Step 3: Manually resolve the conflict → choose what should remain, remove all conflict markers (<<<<<<<, =======, >>>>>>>).
Step 4: Stage the resolve file → git add app.txt
Step 5: Complete the Merge → git commit -m "Resolve merge"
About the Merge → If don’t want to continue, → git merge --abort
This restores the repository to the state before merge started.
- Stashing git stash → Temporarily save uncommitted changes.
git stash list → Show list of stashes.
git stash apply → Reapply stash but keep it.
git stash pop → Reapply stash and remove it.
-
Rebase git rebase main → Reapply commits on top of another branch. Keeps history clean and linear.
-
Amend Commit git commit --amend → Modify the last commit (message or files).
Keep reading
You may also like
devops
Kubernetes Explained for Absolute Beginners
The clearest possible introduction to Kubernetes — what it is, why it exists, and the seven words you need to know.
devops
GitHub Actions: A Complete CI Pipeline
Lint, test, build, publish — a battle-tested workflow template.
devops
The Perfect Dockerfile for Node.js Apps
Multi-stage builds, layer caching and a 90% smaller image size.
Discussion (0)
No comments yet. Be the first to weigh in.