0% found this document useful (0 votes)
32 views14 pages

Git Demystified - 100+ Essential Commands

This document provides a comprehensive guide to over 100 essential Git commands for developers, covering everything from setup to advanced usage. It emphasizes the importance of understanding Git for collaboration, conflict resolution, and version control, with practical examples and interview tips throughout. The conclusion highlights that mastering Git involves not only knowing commands but also grasping workflows and problem-solving skills.

Uploaded by

dwaraknath2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Git Demystified - 100+ Essential Commands

This document provides a comprehensive guide to over 100 essential Git commands for developers, covering everything from setup to advanced usage. It emphasizes the importance of understanding Git for collaboration, conflict resolution, and version control, with practical examples and interview tips throughout. The conclusion highlights that mastering Git involves not only knowing commands but also grasping workflows and problem-solving skills.

Uploaded by

dwaraknath2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Git Demystified: 100+ Essential Commands Every

Developer Must Master

Imagine this: You’re collaborating on a big project


with a global team. Code changes are flying in from
every direction. Suddenly, your codebase breaks—
conflicts, merge issues, and the dreaded "detached
HEAD" state. Sound familiar? This is where Git
becomes your best ally.
In this post, I’ll guide you through 100+ Git
commands, from beginner to advanced, with real-
world examples. Whether you’re prepping for an
interview or leveling up your skills, this cheat sheet
🚀
will make you a Git pro in no time. Let's dive in!
1. Setting Up Git: Your Identity Matters
Before you can start contributing to a project, Git
needs to know who you are:

bash

$ git config --global user.name "Your Name"


$ git config --global user.email
"[email protected]"

These commands set your name and email for all


repositories. You’ll see your identity stamped on
every commit.

🔑 Why it matters: In interviews, you might be asked,


"How does Git track authorship?" The answer lies in
this simple setup.
2. Initializing and Cloning Repositories

Want to kickstart a new project or collaborate on an


existing one? Here’s how:

bash

$ git init
Initialized empty Git repository in /project/.git/

$ git clone https://github.com/user/repository.git


Cloning into 'repository'...
git init: Initializes a new local repository.
git clone: Creates a local copy of a remote
repository.

💡 Pro Tip: Interviewers often ask about git clone. Be


ready to explain how it differs from git init when
starting a project.
3. Tracking Changes: The Power of Staging and
Committing
Here’s where Git shines—tracking changes efficiently.

bash

$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be
committed)

$ git add file1.txt


$ git commit -m "Add initial file"
[main (root-commit) 1a2b3c] Add initial file
git status: Shows the current state of your
working directory.
git add: Stages changes for the next commit.
git commit: Records the changes in the
repository.

🎯 Key Insight: Staging allows you to control what


goes into your commit, ensuring cleaner commit
histories—a point interviewers love to test!
4. Inspecting Your Repository

Need to review your commit history or check who


changed what? These commands are your go-to tools:

bash

$ git log
commit 1a2b3c Author: Your Name
<[email protected]> Date: Mon Nov 13 12:00
2024

$ git show 1a2b3c


git log: Displays commit history.
git show: Shows details of a specific commit.

👀 Interview Tip: Be ready to demonstrate how you’d


track down a bug using commit history.
5. Undoing Mistakes: Your Safety Net
Mistakes happen. Here’s how Git helps you recover:

bash

$ git reset --soft HEAD~1 # Undo last commit but


keep changes staged
$ git reset --hard HEAD~1 # Undo last commit and
discard changes
$ git checkout -- file1.txt # Discard changes in
working directory
git reset: Reverts commits.
git checkout: Restores specific files.

🔄 Real-Life Scenario: "Oops, I didn’t mean to


commit that." These commands save the day.
6. Branching Out: Work Smarter, Not Harder
Branches allow you to work on different features
independently:

bash

$ git branch feature-login


$ git checkout feature-login
Switched to branch 'feature-login'

$ git checkout -b hotfix-issue123


git branch: Lists, creates, or deletes branches.
git checkout: Switches to a specific branch.
git checkout -b: Creates and switches to a new
branch.

🌱 Best Practice: Use feature branches for new work.


It’s a standard practice interviewers love to hear
about.
7. Merging and Resolving Conflicts: A Developer’s
Daily Battle
Merging brings changes together, but sometimes
conflicts arise:

bash
$ git merge feature-login
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
Resolve conflicts manually, then:

bash

$ git add file1.txt


$ git commit -m "Resolve merge conflict"

💥 Common Pitfall: Be ready to explain conflict


resolution in interviews. Practice makes perfect!
8. Working with Remotes: Collaborate Like a Pro
When working in teams, pushing and pulling changes
is essential:

bash

$ git remote add origin


https://github.com/user/repository.git
$ git push origin main
$ git pull origin main
git remote add: Links your local repository to a
remote.
git push: Sends local commits to the remote
repository.
git pull: Fetches and merges changes from the
remote.

🌐 Interview Insight: Be prepared to discuss how Git


supports team collaboration.
9. Stashing: Save Work in Progress
Need to switch tasks but want to save your work
temporarily? Use stash:

bash
$ git stash
$ git stash list
$ git stash apply stash@{0}
git stash: Saves changes for later.
git stash apply: Restores stashed changes.

🚀 Real-World Usage: Perfect for hotfix situations


where you need to pause ongoing work.
10. Advanced Commands for Power Users
Here are some advanced commands to show off your
expertise:

bash
$ git cherry-pick <commit_hash> # Apply specific
commits
$ git rebase main # Reapply commits on top of
another base branch
$ git bisect start # Binary search for bugs

🧠 Interview Power Play: Know when to use git rebase


vs. git merge. Many candidates stumble here—don’t
be one of them!
11. Git Hooks: Automate Your Workflow

Automate tasks like pre-commit checks with Git


hooks:

bash

$ cat .git/hooks/pre-commit
#!/bin/sh
echo "Running pre-commit checks..."

🚦 Bonus Tip: Hooks can enforce coding standards,


boosting your team’s productivity.
12. GitHub Integration: Supercharge Collaboration

Combine Git with GitHub for seamless collaboration:

bash

$ git fork [repository-url] # Fork a project on GitHub


$ git pull request # Create a PR (via GitHub CLI)

🛠️ Pro Move: Contributing to open-source projects


via GitHub shows initiative—a great topic to bring up
in interviews.
Conclusion: Practice and Conquer
Mastering Git is about more than just commands. It’s
about understanding workflows, collaboration, and
problem-solving. Whether you’re acing an interview
or managing live projects, these commands will
make you a Git ninja.

You might also like