Git Advanced
1. What is the difference between git rebase and git merge, and when should you use each?
Use git merge when you want to maintain a history of all commits. Use git rebase when you want a linear and cleaner history.
2. How do you resolve merge conflicts in Git?
Open conflicting files, manually resolve issues, use git add <file> to stage, and then commit the merge.
3. How can you recover a deleted branch in Git?
Use git reflog to find the commit hash and recreate the branch using git checkout -b <branch-name> <commit-hash>.
4. What is the purpose of git cherry-pick?
git cherry-pick <commit-hash> applies a specific commit from one branch to another without merging.
5. What is a detached HEAD state in Git?
It occurs when you check out a specific commit instead of a branch. To fix it, create a new branch or check out an existing one.
6. What is the difference between git reset, git checkout, and git revert?
- git reset moves the HEAD to a specific commit.
- git checkout switches between branches or commits.
- git revert creates a new commit that undoes a previous commit.
7. How do you squash multiple commits into one?
Use git rebase -i HEAD~n, where n is the number of commits to squash.
9. How do you create and apply Git patches?
Use git format-patch to generate patches and git apply to apply them.
9. How do you track only specific files in a repository?
Use git add -N <file> to start tracking without adding content.
10. What are Git hooks, and how can you use them?
Git hooks are scripts that execute before or after specific Git actions. They are stored in the .git/hooks directory and can automate tasks like linting, testing, or enforcing commit messages.