The Ultimate Git Q&A Guide (Beginner to Expert)

Git is the backbone of modern software development. Whether you are a beginner just learning git init or an advanced user managing complex workflows with git rebase, this Q&A post has you covered.


1. What is core.excludesFile in Git?

Answer:
The core.excludesFile setting in Git defines the global ignore file (similar to .gitignore, but system-wide).

  • By default, .gitignore works per project.
  • core.excludesFile is useful when you want to ignore files across all repositories on your machine (e.g., .DS_Store, IDE configs, log files).

πŸ‘‰ Example:

git config --global core.excludesFile ~/.gitignore_global

Then create a file ~/.gitignore_global and add patterns like:

*.log
.DS_Store
node_modules/


2. What does git branch -f branch-name do?

Answer:
git branch -f force-moves a branch pointer to a specific commit. It’s useful when you want to reset a branch without checking it out.

πŸ‘‰ Example: Move dev branch to match main:

git branch -f dev main

Now dev points exactly where main is.

⚠️ Be careful β€” this changes commit history for that branch pointer.


3. How does git stash work?

Answer:
git stash temporarily saves your uncommitted changes (both staged and unstaged) so you can work on something else without committing.

πŸ‘‰ Example:

git stash         # saves changes
git stash list    # see all stashes
git stash pop     # reapply latest stash and remove it
git stash apply   # reapply without removing


4. Difference between git stash pop and git stash apply?

  • git stash pop β†’ applies the stash and removes it from the stash list.
  • git stash apply β†’ applies the stash but keeps it in the list.

5. How to use git apply?

Answer:
git apply applies a patch (like a diff) directly to your working directory without committing.

πŸ‘‰ Example:

git diff > fix.patch
git apply fix.patch

This is useful when you want to share changes as patches instead of pushing branches.


6. How to explore history with git log?

Answer:
git log shows commit history. Some useful options:

git log --oneline --graph --decorate --all

  • --oneline β†’ short hash & message
  • --graph β†’ ASCII branch graph
  • --decorate β†’ shows branch/tags
  • --all β†’ shows all refs

7. How to compare changes with git diff?

Answer:

  • Compare working directory vs. index (unstaged changes): git diff
  • Compare staged changes vs. last commit: git diff --cached
  • Compare two commits: git diff commit1 commit2

8. What is git merge vs. git rebase?

Answer:

  • git merge branchX β†’ merges branch history, creates a merge commit (keeps history intact).
  • git rebase branchX β†’ re-applies commits on top of branchX, creating a linear history.

πŸ‘‰ Use merge for shared branches.
πŸ‘‰ Use rebase for private branches before pushing.


9. How does git fetch differ from git pull?

  • git fetch β†’ downloads changes from remote but does not merge into your branch.
  • git pull = git fetch + git merge (or git rebase if you use --rebase).

10. How to move/rename a file with git mv?

git mv old.txt new.txt
git commit -m "Renamed file"

This keeps history intact instead of manually deleting & re-adding files.


11. What is git pull --rebase?

Answer:
It fetches changes and rebases your commits on top of the updated branch (instead of creating a merge commit).

πŸ‘‰ Example:

git pull --rebase origin main

This keeps history linear.


12. How to use git clean?

Answer:
Removes untracked files (careful ⚠️).

git clean -fd

  • -f β†’ force
  • -d β†’ remove untracked dirs

13. How to clone a repo into a subdirectory?

git clone https://github.com/user/repo.git my-subdir

This creates the repo inside my-subdir.


14. How to find who changed a line with git blame?

git blame file.txt

Shows commit hash, author, and time for each line.

πŸ‘‰ Useful with -L to blame specific lines:

git blame -L 10,20 file.txt


15. How to ignore all files except one?

In .gitignore:

*
!file.txt

This ignores everything except file.txt.


16. How to undo a merge commit?

If not pushed yet:

git reset --hard HEAD~1

If pushed, use git revert:

git revert -m 1 <merge_commit_hash>

This undoes merge without rewriting history.


17. What is git switch?

  • git switch branch β†’ switches branches.
  • git switch -c new-branch β†’ creates & switches to new branch.

πŸ‘‰ It’s a modern alternative to git checkout (for branches).


18. What are Git Hooks?

Answer:
Git hooks are scripts that run automatically on certain events (like commits, pushes).

Examples in .git/hooks/:

  • pre-commit β†’ run tests before committing
  • pre-push β†’ check code quality before pushing
  • commit-msg β†’ enforce commit message rules

πŸ‘‰ Hooks make Git workflows automated & consistent.


πŸš€ Final Thoughts

This Q&A covered everything from basic commands like git stash to advanced tricks like core.excludesFile, undoing merges, and Git hooks. Bookmark this page, and you’ll never get stuck in Git again.

πŸ‘‰ Share this with your dev friends. Every developer has been stuck with Git at least once πŸ˜‰


Leave a comment