Git Interview Questions ?





1) What is the difference between Git and GitHub?

Answer: Git is a version control system of distributed nature that is used to track changes in source code during software development. It is used for coordinating work among developers in project. It can also be used to track changes in any set of files. The main objectives of Git are speed, data integrity, and support for distributed, non-linear workflows. GitHub is a Git repository hosting service, plus it adds many of its own features. GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, basic task management tools for every project.

2) What are the benefits of using Version Control System?

Answer
  • a) With the Version Control System(VCS), all the team members can work freely on any file at any time. VCS gives the flexibility to merge all the changes into a common location. 
  •  All the previous versions and variants are neatly packed up inside the VCS. We can request any version at any time as per our requirement and we will have a snapshot of the complete project right at hand. 
  • Whenever we save a new version of our project, our VCS requires a short description of the changes that we have made. Additionally, we can see what changes are made in the file’s content. This helps us to know what changes have been made in the project and by whom and when those changes were made. 
  • A distributed VCS like Git allows all the team members to have a complete history of the project so if there is a breakdown in the central server you can use any of your teammate’s local Git repository
3) Explain GIT commands ?

Answer:
  • git clone: This command is used to create a copy of a repository (usually hosted on a remote server) onto your local machine. 
For example:
git clone <repository_URL>
  • git status: This command shows the current status of your working directory and staging area. It displays which files are modified, which are staged for commit, and which are untracked.
  • git add: This command is used to add changes in the working directory to the staging area in preparation for a commit. 
There are several ways to use git add: 
git add <file>: Add a specific file.
git add . or git add --all: Add all changes in the working directory.
git add -u: Add all modified (but not untracked) files.
  • git commit: This command records the changes made to the files in the repository. The -m flag allows you to add a message describing the changes in the commit. 
For example:
git commit -m "Added new feature"
  • git push: This command is used to upload local repository content to a remote repository. It's typically used to publish local changes to a central repository. 
For example:
git push origin master :This command pushes the commits in your local master branch to the origin remote repository.

  • git pull: This command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. It's often used to fetch changes from a remote repository and merge them into the current branch. 
For example:
git pull origin master :This command fetches changes from the master branch of the origin remote repository and merges them into your current branch.
  • git log: This command displays a history of commits in the repository. It shows the commit history with details such as commit author, date, and commit message.
  • git rm: This command is used to remove files from the index and the working directory so they will no longer be tracked. 
For example:
git rm <file>
  • git checkout: This command is used to switch branches or restore working tree files. 
For example: 
git checkout <branch_name>: Switch to a different branch.
git checkout -- <file>: Discard changes in the working directory and restore the file to its state in the last commit.
  • git fetch: This command is used to retrieve changes from a remote repository without merging them into the current branch. It's useful for seeing what other developers have been working on without affecting your local branch.
  • git branch: This command is used to list, create, or delete branches. It can also be used to switch between branches. 
For example: 
git branch: List all existing branches.
git branch <branch_name>: Create a new branch.
git branch -d <branch_name>: Delete a branch.

4) What is Git Repository ?

Answer: Git Repository is a place where we will store/maintain our project source code.
  • Public Repository (Every body can see, we choose who can commit) 
  • Private Repository (We choose who can see and who can commit) 
Note: For every project we will create one git repository.

5) What is a clone in GitHub?


Answer: Cloning in a GitHub repository is nothing but taking project from git hub central repository to our local repository.

git clone

6) What are the commands that you use to write a commit message?

Answer:

git commit -m 'msg' is used to write commit message

Note: Commit message represents why we are committing this change to git repository

7) Explain briefly about GIT stash?


Answer: GIT stash is used when there is a need of storing the current changes available in working tree to temporary space and work on other changes due to priority. Once priority changes completed then user can get old changes back from temporary space.

8) What is the purpose of GIT status ?

Answer: git status command is used to list down stated and un-staged files in working tree

9) What is the difference between staged and un-staged file ?

Answer: staged file means it is eligible for commit un-staged file means which is modified in local but not eligible for commit.
Note: To commit changes we have to stage it using git add

10) What are the advantages of using the GitHub repository?


