0% found this document useful (0 votes)
130 views8 pages

Git Interview Questions & Answers

This document provides a comprehensive list of Git interview questions and answers, covering fundamental concepts such as Git's functionality, repository management, branching strategies, and commands for version control. It contrasts Git with SVN, explains the purpose of various commands, and discusses best practices for using Git effectively. Additionally, it highlights differences between GitHub and Bitbucket, and offers guidance on managing remote repositories.

Uploaded by

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

Git Interview Questions & Answers

This document provides a comprehensive list of Git interview questions and answers, covering fundamental concepts such as Git's functionality, repository management, branching strategies, and commands for version control. It contrasts Git with SVN, explains the purpose of various commands, and discusses best practices for using Git effectively. Additionally, it highlights differences between GitHub and Bitbucket, and offers guidance on managing remote repositories.

Uploaded by

shobhitgupta2300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link]

com/@madhukiran_mindcircuit

Git Interview-Questions-Answers

1. What is Git?

Git is a distributed version control system used for tracking changes in source code during
software development.

[Link] makes Git a Distributed Version Control System?

Git allows developers to work locally with full version history, then push changes to a remote
repository like GitHub.

[Link] Git and SVN.

Git is distributed and allows offline work; SVN is centralized and requires internet
connectivity.

[Link] is a Git repository?

It’s a storage space where Git tracks and saves all changes made to project files.

[Link] to initialize a Git repository?

git init initializes a new Git repository.

[Link] is a bare repository?

A Git repo without a working directory, typically used as a central shared repository.

git init --bare [Link]

• This way, the bare repository acts like a GitHub or GitLab repository, but hosted in-
house.

• Since the repo is not meant for editing or working in, having a working directory is
unnecessary.

• It avoids confusion or merge conflicts caused by someone accidentally editing files in


a shared repo.

[Link] do you configure Git user details globally?

git config --global [Link] "Your Name"

git config --global [Link] "your@[Link]"

[Link] to create aliases for Git commands?

+91 7396627149 devopstraininghub@[Link]


[Link]

git config --global [Link] checkout makes git co work as git checkout.

[Link] does git clone do?

Downloads an entire remote repository to your local machine.

[Link] is the purpose of git add?

Moves changes from the working directory to the staging area.

[Link] is the staging area in Git?

An intermediate area to review and format commits before finalizing them.

[Link] to commit with both add and message in one step?

git commit -a -m "message"

[Link] to edit a commit message?

git commit --amend -m "New message"

[Link] to undo a commit but keep the changes staged?

git reset --soft HEAD~1

[Link] to remove a file from staging area?

git reset HEAD <filename>

[Link] between --soft, --mixed, and --hard reset?

• Soft: resets to commit, keeps changes staged

• Mixed: resets and unstages changes

• Hard: resets and discards changes

[Link] is HEAD in Git?

A pointer to the current branch reference.

[Link] is the purpose of .gitignore?

To exclude files from being tracked by Git.

[Link] to view the commit history?

git log

+91 7396627149 devopstraininghub@[Link]


[Link]

git log --oneline

git log --author="user"

[Link] to compare two commits?

git diff commit1 commit2

[Link] to recover a deleted file?

git checkout -- <filename>

• Note : It discards local changes in a file

[Link] to create and switch to a new branch?

git checkout -b new_branch

[Link] to rename a branch?

git branch -m old_name new_name

[Link] to delete local and remote branches?

• Local: git branch -d branch_name

• Remote: git push origin --delete branch_name

[Link] to see all local and remote branches?

git branch -a

[Link] between git fetch and git pull?

git fetch

• Downloads commits, files, and references from the remote repository into your local
repository, but does not merge them into your working directory.

• Safe to use because it doesn't change your working files.

• Ideal when you want to see what others have changed before integrating it into your
code.

git pull

• It is essentially: git fetch + git merge

+91 7396627149 devopstraininghub@[Link]


[Link]

• It fetches changes from the remote and then automatically merges them into your
current branch.

• Can cause merge conflicts if your local changes conflict with remote ones.

[Link] to fetch a remote branch without merging?

git fetch origin branch_name

[Link] to merge a branch?

Be in the target branch, then: git merge source_branch

[Link] causes a Git merge conflict?

When changes in the same line/file differ across merged branches.

[Link] to resolve merge conflicts?

Edit the file manually, then git add and git commit.

[Link] to abort a merge conflict?

git merge --abort

[Link] does git rm do?

