0% found this document useful (0 votes)
25 views5 pages

Popular Git Interview Questions and Answers

Uploaded by

hemanth-moka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Popular Git Interview Questions and Answers

Uploaded by

hemanth-moka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

2. How do you clone a repository?

Use `git clone <repository-url>` to make a copy of the target repository on your local
machine.

3. `git pull` vs `git fetch`

`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.

4. Explain the Git workflow.

The basic Git workflow involves creating branches, making changes, committing
those changes, and then merging those changes back into the main branch.

5. How do you fix a merge conflict?

Fix merge conflicts by editing the conflicted files to choose which changes to keep,
then staging and committing those changes.

6. What is a branch in Git?

A branch in Git is a separate line of development, allowing you to work on different


features or fixes independently.

7. How do you create a new branch and switch to it?

Use `git checkout -b <branch-name>` to create and switch to a new branch.


8. What is a commit in Git?

A commit represents a specific moment's capture of your repository, documenting


the modifications made to your project.

9. How do you push changes to a remote repository?

Use `git push <remote-name> <branch-name>` to send your committed changes to


a remote repository.

10. What is `git merge` and how do you use it?

`git merge <branch>` merges changes from one branch into the current branch.

11. What is a `.gitignore` file?

A `.gitignore` file identifies files that should remain untracked and be disregarded by
Git on purpose.

12. How do you revert a commit?

To create a new commit that reverses the changes introduced in a specific commit,
execute the command `git revert <commit-hash>`.

13. What is a fast-forward merge?

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.

17. What is `git stash`?

`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?

Use `git remote -v` to list all remote connections.

19. What does `git fetch` do?

`git fetch` updates your local copy of a remote branch, without merging the changes
into your current branch.

20. How do you delete a branch locally and remotely?

Locally: `git branch -d <branch-name>`, remotely: `git push <remote-name> --delete


<branch-name>`.

21. What is the purpose of `git config`?

`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.

23. What is `git log` and how do you use it?

`git log` displays the commit history. You can use various options to format the
output, such as `--oneline`, `--graph`, etc.

24. How do you find a specific commit by message?


Use `git log --grep="commit message"` to search the commit history for a specific
message.

25. What is the use of `git diff`?

`git diff` shows the differences between commits, commit and working directory, etc.

26. How do you add files to a commit?

Use `git add <file-name>` to stage a file for commit.

27. What does `git commit -m "message"` do?

It commits the staged changes to the repository with a message describing the
commit.

28. How do you update a Git repository to the latest version?

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.

30. How do you see the changes made by a specific commit?

Use `git show <commit-hash>` to display the changes made in a commit.

31. What is the significance of the HEAD pointer in Git?

HEAD points to the most recent commit on the current branch, indicating the
workspace's latest status.

32. How do you rename a local Git branch?

Use `git branch -m <old-name> <new-name>` to rename a local branch.

33. What is the purpose of `git checkout -- <file>`?


It is used to discard changes in the working directory for the specified file.

34. How do you create a tag in Git?

Use `git tag <tag-name>` to create a lightweight tag, or `git tag -a <tag-name> -m
"message"` for an annotated tag.

35. What is `git push --tags`?

It pushes all your tags to the remote repository.

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.

37. What is the use of `git rm`?

`git rm <file>` removes files from the working directory and stages the deletion.

38. How do you compare two commits?

Use `git diff <commit1> <commit2>` to see the differences between two commits.

39. What is `git rebase` and how is it different from merge?

`git rebase` rewrites the commit history by moving the branch to the tip of the target
branch, whereas merge combines two histories together.

40. How do you handle a merge conflict in Git?

Resolve the conflict manually in the affected files, mark them as resolved with `git
add`, and then complete the merge with `git commit`.

You might also like