Monday, 4 September 2023

Git Interview Questions

 

1.What is Git and why is it used?

·         Git is the most popular, open-source, widely used, and an example of distributed version control system (DVCS) used for handling the development of small and large projects in a more efficient and neat manner.

2. What does the command git config do?

The git config command is a convenient way to set configuration options for defining the behavior of the repository, user information and preferences, git installation-based configurations, and many such things. 

For example:
To set up your name and email address before using git commands, we can run the below commands:

·         git config –global user.name “<<your_name>>”
 

·         git config --global user.email “<<your_email>>”

3. Can you explain head in terms of git and also tell the number of heads that can be present in a repository?

·         head is nothing but a reference to the last commit object of a branch.

·         For every repository, there will always be a default head referred to as “master” or now “main” (as per GitHub) but there is no restriction to the count of heads available. In other words, it can have any number of heads.

·         Usages:

- To go or checkout to 1 commit before the latest commit, we use 

·         git checkout HEAD~1

- To uncommit the last 3 commits without losing the changes, we first run 

·           git reset HEAD~3.

·         Then we can see the changes made in the last 3 commits and then update it manually and commit it finally.

- In order to uncommit the last 3 commits and also remove the changes, we can run the command: 
git reset --hard HEAD~3. This command will completely remove all the changes.

- To look into the changes made in the last 3 commits, we can run 

·         git diff HEAD~3

- To make a new commit by reverting the last 3 commits, we can run the command: 

·         git revert --no-commit HEAD~3...HEAD

4. What does git status command do?

git status command is used for showing the difference between the working directory and the index which is helpful for understanding git in-depth and also keep track of the tracked and non-tracked changes.

 

5.Define “Index”.

Before making commits to the changes done, the developer is given provision to format and review the files and make innovations to them. All these are done in the common area which is known as ‘Index’ or ‘Staging Area’.


In the above image, the “staged” status indicates the staging area and provides an opportunity for the people to evaluate changes before committing them.

6. Tell me something about git stash?

Git stash can be used in cases where we need to switch in between branches and at the same time not wanting to lose edits in the current branch. Running the git stash command basically

 pushes the current working directory state and index to the stack for future use and thereby providing a clean working directory for other tasks

git stash apply

 

7. What is the command used to delete a branch?

·         To delete a branch we can simply use the command git branch –d [head].

·         To delete a branch locally, we can simply run the command: git branch -d <local_branch_name>

·         To delete a branch remotely, run the command: git push origin --delete <remote_branch_name>

·         Deleting a branching scenario occurs for multiple reasons. One such reason is to get rid of the feature branches once it has been merged into the development branch

 

8. What differentiates between the commands git remote and git clone?

git remote command creates an entry in  git config that specifies a name for a particular URL. Whereas git clone creates a new git repository by copying an existing one located at the URL.

 

9. Differentiate between git pull and git fetch.

    

git pull 

git fetch

This command pulls new changes from the currently working branch located in the remote central repository.

This command is also used for a similar purpose but it follows a two step process: 
1. Pulls all commits and changes from desired branch and stores them in a new branch of the local repository. 
current
2. For changes to be reflected in the current / target branch, git fetch should be followed by git merge command.

git pull = git fetch + git merge 

 

10. Can you give differences between “pull request” and “branch”?

  

pull request

branch

This process is done when there is a need to put a developer’s change into another person’s code branch. 

A branch is nothing but a separate version of the code.

 

11. What is the difference between git stash apply vs git stash pop command?

·         git stash pop command throws away the specified stash (topmost stash by default) after applying it.

·         git stash apply command leaves the stash in the stash list for future reuse. In case we wanted to remove it from the list, we can use the git stash drop command.

git stash pop = git stash apply + git stash drop

 

12. What command helps us know the list of branches merged to master?

·         git branch --merged helps to get the list of the branches that have been merged into the current branch.

·         Note: git branch --no-merged lists the branches that have not been merged to the current branch

13. How will you resolve conflict in Git?

·         Conflicts occur whenever there are multiple people working on the same file across multiple branches. In such cases, git won't be able to resolve it automatically as it is not capable of deciding what changes has to get the precedence.

·         Following are the steps are done in order to resolve git conflicts:
1. Identify the files that have conflicts.
2. Discuss with members who have worked on the file and ensure that the required changes are done in the file.
3. Add these files to the staged section by using the git add command.
4. Commit these changes using the git commit command.
5. Finally, push the changes to the branch using the git.

 

14. What is best advisable step in cases of broken commit: Create an additional commit OR amend an existing commit?

·         It is always advisable to create an additional commit rather than amending the existing commit due to the following reasons:
- Doing the amend operation destroys the previously saved state of that commit. If only the commit message gets changes or destroyed, it's acceptable but there might be cases when the contents of the commits get amended. This results in the loss of important information associated with the commit.
- Over usage of 
git commit --amend can have severe repercussions as the small commit amend can continue to grow and gather unrelated changes over time.

 

15. How to revert a bad commit which is already pushed?

There can be cases where we want to revert from the pushed changes and go back to the previous version. To handle this, there are two possible approaches based on the situations:

·         Approach 1: Fix the bad changes of the files and create a new commit and push to the remote repository. This step is the simplest and most recommended approach to fix bad changes. You can use the command: git commit -m "<message>"

·         Approach 2: New commit can be created that reverts changes done in the bad commit. It can be done using git revert <name of bad commit>

git push origin <branch-name>


16.Explain steps involved in removing a file from git index without removing from the local file system?

·         git reset command for removing the file from the staged version and then adding that file to the .gitignore file to avoid repeating the same mistake again.

git reset <file_name> # remove file from index

17. Can you tell the differences between git revert and git reset?

git revert 

git reset

This command is used for creating a new commit that undoes the changes of the previous commit.

This command is used for undoing the local changes done in the git repository

Using this command adds a new history to the project without modifying the existing history

This command operates on the commit history, git index, and the working directory.

18. What is the meaning of “Index” or “Staging Area” in GIT?

When we are making the commits, we can make changes to it, format it and review it in the intermediate area known as ‘Staging Area’ or ‘Index’.







 


No comments:

Post a Comment