Answer: The following are the few advantages of using the GitHub repository;
  • Provides high availability
  • Easy Collaboration tool
  • Distributed version control
  • Quality open source project
  • Security, Git is designed specially to maintain the integrity of source code
11) What are the languages used in GitHub?

Answer: GitHub is a fast tool and ‘C’ language makes this possible by reducing the overhead of runtime associates with the higher programming
language.

12) What is the role of “Git push”?


Answer: The “Git push” command is used for pushing changes from local repository to central repository.

13) What is the role of “Git pull"?

Answer: The “Git pull command is used for taking latest changes from central repository to local repository

14) How to un-stage the file which is in staging area?

Answer: Using git reset command we can un-stage the file.

15) What is commit-id in git ?

Answer: When we do commit in git it will generate one unique id with 40 characters length to differentiate one commit with another commit.

Using commit-id we can identify what all changes committed to git repository.

16) How to remove a file from git repository ?

Answer: To remove a file from git repository we can use "git rm" command.

17) What are branches in git hub?

Answer
: Git branches are used to maintain multiple code bases in the project like 'develop', 'qa', 'uat', 'release', 'playground' etc.
  • 'master' branch is the main & default branch in git repository
  • 'develop' branch is used for all developers code integration
  • 'qa' branch used for providing code for system integration testing (SIT)
  • 'uat' branch used for providing code for user acceptance testing (UAT)
  • 'release' branch used for production release
  • 'playground' branch used for experiments

18) What is branch locking?

Answer: When we release code to SIT or UAT or PROD we will lock the branch so that nobody can commit changes to that branch so that existing functionality or code will not be disturbed.

19) What is branch merging ?

Answer: Merging changes from one branch to another branch is called as branch merging. For example we have done changes in develop branch and tested then we can merge the code from develop branch to master branch.

Note
: We will use "pull request" for branch merging.

20) Have you ever faced problems with branches merging ?

Answer: Yes, while merging code from one branch to another branch i have faced conflicts and i resolved conflicts then i merged my code from develop branch to master branch. What is the GIT stash drop?

21) What is "git log" and when you can use it?


Answer: The Git log command is used to find the history of commits happened in git repository. Using that history we can identify who, when, why & what changed in git repository.

22) What is a ‘conflict’ in a GIT repository?

Answer: When we are pushing/merging changes to git repository there is a chance of getting git conflicts. Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

23) How to resolve a conflict in Git?

Answer: The following steps will resolve conflict in Git Identify the files that have caused the conflicts

  • Make the necessary changes in the files so that conflict does not arise again
  • Add these files to staging using git add . command
  • Commit the changed file(s- using "git commit" command
24) How to compare files in git?

Answer: To compare files we can use "git diff" command

25) What is the purpose of ‘git config’?

Answer: Git uses your username to associate commits with an identity. The git config command can be used to set your user-name and email id.
Suppose you want to give a username and email id to associate a commit with an identity so that you can know who has made a particular commit git config –global user.name “Your Name”: This command will add a username.

git config –global user.email “Your E-mail Address”: This command will add an email id.

26) Which client software you are using to interact with git repository?

Answer: We have several client software's to interact with git repository
  • Git bash
  • Git GUI
  • Git Tortoise etc.
27) How do you revert a comm it that has already been pushed to central repository ?

Answer:

There are approaches to handle this scenario

Approach-1 : Remove or edit the changes in the file in a new commit and then push it to the remote repository. This is the most obvious way to fix an error. Once we have made required changes then commit that file to the remote repository using : git commit -m “commit message”Also, you can create a new commit that undoes all changes that were made in the bad commit.

28) What is the difference between git pull and git fetch?

Answer: Git pull command pulls new changes or commits from a particular branch from your central repository and updates your target branch in your local repository.

Git fetch is also used for the same purpose but it works in a slightly different way. When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge.

Your target branch will only be updated after merging the target branch and fetched branch. Just to make it easy for you, remember the equation below:

Git pull = git fetch + git merge

29) What is the difference between git pull and git clone ?

Answer
:

"git clone" is used to take entire project from central repository to local repository
"git pull" is used to take only latest changes/commits from central repository to local repository

30) What is the git stash drop ?

Answer:  git stash drop command is used to remove all stashes available in git.

Comments