Deletes files from working directory and stages the deletion.

33. How to check which branches have been merged into master?

git branch --merged

[Link] Git branching strategy you've used.

Feature, Task, and Release branching (explain with real project use).

[Link] is Git stash and when do you use it?

Temporarily stores changes not ready to commit. Useful when switching branches mid-work.

[Link] related to Git stash:

• Save: git stash save "msg"

• Apply: git stash apply stash@{0}

• Pop: git stash pop

+91 7396627149 devopstraininghub@[Link]


[Link]

• List: git stash list

[Link] is git rebase different from git merge?


• Merge:
- Preserves full history with a merge commit

- Combine two branches, keeping both histories

- Collaborative work, preserves all contributions


• Rebase:
- Move (replay /rebase ) your changes onto another branch

- re-applies commits in a linear fashion; cleaner history

- Rewrites history – dangerous if used on public/shared branches

38. How to revert a commit?


git revert <commit_id>

[Link] to change the remote URL?

git remote set-url origin <new_url>

[Link] is a pull request (PR)?

A request to merge changes from one branch to another, usually with a code review.

[Link] is a fast-forward merge?

A merge where the target branch pointer is simply moved forward to the latest commit.

[Link] to force push changes?

git push origin branch_name --force

[Link] to tag a specific commit?

git tag v1.0 <commit_id>

Push tags: git push origin v1.0

[Link] between annotated and lightweight tags?

Annotated has metadata and is stored in Git history. Lightweight is just a pointer.

[Link] to list and delete tags?

+91 7396627149 devopstraininghub@[Link]


[Link]

• List: git tag

• Delete: git tag -d tagname

[Link] is a detached HEAD state?

When HEAD points to a specific commit instead of a branch.

[Link] to cherry-pick a commit from another branch?

git cherry-pick <commit_id>

[Link] to rebase interactively?

git rebase -i HEAD~n to squash/reword commits.

[Link] do you lock a branch in GitHub?

In repository settings → Branches → Add branch protection rules.

[Link] to grant access to a GitHub repository?

Go to Settings → Collaborators → Invite user by GitHub username.

[Link] is the difference between git revert and git reset?

git revert

• Creates a new commit that undoes the changes made by a previous commit.

• Does NOT remove any commit from history.

• Safe to use in shared/public branches (like main or develop), because it preserves


history.

git reset

• Moves the HEAD and optionally updates the working directory and staging area.

• Can be destructive (especially with --hard), because it removes commits from history.

• Typically used in private branches or local development.

Common options:

git reset --soft : Keeps changes staged

git reset --mixed (default): Keeps changes in working directory

+91 7396627149 devopstraininghub@[Link]


[Link]

git reset --hard : Discards all changes

[Link] are the steps to push your code to a central repo (e.g., GitHub)?

Answer: git init #### Initialize local repo

git add . #### Stage changes

git commit -m "message" #### Commit changes

git remote add origin #### Link to central repo

git push -u origin main #### Push code

53. git push vs git pull ?

git push

• You use it to share your code with others.

• Only works if your local branch is ahead of the remote.

Example:

git push origin main – pushes your local main to the remote origin.

git pull

• Combines git fetch + git merge.

• It brings in changes from the remote repository into your current branch.

Example:

git pull origin main – fetches and merges changes from origin/main.

54. GitHub vs Bitbucket ?

GitHub

• A popular platform for hosting Git repositories, especially used for open-source
projects.

• Owned by Microsoft and widely adopted by individual developers and the open-source
community.

• Strong features like GitHub Actions, Copilot, and seamless integration with VS Code.

Bitbucket

+91 7396627149 devopstraininghub@[Link]


[Link]

• A Git repository hosting service designed for teams and enterprises.

• Owned by Atlassian, and integrates deeply with Jira, Trello, and Confluence.

• Offers Bitbucket Pipelines for built-in CI/CD, great for managing private team projects.

55. You have code on your local machine and have pushed it to a remote repository. Now
you want to push the same code to a different (new) remote repository. How can you
change the remote URL?

To change the remote repository URL

Check current remote URL:

git remote -v

Change the remote URL to the new repository:

git remote set-url origin <new-repo-url>

Replace with the URL of the new Git repository (e.g., from GitHub or Bitbucket).

git push origin main

(Use main or whatever your branch name is.)

git remote set-url origin [Link]

git push origin main

For More information:


[Link]
Answers/tree/main/GIT

+91 7396627149 devopstraininghub@[Link]

You might also like