Interview

15 Git Commands Interview Questions and Answers

Prepare for your interview with this guide on Git commands, featuring common questions and answers to enhance your version control skills.

Git is an essential tool for version control in modern software development. It allows developers to track changes, collaborate seamlessly, and manage codebases efficiently. Mastery of Git commands is crucial for maintaining project integrity and ensuring smooth teamwork, making it a highly sought-after skill in the tech industry.

This article offers a curated selection of Git command questions and answers to help you prepare for your upcoming interview. By familiarizing yourself with these scenarios, you’ll be better equipped to demonstrate your proficiency and problem-solving abilities in version control systems.

Git Commands Interview Questions and Answers

1. Explain the purpose of git init and when you would use it.

The git init command creates a new Git repository by setting up necessary files and directories, including a .git directory. Use it when starting a new project, version controlling an existing project, or creating a bare repository for remote use.

2. What does git commit -m "message" do?

The command git commit -m "message" saves changes to the local repository with a commit message. The -m flag allows you to include this message directly from the command line, describing the changes made. This records the staged changes in the repository, creating a new commit object with metadata like the author, timestamp, and message.

3. How do you switch between branches?

To switch between branches, use git checkout followed by the branch name. In newer Git versions, git switch is available for this purpose.

Example:

git checkout feature-branch

or

git switch feature-branch

4. What is the purpose of git merge?

The git merge command integrates changes from different branches. It combines branches into a single branch, useful in collaborative projects. There are two types of merges: fast-forward, which moves the branch pointer forward, and three-way, which creates a new commit combining changes from both branches.

5. What does git rebase do?

The git rebase command integrates changes from one branch into another by moving or “replaying” commits from the current branch onto the target branch, resulting in a linear project history. For example, rebasing a feature branch onto the main branch makes it appear as if the feature branch was created from the latest commit on the main branch.

6. How can you undo the last commit?

To undo the last commit, use git reset if it hasn’t been pushed. --soft moves changes back to the staging area, while --hard discards them. If the commit is pushed, use git revert to create a new commit that undoes the changes.

git reset --soft HEAD~1
git reset --hard HEAD~1
git revert HEAD

7. How do you delete a branch both locally and remotely?

To delete a branch locally, use git branch -d or -D to force delete. To delete a remote branch, use git push --delete.

git branch -d branch_name
git branch -D branch_name
git push origin --delete branch_name

8. What is the purpose of git stash and how do you use it?

The git stash command saves the current state of your working directory and index without committing them, allowing you to switch branches or perform other tasks. You can apply stashed changes back to your working directory when ready.

Example:

git stash
git stash list
git stash apply
git stash apply stash@{1}
git stash drop stash@{1}
git stash pop

9. How can you view the differences between two commits?

To view differences between two commits, use git diff with the commit hashes. The --name-only option lists only the names of changed files.

git diff <commit1> <commit2>
git diff --name-only <commit1> <commit2>

10. What is the purpose of git cherry-pick?

The git cherry-pick command applies changes from a specific commit to the current branch, useful for selectively integrating changes without a full merge.

git cherry-pick <commit-hash>

11. How do you configure Git to ignore certain files?

To ignore certain files, use a .gitignore file in the root directory. List files and directories to be ignored.

Example:

*.log
node_modules/
temp/

12. Explain the purpose of git reset and its different modes.

The git reset command undoes changes in a repository. It has three modes: --soft moves HEAD to a commit without changing the index or working directory, --mixed resets the index but not the working directory, and --hard resets both, discarding changes.

13. What does git log do and how can you customize its output?

The git log command shows commit history. Customize output with options like --oneline, --graph, --decorate, --stat, and --pretty=format:.

Example:

git log --oneline --graph --decorate --stat

14. What is the difference between git rebase and git merge?

git rebase and git merge both integrate changes from one branch into another. git merge creates a new commit preserving both branches’ histories, while git rebase rewrites the commit history for a linear appearance.

Example of git merge:

git checkout main
git merge feature-branch

Example of git rebase:

git checkout feature-branch
git rebase main

15. How do you revert a commit that has already been pushed to a remote repository?

To revert a pushed commit, use git revert to create a new commit that undoes the changes, maintaining the commit history.

Example:

git log
git revert <commit-hash>
git push origin main
Previous

10 Data Pipelines Interview Questions and Answers

Back to Interview
Next

10 Java Memory Interview Questions and Answers