Git Basics and Beyond: Your Interview Survival Guide

Roshni Wakodikar
5 min readJan 8, 2025

--

Git is an indispensable tool for developers, powering version control for projects of all sizes. This blog takes you through all the Git commands you need to ace any technical interview, with real-life scenarios and detailed explanations of each command and its options. Whether you’re using GitHub, GitLab, or any Git-based platform, this guide has you covered.

Why Git Matters

In the modern development workflow, collaboration is key. Git allows teams to work together efficiently, track changes, and maintain a clean and structured codebase. Familiarity with Git isn’t just a bonus for developers — it’s a necessity.

Getting Started with Git

1. Initialize a Repository

To start tracking changes in a project, initialize a Git repository:

git init

This command sets up Git in your local project folder, creating a .git directory that tracks changes.

Options:

  • git init --bare: Creates a bare repository for shared use, typically on a remote server.

2. Clone a Repository

Copy an existing repository to your local machine:

git clone <repository_url>

Options:

  • git clone <url> <directory>: Specify a target directory name.
  • git clone --depth=1: Clone only the latest version for faster setup.

Tracking Changes

3. Check Repository Status

View the current state of your working directory:

git status

Use this command after making changes, you want to see which files are modified, staged, or untracked.

4. Add Changes to the Staging Area

Prepare files for committing:

git add <file_name>

To stage all changes:

git add .

You’ve updated a feature and want to commit your changes.

Options:

  • git add -p: Interactively stage changes.

5. Remove Files from Staging Area

Unstage files that were accidentally added:

git reset <file_name>

Useful when you staged the wrong file and need to unstage it.

Committing Changes

6. Commit Changes

Save staged changes with a descriptive message:

git commit -m "Add feature X"

You made a typo in your last commit message and need to fix it.

Options:

  • git commit --amend: Edit the last commit message or add changes to the previous commit.
  • git commit -a -m "Message": Automatically stage and commit tracked files.

7. View Commit History

Check your repository’s commit history:

git log

You’re debugging and want to see recent changes.

Options:

  • git log --oneline: Compact display of commit hashes and messages.
  • git log --graph: Visualize branch merges.
  • git log -p: Show differences introduced in each commit.

Stashing Changes

8. Stash Uncommitted Changes

Temporarily save uncommitted changes:

git stash

Options:

  • git stash save "Message": Add a description to your stash.
  • git stash --include-untracked: Stash untracked files.

9. List Stashes

git stash list

To check the saved stashes.

10. Apply Stashed Changes

git stash apply

Options:

  • git stash pop: Apply the latest stash and remove it from the stash list.
  • git stash apply stash@{index}: Apply a specific stash.

Branching and Merging

Branch Types and Strategies:

  • Feature Branches: For developing new features.
  • Release Branches: For stabilizing a new version.
  • Hotfix Branches: For critical fixes in production.

Popular Branching Strategies:

  • Git Flow: Structured branching with develop, release, feature, and hotfix branches.
  • GitHub Flow: A simpler approach using main and short-lived feature branches.
  • Trunk-Based Development: A single branch (main) with frequent small merges.

11. Create a Branch

git branch <branch_name>

12. Switch to a Branch

git checkout <branch_name>

13. Create and Switch to a Branch

git checkout -b <branch_name>

14. List Branches

git branch

Options:

  • git branch -r: List remote branches.
  • git branch -a: List all local and remote branches.

15. Merge Branches

Combine changes from one branch into another:

git merge <branch_name>

Options:

  • git merge --squash: Merge commits into a single commit.

16. Delete a Branch

git branch -d <branch_name>

Options:

  • git branch -D <branch_name>: Force delete a branch.

Working with Remotes

17. Add a Remote

git remote add origin <url>

To link your local repository to a remote repository.

18. View Remotes

git remote -v

19. Push Changes to Remote

git push origin <branch_name>

20. Fetch Changes from Remote

Download changes without merging:

git fetch

21. Pull Changes from Remote

Fetch and merge changes:

git pull

Options:

  • git pull --rebase: Reapply your changes on top of the latest fetched changes.

Advanced Git Commands

22. Rebase

Apply commits on top of another base branch:

git rebase <branch_name>

23. Cherry-Pick

Apply a specific commit to your current branch:

git cherry-pick <commit_hash>

24. Squash Commits

Combine multiple commits into one:

git rebase -i <commit_hash>

Dealing with Deleted Branches and Commits

25. Recover Deleted Branches

git reflog

Find the commit hash of the deleted branch and recreate it:

git branch <branch_name> <commit_hash>

Resolving Conflicts

Conflicts occur when Git can’t automatically merge changes, such as:

  • Two branches modify the same line in a file differently.
  • A file is deleted in one branch but modified in another.

Steps to Resolve Conflicts:

  1. Identify Conflicts: git status
  2. Edit Conflicting Files: Manually resolve sections marked with <<<<<<, ======, >>>>>>.
  3. Mark as Resolved: git add <file_name>
  4. Continue the Merge: git commit

Deep Dive into git reset

git reset is a powerful command in Git that allows you to undo changes by moving the HEAD (the current branch pointer) to a previous commit. It is used to uncommit changes, untrack files, or discard changes, depending on how it is used.

The git reset command has three main modes:

  1. Soft Reset (--soft):
  • Keeps your changes staged in the index (staging area), so the changes are ready to be committed again.

Example: git reset --soft HEAD~1

2. Mixed Reset (--mixed):

  • Unstages the changes from the staging area (index) but keeps the changes in your working directory.
  • Default behavior of git reset (if no options are specified).

Example:git reset --mixed HEAD~1

3. Hard Reset (--hard):

  • Resets the staging area and working directory to match the specified commit, essentially discarding all local changes.

Example: git reset --hard HEAD~1

  • Hard Reset: Be cautious as it can permanently remove changes.

Conclusion

This guide covers everything from basic commands to advanced techniques, preparing you for any Git-related question in an interview. The best way to master Git is through practice. Experiment with these commands and workflows to deepen your understanding.

What’s your favorite Git trick? Share it in the comments below!

--

--

No responses yet