CodeBlog.xyz

Git 40 intermediate-level questions

September 8, 2023 | by Meir Achildiev

Git questions

Below are 40 intermediate-level questions about Git, complete with answers and examples to deepen your understanding.

1. What is Git rebase, and how does it differ from merge?

Answer: Both git rebase and git merge are used to integrate changes from one branch into another. git rebase moves a branch to a new base commit, while git merge creates a new commit that brings in changes from another branch.

Example:

  • Rebase: git rebase main
  • Merge: git merge feature

2. What is a fast-forward merge?

Answer: A fast-forward merge happens when the feature branch is directly ahead of the base branch, allowing Git to simply move the HEAD pointer forward without creating a new commit.

Example:

git checkout main
git merge feature

3. How do you squash commits?

Answer: Use git rebase -i HEAD~[n] where [n] is the number of last commits you want to squash.

Example:

git rebase -i HEAD~3

4. What is a detached HEAD?

Answer: A detached HEAD occurs when you check out a commit that is not the tip of any branch, which can be dangerous for making changes.

5. What are Git hooks?

Answer: Git hooks are scripts that run automatically before or after events like commit, push, and receive.

6. What is git bisect?

Answer: git bisect is a binary search tool that helps to find the commit that introduced a bug.

Example:

git bisect start
git bisect bad
git bisect good [good_commit]

7. How do you revert a specific file to a specific commit?

Answer: Use git checkout [commit_hash] -- [file_path].

Example:

git checkout abc123 -- my-file.txt

8. What is a remote tracking branch?

Answer: It’s a branch in your local Git repository that tracks a branch on a remote repository.

9. What is git annotate?

Answer: git annotate is similar to git blame. It shows who made each change in a file and when, line by line.

Example:

git annotate my-file.txt

10. How do you rename a branch?

Answer: Use git branch -m [old_name] [new_name].

Example:

git branch -m old-name new-name

11. How can you configure a global .gitignore?

Answer: Use git config --global core.excludesFile '~/.gitignore_global' to set a global .gitignore file.

12. What is git stash pop?

Answer: git stash pop applies stashed changes and removes them from the stash.

Example:

git stash pop

13. How do you remove a submodule?

Answer: To remove a submodule, you need to delete the relevant section from .gitmodules and .git/config, then run git rm --cached [path_to_submodule].

14. What is git clean?

Answer: git clean is used to remove untracked files and directories.

Example:

git clean -fd

15. How do you configure aliases in Git?

Answer: Use git config --global alias.[alias_name] '[git_command]'.

Example:

git config --global alias.st 'status'

16. How do you ignore changes in a tracked file temporarily?

Answer: Use git update-index --assume-unchanged [file].

Example:

git update-index --assume-unchanged config.yml

17. What is a bare repository in Git?

Answer: A bare repository is a Git repository without a working directory. It is suitable for sharing and collaborating.

18. How do you archive a repository?

Answer: Use git archive to create a tar or zip archive of a Git repository.

Example:

git archive --format=tar --output=/path/to/archive.tar HEAD

19. How do you create and apply patches in Git?

Answer: Use git format-patch to create a patch and git apply to apply a patch.

Example:

git format-patch -1
git apply 0001-Commit-Message.patch

20. What are pre-receive, update, and post-receive hooks in Git?

Answer: These are server-side hooks. Pre-receive enforces rules before updates are done, update is invoked once for each branch, and post-receive runs after the update is accepted.

21. What is git cherry?

Answer: git cherry is used to find commits yet to be applied to the upstream.

Example:

git cherry -v main feature-branch

22. What does git revert do?

Answer: git revert creates a new commit that undoes changes made in a specific commit, without rewriting history.

Example:

git revert [commit_hash]

23. What is a Git blob?

Answer: A blob (Binary Large Object) is a type of object that represents a file in Git. It stores the file data but doesn’t contain any metadata.

24. What are Git sub-trees?

Answer: Git sub-trees allow you to embed one repository inside another as a sub-directory, without using submodules.

25. How do you set up a Git proxy?

Answer: You can set up a Git proxy using the http.proxy or https.proxy configuration.

Example:

git config --global http.proxy http://proxy.example.com:8080

26. What is git reflog?

Answer: git reflog shows a log of where your HEAD and branch references have been, useful for recovering lost commits.

Example:

git reflog

27. How do you delete a local branch?

Answer: Use git branch -d [branch_name] or git branch -D [branch_name] to forcefully delete.

Example:

git branch -d feature-branch

28. How do you delete a remote branch?

Answer: Use git push [remote_name] --delete [branch_name].

Example:

git push origin --delete feature-branch

29. What is git filter-branch?

Answer: git filter-branch lets you rewrite Git revision history, such as changing email addresses, removing files, etc.

30. How do you configure different emails for different repositories?

Answer: You can use git config user.email [email] within the repository directory to set a specific email for that repo.

Example:

git config user.email "email@specific.repo"

31. What is the Git staging area?

Answer: The staging area is an intermediate area where commits can be formatted and reviewed before completing the commit.

32. What is git gc?

Answer: git gc (Garbage Collect) cleans up unnecessary files and optimizes the local repository.

Example:

git gc

33. What are symbolic references in Git?

Answer: A symbolic reference is a file that contains a string that references another reference.

34. How do you solve conflicts in Git?

Answer: Conflicts can be solved manually by editing the conflicted files, then adding and committing them.

Example:

# Edit files to resolve conflict
git add .
git commit

35. What is a tracking connection in Git?

Answer: A tracking connection links your local branch to the remote branch and helps you push, fetch, and pull changes.

36. What is git stash drop?

Answer: git stash drop removes a single stashed state from the stash list.

Example:

git stash drop

37. How do you list only the files that have changed between two commits?

Answer: Use git diff --name-only [commit1] [commit2].

Example:

git diff --name-only HEAD HEAD~1

38. What are Git notes?

Answer: Git notes are used to add metadata to Git objects like commits, without changing the objects themselves.

39. How do you edit an incorrect commit message?

Answer: Use git commit --amend.

Example:

git commit --amend -m "New commit message"

40. What is git rev-parse?

Answer: git rev-parse parses Git references and returns the corresponding SHA-1 hash.

Example:

git rev-parse HEAD

RELATED POSTS

View all

view all