Introduction: What is GIT?
Git is a free and open-source distributed version control system, which is designed to handle everything from small to very large projects with speed and efficiency.
Uses of Git
- Git is used to tracking changes in the source code.
- Distributed version control tool used for source code management.
- Allows multiple developers to work together.
- Supports non-linear development because of its thousands of parallel branches.
Features of Git
- Free and open-source
- Tracks history
- Supports non-linear development
- Creates backup
- Supports collaboration
- Branching is easier
- Distributed development.
The following image shows the git workflow diagram:
So Why Git? What are the advantages?
Imagine a scenario without using Git.
There is a large project and 100 developers are working on the project.
- Developers used to submit their codes to the central server without having a copy of their own.
- Any changes made to the source code were not known to the other developers.
- There was no communication between any of the developers.
Now, let's analyse the scenario after using Git.
- Every developer has an entire copy of the code on their local system.
- Any change made to the source code can be tracked by others.
- There is regular communication between the developers.
Therefore, for large projects that involve thousands of developers, Git helps those developers to work collaboratively and efficiently in a structured manner.
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.
- It is most suitable when there are multiple people working on projects as a team and is used for tracking the project changes and efficiently supports the collaboration of the development process.
- With the help of the versioning system, the developer can identify who has made what changes and then run tests and fix bugs if any and then do necessary feature implementation. In case of any unforeseen circumstances, the code can be reverted to any of the previously working versions thereby saving huge efforts.
Scope of Git:
- Due to a well-established version control system and the support for collaborative work, git has garnered wide popularity not just amongst the software developers, but also among the people who do other tasks like documentation or any other collaborative work. It can seem challenging at first, but once we get the hang of git, we find that it makes our lives much simpler.
- It has an amazing branching system that supports nonlinear development along with keeping the developers accountable for their code. This helps in making the development process efficient and faster.
Basic GIT Interview Questions
1. What is a version control system (VCS)?
A VCS keeps track of the contributions of the developers working as a team on the projects. They maintain the history of code changes done and with project evolution, it gives an upper hand to the developers to introduce new code, fixes bugs, and run tests with confidence that their previously working copy could be restored at any moment in case things go wrong.
2. What is a git repository?
A repository is a file structure where git stores all the project-based files. Git can either stores the files on the local or the remote repository.
3. What does git clone do?
The command creates a copy (or clone) of an existing git repository. Generally, it is used to get a copy of the remote repository to the local repository.
4. 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.
To set up your name and email address before using git commands, we can run the below commands:
git config –-global user.name “<>”
git config --global user.email “<>”
5. Can you explain head in terms of git and also tell the number of heads that can be present in a repository?
- A 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.
- 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
6. What is a conflict?
- Git usually handles feature merges automatically but sometimes while working in a team environment, there might be cases of conflicts such as:
1. When two separate branches have changes to the same line in a file
2. A file is deleted in one branch but has been modified in the other.
- These conflicts have to be solved manually after discussion with the team as git will not be able to predict what and whose changes have to be given precedence.
7. What is the functionality of git ls-tree?
This command returns a tree object representation of the current repository along with the mode and the name of each item and the SHA-1 value of the blob.
8. 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.
9. 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’.
10. What does git add command do?
- This command adds files and changes to the index of the existing directory.
- You can add all changes at once using git add . command.
- You can add files one by one specifically using git add command.
- You can add contents of a particular folder by using git add // command.
11. Why is it considered to be easy to work on Git?
With the help of git, developers have gained many advantages in terms of performing the development process faster and in a more efficient manner. Some of the main features of git which has made it easier to work are:
- Due to its sophisticated branching capabilities, developers can easily work on multiple branches for the different features of the project.
- It also has an easier merge option along with an efficient work-flow feature diagram for tracking it.
Distributed manner of development:
- Git is a distributed system and due to this nature, it became easier to trace and locate data if it's lost from the main server.
- In this system, the developer gets a repository file that is present on the server. Along with this file, a copy of this is also stored in the developer’s system which is called a local repository.
- Due to this, the scalability of the project gets drastically improved.
Pull requests feature:
- This feature helps in easier interaction amongst the developers of a team to coordinate merge-operations.
- It keeps a proper track of the changes done by developers to the code.
Effective release cycle:
- Due to the presence of a wide variety of features, git helps to increase the speed of the release cycle and helps to improve the project workflow in an efficient manner.
12. How will you create a git repository?
- Have git installed in your system.
- Then in order to create a git repository, create a folder for the project and then run git init.
- Doing this will create a .git file in the project folder which indicates that the repository has been created.
13. 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.
14. 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
- To delete a branch remotely, run the command: git push origin --delete
- 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.
15. 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.
16. What does git stash apply command do?
- git stash apply command is used for bringing the works back to the working directory from the stack where the changes were stashed using git stash command.
- This helps the developers to resume their work where they had last left their work before switching to other branches
17. Can you tell the difference between Git and GitHub?
This is a distributed version control system installed on local machines which allow developers to keep track of commit histories and supports collaborative work.
This is a cloud-based source code repository developed by using git.
This is maintained by “The Linux Foundation”.
This was acquired by “Microsoft”
SVN, Mercurial, etc are the competitors
GitLab, Atlassian BitBucket, etc are the competitors.
18. What do the git diff and git status commands do?
This shows the changes between commits, working trees, etc.
This shows the difference between the working directory and index that is essential in understanding git in depth.
git diff works in a similar fashion to git status with the only difference of showing the differences between commits and also between the working directory and index.
19. 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.
20. 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.
21. What are the functionalities of git reset --mixed and git merge --abort?
- git reset --mixed command is used for undoing changes of the working directory and the git index.
- git merge --abort command is used for stopping the merge process and returning back to the state before the merging occurred.
22. Can you tell the differences between git revert and 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.
In this article, we have seen the introduction and scope of git along with the most commonly asked git interview questions. The popularity of Git is growing at an amazing pace and it is predicted that one day all the developers around the world would be working together as one single entity with the help of git.
Git is a freely available, distributed version control system that helps developers to collaborate with multiple people on their projects and makes their life easy. With so much scope and the ever-growing demand, git has evolved into being an inseparable part of a developer’s life.