Popular Git Interview Questions and Answers
Popular Git Interview Questions and Answers
1. What is Git?
Git is a decentralized system for version control that enables developers to monitor
and control modifications to their code repository.
Use `git clone <repository-url>` to make a copy of the target repository on your local
machine.
`git pull` updates your current branch with the latest changes from the remote, while
`git fetch` retrieves the latest changes from the remote without integrating them into
your local branch.
The basic Git workflow involves creating branches, making changes, committing
those changes, and then merging those changes back into the main branch.
Fix merge conflicts by editing the conflicted files to choose which changes to keep,
then staging and committing those changes.
`git merge <branch>` merges changes from one branch into the current branch.
A `.gitignore` file identifies files that should remain untracked and be disregarded by
Git on purpose.
To create a new commit that reverses the changes introduced in a specific commit,
execute the command `git revert <commit-hash>`.
A fast-forward merge happens when the target branch's head is behind the merged
branch's head, allowing the target branch to fast-forward to the tip of the merged
branch.
14. How do you change a commit message that you have already pushed?
Use `git commit --amend` to change the last commit message, then use `git push --
force` to update the remote repository.
15. What is the difference between `git checkout`, `git reset`, and `git
revert`?
`git checkout` switches branches or restores working tree files, `git reset` changes
the head to a specific state, and `git revert` undoes changes by creating a new
commit.
16. How do you squash commits?
Use `git rebase -i` and then choose to squash the commits in the interactive prompt.
`git stash` temporarily shelves changes so you can work on a different branch with a
clean working directory.
18. How do you list all the remote connections for a repository?
`git fetch` updates your local copy of a remote branch, without merging the changes
into your current branch.
`git config` is used to set configuration options for your Git installation, such as user
name and email.
22. How do you list all the branches that are merged into the current
branch?
Use `git branch --merged` to list branches merged into the current branch.
`git log` displays the commit history. You can use various options to format the
output, such as `--oneline`, `--graph`, etc.
`git diff` shows the differences between commits, commit and working directory, etc.
It commits the staged changes to the repository with a message describing the
commit.
Execute the command `git pull` to retrieve and integrate updates from the remote
repository into your local branch.
29. What is `git branch -d` and how do you use it?
`git branch -d <branch-name>` deletes a local branch, if it has been fully merged in
its upstream branch.
HEAD points to the most recent commit on the current branch, indicating the
workspace's latest status.
Use `git tag <tag-name>` to create a lightweight tag, or `git tag -a <tag-name> -m
"message"` for an annotated tag.
36. How do you revert to a previous commit without losing the changes
made since?
Use `git revert <commit-hash>` for each commit you want to revert. This creates new
commits that reverse the changes.
`git rm <file>` removes files from the working directory and stages the deletion.
Use `git diff <commit1> <commit2>` to see the differences between two commits.
`git rebase` rewrites the commit history by moving the branch to the tip of the target
branch, whereas merge combines two histories together.
Resolve the conflict manually in the affected files, mark them as resolved with `git
add`, and then complete the merge with `git commit`.