Git Basic Interview Questions and Answers
1. What is Git ?
Ans: Git is a distributed version control system (DVCS) used for tracking changes in source code during software development
It helps multiple people work on the same code base and is a cornerstone of modern software development, allowing teams to collaborate, roll back changes, and manage code branches. Git is known for its branching capabilities, which allow multiple versions of a code base to exist simultaneously, facilitating parallel development, experimentation, and feature isolation.
2. Why git is used?
Ans:
1.Version Control: Git allows you to track changes in your code base over time, creating a history of all changes made to your project. This can be useful for understanding how the code has evolved, reverting to earlier versions if necessary, and identifying when and why specific changes were made.
2. Collaboration: Git enables multiple developers to work on the same project concurrently. It allows team members to work independently on different parts of the codebase, with the ability to merge their changes together later.
3. Branching and Merging: Git supports branching and merging, which makes it easier to manage parallel development efforts. Developers can create branches to work on new features or bug fixes without affecting the main development line. Branches can be merged back into the main codebase once the work is complete.
4. Staging and Committing Changes: With Git, you can stage changes in your codebase before committing them, allowing you to review your changes before making them permanent. This helps ensure that only the intended changes are committed.
5. Distributed Development: Git is a distributed version control system, meaning that each developer has their own copy of the repository. This allows developers to work offline and share their changes later, which can be useful in situations where internet connectivity is limited.
6. Open Source: Git is open source, meaning that the source code is freely available for anyone to use, modify, and distribute. This has helped to make Git one of the most widely used version control systems in the world.
3. What is the features of git?
Ans: Below are some features of git:
- Distributed Version control
- Branching and merging
- Commit history and revisions
- Staging Area
- Fast and Efficient
- Security and integrity
- Open source and Large Community
- Compatibility
5. What is diff between Git and Github?
Ans:
Features | Git | Github |
Definition | A distributed version control system | A web-based platform built around Git |
Access | Command-line tool | Web Based Interface |
Where it’s used | Typically on local machines | Accessed via the web, from anywhere with an internet connection |
Key Features | Version control, branching and merging | Web-based collaboration tools, issue tracking, pull requests |
Hosting | Local or on a private server | Hosted on the GitHub platform |
Collaboration | Allows collaboration via local network | Enables remote collaboration with team members across the globe |
Uses | Used for tracking changes in source code | Used for hosting Git repositories, facilitating collaboration and sharing |
Ownership | Owned by Linus Torvalds | Owned by Microsoft |
6.What is centraliazed Version control system?
Ans: In this system, there is a central repository that stores the entire version history of a project. Developers check out files from this central repository to work on them and then check them back in when they are done.
Some features are as follows:
- Centralized Repository: There is a single central repository that stores all versions of files and directories.
- Client-Server Model: Developers interact with the central repository through a client-server architecture. They check out files from the central repository to their local machine for editing and then check them back in.
- Dependency on Central Server: Developers need access to the central server to perform most version control operations, such as checking out files, committing changes, and viewing version history.
7. What is Distributed Version control system?
Ans: In this system, every developer has a complete copy of the repository on their local machine, including the full history of the project. Developers can work independently on their local copies and then synchronize their changes with other repositories as needed.
Some features are as follows:
- Distributed Repository: Every developer has a full copy of the repository, including the complete version history, on their local machine.
- Peer-to-Peer Model: Developers can work independently on their local copies without needing constant access to a central server. They can commit changes, view version history, create branches, and perform other version control operations locally.
- Flexible Collaboration: Developers can synchronize their changes with other repositories using push and pull operations. This allows for flexible collaboration, even in scenarios where network connectivity is limited or the central server is unavailable.
8. What is difference between centralized version control system and distributed version control system?
Ans:
Sr No | Aspect | Centralized VCS | Distributed VCS |
1 | Repository Location | Single central repository | Each developer has a complete copy of the repository |
2 | Network Dependency | Requires constant connection to central server | Does not require constant network connection |
3 | Offline Work | Limited offline capability | Full offline capability |
4 | Repository Access | Access through client-server architecture | Access through local copy of the repository |
5 | Commit Operations | Commits directly to the central repository | Commits to local repository first, then syncs with others |
6 | Version History | Central repository stores entire version history | Each repository contains full version history |
7 | Branching and Merging | Usually centralized, managed by the central server | Distributed, managed locally and merged as needed |
8 | Collaboration Flexibility | Limited flexibility without access to central server | High flexibility, each developer has full repository |
9 | Scalability | Central server can become a bottleneck as projects grow | Highly scalable, no single point of failure |
10 | Examples | CVS, SVN | Git, Mercurial, Bazaar |
9. What is pull request in the git?
Ans: A pull request serves as a mechanism for contributing to a project, typically utilized in projects hosted on GitHub. In this process, a developer implements modifications within their own branch, uploads these changes to a repository, and then initiates a pull request. This action prompts the project’s maintainers to examine the proposed changes, engage in discussions about possible adjustments, and ultimately integrate the pull request into the primary branch.
10. What is a repository in Git?
Ans: refers to a central location where a project’s files and revision history are stored. It’s essentially a directory or folder on your local machine or a remote server that contains all the files and directories related to a specific project, along with metadata such as commit history, branches, tags, and more.
11. How do you add a file to the staging area?
Ans: To add a file to the staging area in Git, you use the git add
command followed by the name of the file or directory you want to stage. Here’s the basic syntax:
git add .
12. What are the types of git repository?
Ans: There are two main types of repositories in Git:
Local Repository: This is a repository stored on your local machine. When you initialize a Git repository in a directory using the git init
command, Git creates a hidden .git
folder within that directory. This .git
folder contains all the information necessary for version control, such as the commit history, configuration settings, and references to other branches.
Remote Repository: This is a repository hosted on a remote server, such as GitHub, GitLab, Bitbucket, or a private server. Developers use remote repositories to collaborate with others by pushing and pulling changes between their local repositories and the remote repository. Remote repositories serve as a centralized location where multiple developers can contribute to a project, share code, and track changes.
12. How does git work?
Ans: Git works by taking snapshots of a project’s files. Unlike other version control systems, Git records the entire contents of each file and its changes every time a commit is made. This makes operations like branching, merging, and reverting changes more efficient.
13. What is a remote in Git?
Ans: In Git, a “remote” refers to a repository that is hosted on a separate server or location from your local repository. Remotes are essentially pointers to the location of other copies of the repository, typically stored on services like GitHub, GitLab, Bitbucket, or on another server accessible via SSH or HTTP.
Git Intermediate Interview Questions and Answers
1. What is a remote in Git?
Ans: In Git, remote is a repository hosted on a server or another location, separate from your local repository. Remotes serve as a means of collaboration and synchronization between multiple developers working on the same project. Typically, a remote repository is used to share changes and collaborate with others.
2. How do you rename a Git branch?
Ans: To rename the git branch:
git branch -m <new_name_name>
To rename a different branch:
git branch -m <old_branch_name> <new_branch_name>
3. What is Branching in Git?
Ans: Branching in Git refers to the practice of diverging from the main line of development to work on a new feature, bug fix, or experiment without affecting the main code base.
It allows developers to isolate changes, work on multiple tasks simultaneously, and collaborate effectively. Each branch represents a separate line of development, with its own set of commits and version history.
4. What is diff between git fetch
and git pull
?
Ans: The main difference is that git fetch
leaves your working directory unchanged, allowing you to review the changes before merging, while git pull
automatically merges the changes.
The difference are explained below in the detail
1.Git Fetch:
This command downloads the latest changes from the remote repository, but it does not automatically merge these changes into your local branch. Instead, it updates your local copy of the remote branches (e.g., origin/main
) and leaves your working directory unchanged. This allows you to review the changes before deciding whether to merge them into your local branch.
git fetch
: Retrieves the latest updates from the remote repository.
git fetch origin
: Retrieves updates from the remote repository named “origin”.
git fetch --all
: Retrieves updates from all remote repositories.
2. Git pull: This command downloads the latest changes from the remote repository and merges them into your local branch. It’s essentially a combination of git fetch
followed by git merge
. If there are conflicts, you’ll need to resolve them manually.
git pull
: Fetches the latest updates from the remote repository and merges them into your local branch.
git pull origin main
: Fetches updates from the remote branch “main” of the remote repository named “origin” and merges them into your local branch.
5. How do you revert a commit that has already been pushed and made public?
Ans: Reverting a commit that has already been pushed and made public involves a bit more caution because you don’t want to disrupt the work of other collaborators who might have pulled the changes. Here’s a step-by-step guide on how to do it safely:
1.Identify the commit to revert: First, you need to identify the commit you want to revert. You can use git log
to find the commit hash or any other way you prefer to find it.
git log
2. Create a new revert commit: Instead of directly removing the commit, Git provides a safer way to undo changes by creating a new commit that undoes the changes introduced by the commit you want to revert. Use the git revert
command followed by the commit hash you want to revert:
git revert <commit-hash>
3. Resolve any conflicts (if necessary): Sometimes, Git might not be able to automatically revert the changes and will prompt you to resolve any conflicts manually. You’ll need to resolve these conflicts and commit the changes.
4. Push the revert commit: After resolving any conflicts and making sure everything looks good, you can push the revert commit to the remote repository:
git push origin <branch-name>
6. What is a .gitignore
file?
Ans: A .gitignore
file is a text file used by git to specify intentionally untracked files that git should ignore. These files are typically used to prevent certain files or directories from being tracked by Git, particularly files that are generated during the build process, log files, temporary files, or any other files that are not meant to be version-controlled.
7. What is git commit
?
Ans: commit represents a snapshot of the project at a specific point in time. It captures the changes made to files since the last commit and includes a commit message describing the modifications.
Commits are permanent and immutable, forming a version history of the project. They enable developers to track changes, revert to previous states, and collaborate effectively on code bases.
8. What is git stash
?
Ans: This command used to temporarily store changes that are not ready to be committed, allowing you to switch branches or perform other operations without committing incomplete work. It’s particularly useful when you’re working on a task and need to switch to another branch to address an urgent issue or to work on a different feature.
9. How git stash
works?
Ans:
1.Uncommitted Changes: When you’re working on a project in Git and have made changes to tracked files (files that are already being version-controlled), but you’re not yet ready to commit them, you’re in a situation where you might want to use git stash
.
2. Running git stash
: When you run the command git stash
, Git takes all your modified tracked files that are currently in your working directory and staged area, and temporarily saves them away. It effectively reverts your working directory and staged area to the state it was in at the last commit.
3. Stash Stack: Git maintains a stack of stashes. Each time you run git stash
, your changes are pushed onto this stack. You can stash multiple times, and each stash is added to the top of the stack.
4. Clean Working Directory: After running git stash
, your working directory is now clean, meaning it matches the state of your last commit.
5. Switching Contexts: With your changes stashed away, you’re free to switch branches, pull changes from a remote repository, or do whatever else you need to do without worrying about these changes interfering.
6. Retrieving Stashed Changes: When you’re ready to return to the changes you stashed, you can apply them back to your working directory. You have two main options:
git stash apply
: This command applies the most recent stash to your working directory without removing it from the stash stack. You can apply multiple stashes in sequence.git stash pop
: This command applies the most recent stash and removes it from the stash stack. It’s like applying the stash and immediately dropping it from the stack.
7. Resolved Conflicts (if any): If applying the stashed changes results in conflicts with the current state of your repository, Git will notify you. You’ll need to resolve these conflicts manually, just as you would with a regular merge conflict.
8. Continuing Work: Once you’ve retrieved your stashed changes, you can continue working on them, make further modifications, and eventually commit them when you’re ready.
10. How do you view the commit history?
Ans:
1.`git pull`: This will display a list of commits in reverse chronological order (newest commits first), including information such as the commit hash, author, date, and commit message for each commit.
git pull
2. If you want to view a summarized version of the commit history, you can add some options to the git log
command.
you can use --oneline
to display each commit on a single line
git log --oneline
You can use --graph
to show a graphical representation of the commit history.
git log --graph
11. How do you create a new branch?
Ans:
To create the new branch:
git branch testing
To create the new branch and switch instantly:
git checkout -b testing
12. What is git merge --squash
?
Ans: The git merge --squash
command is used to condense all the changes from a feature branch into a single commit when merging it into another branch. This is particularly useful when you want to keep a clean and concise commit history, especially for feature branches that contain multiple smaller commits.
13. What is git rebase
?
Ans: git rebase
is a Git command used to reapply a sequence of commits on top of another branch. It’s primarily used to incorporate changes from one branch into another in a more linear fashion, often to maintain a clean and organized commit history.
14. What is git merge
?
Ans: git merge
is commonly used for integrating changes from feature branches into the main development branch (main
or master
). It creates a merge commit to track the history of when and where branches were merged, making it useful for collaboration and maintaining a clear history of changes in your Git repository.
15. What is the difference between git merge and
git rebase
?
Ans:
Feature | git merge | git rebase |
Purpose | Integrates changes from one branch into another, creating a merge commit. | Integrates changes from one branch into another by reapplying commits on top of another branch. |
Commit History | Preserves the commit history of both branches, creating a new merge commit. | Rewrites the commit history, resulting in a linear history without additional merge commits. |
Branch Visualization | Branches remain distinct and retain their original branching structure. | Branches are linearized, making it appear as if the commits were made directly on top of the base branch. |
Conflict Resolution | May result in merge conflicts, which need to be resolved before completing the merge. | May result in conflicts during the reapplication of commits. Conflicts need to be resolved at each commit during the rebase process. |
Use Cases | Best suited for integrating changes from feature branches into the main development branch. | Best suited for keeping a clean and linear commit history, especially for feature branches before merging into the main branch. |
Collaboration | Maintains a more explicit record of when and where branches were merged. | Results in a cleaner commit history, but may require force-pushing if changes have already been pushed to a remote repository. |
Safety | Less risky in terms of potentially overwriting history, as it preserves existing commits. | Potentially riskier, as it rewrites commit history, which could lead to data loss if not used correctly. |
16. What is git push
?
Ans: git push
is used to upload local repository content to a remote repository. It transfers commits from your local repo to the remote.
17. What does git reset
do?
Ans: git reset
is a Git command used to move the HEAD pointer to a specific commit, undoing changes in the working directory and staging area.
18. Explain the use cases for git reset
?
Ans:
1.Soft Reset (git reset --soft
): Moves the HEAD pointer to a specific commit, leaving changes in the working directory and staging area. This option is often used to undo the last commit while keeping the changes staged for a new commit
git reset --soft HEAD~1
2. Mixed Reset (git reset --mixed
): Moves the HEAD pointer to a specific commit and resets the staging area to match that commit, but leaves the changes in the working directory. This option is useful for uncommitting changes and keeping them for further modifications before committing again.
git reset --mixed HEAD~1
3. Hard Reset (git reset --hard
): Moves the HEAD pointer to a specific commit and resets both the staging area and the working directory to match that commit, discarding any changes that are not committed. This option is more aggressive and can result in the loss of uncommitted changes.
git reset --hard HEAD~1
19. What is git diff
?
Ans: git diff
is a Git command that shows the differences between various Git objects such as commits, branches, files, and the working directory. It’s a powerful tool for understanding changes made to your codebase and for tracking modifications over time.
20. List down various use cases of git diff
?
Ans:
1.Comparing Files: You can use git diff
to compare the differences between two versions of a file.
git diff <commit1> <commit2> <file-path>
This command shows the differences between the specified versions of the file.
2. Viewing Changes in the Working Directory: If you’ve made changes to files in your working directory but haven’t yet staged them, you can use git diff
to see the differences between the current state of the files and the last committed state
git diff
3. Comparing Branches: You can use git diff
to compare the differences between two branches.
git diff <branch1> <branch2>
4. Viewing Changes in a Specific Commit: You can use git diff
to view the changes introduced by a specific commit.
git diff <commit>
5. Viewing Staged Changes: You can also use git diff
to view the changes that have been staged but not yet committed.
git diff --staged
21. How do you ignore changes in a tracked file?
Ans: Use `git update-index –assume-unchanged <file>` to ignore changes in a tracked file temporarily.
git update-index --assume-unchanged <file>
Git Advanced Interview Questions and Answers
1. What is the HEAD in Git?
Ans: HEAD
is a reference to the currently checked out commit in your repository. It’s essentially a pointer to the tip of the current branch, indicating the commit where your working directory is based on.
2. What is conflict in git?
Ans: git conflict occurs when there are conflicting changes between different branches or commits that Git cannot automatically resolve.
3. How do you resolve a merge conflict?
Ans: Resolving a merge conflict in Git involves manually addressing the conflicting changes in your files and then finalizing the merge.
1. Identify the Conflict: When you attempt to merge two branches and Git encounters conflicting changes, it pauses the merge process and notifies you that there’s a conflict.
2. Locate the Conflicted Files: Git will mark the conflicted files in your working directory. Inside these files, you’ll find conflict markers indicating the conflicting changes. These markers typically look like this:
<<<<<<< HEAD
// Changes from the current branch (HEAD)
=======
// Changes from the branch being merged
>>>>>>> branch-name
The section between <<<<<<< HEAD
and =======
represents the changes in your current branch (HEAD
), while the section between =======
and >>>>>>> branch-name
represents the changes from the branch you’re trying to merge (branch-name
).
3. Manually Resolve the Conflict: Open the conflicted files in your text editor and manually edit them to remove the conflict markers and choose which changes to keep. You may want to keep changes from one branch, the other branch, or a combination of both.
4. Mark as Resolved: After resolving the conflicts in the file, save the changes and mark the conflict as resolved by removing the conflict markers and any extraneous lines added by Git.
5. Add the Resolved Files: Once you’ve resolved the conflicts in all the conflicted files, stage them for commit using git add <file>
.
6. Commit the Changes: After staging the resolved files, commit the changes using git commit
. Git will automatically generate a merge commit message, or you can provide your own.
7. Complete the Merge: After committing the changes, you can complete the merge process by running git merge --continue
. This tells Git that you’ve resolved the conflicts and it can proceed with finalizing the merge.
8. Verify: Finally, verify that the merge was successful by testing your code and ensuring that everything works as expected.
4. How do you remove a file from Git but not delete it from your file system?
Ans: To remove a file from Git without deleting it from your file system, you can use the `git rm --cached
` command. This command removes the file from the Git repository’s staging area (index), but keeps it in your working directory. Here’s the basic syntax:
git rm --cached <file>
5. How do you change the last commit?
Ans: We can use use git commit --amend
to modify the most recent commit.
6. How do you delete a branch?
Ans:To delete a branch in Git, you can use the git branch
command with the -d
or -D
option followed by the name of the branch you want to delete.
1.Deleting the branch: You can use below command to delete the branch
git branch -d feature-branch
2. Forcing Deletion (Unsafe):If the branch you want to delete has not been merged yet and you’re sure you want to delete it (perhaps it’s a feature branch that you no longer need), you can force-delete it using the -D
option:
git branch -D feature-branch
This option will force-delete the branch regardless of its merge status.
3. Delete a Remote Branch: If you want to delete a branch on a remote repository, you can use the git push
command with the --delete
or -d
option:
git push origin --delete feature-branch
7. What is git checkout
?
Ans: git checkout
is a Git command used to switch between different branches in a git repository, as well as to manipulate the state of the working directory and the staging area.
8. What is a fast-forward merge in Git?
Ans: A fast-forward merge in Git occurs when the commit at the tip of the branch being merged in is a direct descendant of the commit at the tip of the branch being merged into. In other words, there is a linear path from the current branch’s commit to the commit being merged, without any divergent history.
9. List down various use cases of git checkout
command?
Ans:
- Switch branches
- Create and switch to new branches
- checkout specific files or commits
- Discard changes
- Dettach HEAD
1. Switch Branches: The primary use of git checkout
is to switch between branches in your repository. You can use it to move your HEAD
pointer to a different branch, effectively checking out that branch and updating your working directory and staging area to reflect the state of that branch
git checkout <branch-name>
2. Create and Switch to New Branches: You can use git checkout
with the -b
option to create a new branch and switch to it in a single step.
git checkout -b <new-branch-name>
3. Checkout Specific Files or Commits: You can use git checkout
to restore files or directories to a previous state, either from a specific commit or from another branch.
git checkout <commit-hash> <file-path>
4. Discard Changes: You can also use git checkout
to discard changes made to files in your working directory.
git checkout -- <file-path>
This command discards changes made to the specified file and restores it to the state it was in at the last commit.
5. Detach HEAD
: You can also use git checkout
to move HEAD
to a specific commit, effectively detaching it from any branch. This is useful for inspecting historical commits or for creating a new branch from an older commit.
10. What is git stash?
Ans: git stash
is a Git command used to temporarily store changes in your working directory that are not yet ready to be committed. It allows you to save your current modifications, revert them to the last commit, and work on something else without committing half-done work.
11. What is the difference between git stash pop
and git stash apply
?
Ans:
Sr No | Feature | git stash pop | git stash apply |
1 | Effect on Stash Stack | Removes the most recent stash after applying changes. | Leaves the most recent stash intact after applying changes. |
2 | Command Combination | Equivalent to git stash apply followed by git stash drop . | Applies changes from the stash without removing it. |
3 | Usability | Convenient if you don’t need to keep the stash around. | Useful if you want to keep the stash for further use. |
4 | Risk of Data Loss | Higher risk, as the stash is deleted automatically. | Lower risk, as the stash remains available for reuse. |
12. How do you switch branches in Git?
Ans:
git checkout <branch_name>
13. How do you create an empty commit?
Ans: Below is the command to create an empty commit:
git commit --allow-empty
14. What is branching strategy?
Ans: Git branching strategies are methodologies used to manage branches in a Git repository effectively. They help to organize the workflow, improve collaboration among team members, and ensure smooth integration of code. Here are some commonly used Git branching strategies:
Main Branches:
master
: The main branch containing production-ready code.develop
: The branch where the latest development changes are integrated.
Supporting Branches:
feature/*
: Used for developing new features. Branch off fromdevelop
and merge back intodevelop
.release/*
: Used to prepare for a new production release. Branch off fromdevelop
, perform testing and bug fixing, then merge into bothmaster
anddevelop
.hotfix/*
: Used for emergency fixes in themaster
branch. Branch off frommaster
, apply the fix, then merge into bothmaster
anddevelop
.
15. What is the command to discard the changes in the working directory?
Ans: To discard changes in the working directory in Git, you can use the git restore
command. Here are the commands to discard different types of changes:
git restore <file_name> # Discard the changes in specific filename
git restore . # Discard changed in all files
git checkout -- <folder_name> # discard changes in the specific folder
git checkout -- . # discard changes in all files
These above commands will discard the changes made to respective directory or files.
.
You have made some really good points there. I looked on the internet
to find out more about the issue and found most people will go along with your
views on this website.
Thanks for your comment, I am continously adding new blogs related to AWS and Devops related contents.