Skip to main content

git interview questions

GIT


1.What is the purpose of git stash drop?
In case we do not need a specific stash, we use git stash drop command to remove it from the list of stashes. By default, this command removes to latest added stash To remove a specific stash we specify as argument in the git stash drop <stashname> command.

2. What is the HEAD in GIT?
A HEAD is a reference to the currently checked out commit. It is a symbolic reference to the branch that we have checked out. At any given time, one head is selected as the ‘current head’ This head is also known as HEAD (always in uppercase).

3. What is the most popular branching strategy in GIT?
There are many ways to do branching in GIT. One of the popular ways is to maintain two branches:
master: This branch is used for production. In this branch HEAD is always in production ready state.
develop: This branch is used for development. In this branch we store the latest code developed in project. This is work in progress code. Once the code is ready for deployment to production, it is merged into master branch from develop branch.

4. What is SubGit?
SubGit is software tool used for migrating SVN to Git. It is very easy to use. By using this we can create a writable Git mirror of a Subversion repository. It creates a bi-directional mirror that can be used for pushing to Git as well as committing to Subversion. SubGit also takes care of synchronization between Git and Subversion.

5. What is the use of git instaweb?
Git-instaweb is a script by which we can browse a git repository in a web browser. It sets up the gitweb and a web-server that makes the working repository available online.

6. What are git hooks?
Git hooks are scripts that can run automatically on the occurrence of an event in a Git repository. These are used for automation of workflow in GIT. Git hooks also help in customizing the internal behavior of GIT. These are generally used for enforcing a GIT commit policy.

7. What is GIT?
GIT is a mature Distributed Version Control System (DVCS). It is used for Source Code Management (SCM). It is open source software. It was developed by Linus Torvalds, the creator of Linux operating system. GIT works well with a large number of IDEs (Integrated Development Environments) like- Eclipse, InteliJ etc. GIT can be used to handle small and large projects.

8. What is a repository in GIT?
A repository in GIT is the place in which we store our software work. It contains a sub-directory called .git. There is only one .git directory in the root of the project. In .git, GIT stores all the metadata for the repository. The contents of .git directory are of internal use to GIT.

9. What are the main benefits of GIT?
There are following main benefits of GIT:
1.     Distributed System: GIT is a Distributed Version Control System (DVCS). So you can keep your private work in version control but completely hidden from others. You can work offline as well.
2.     Flexible Workflow: GIT allows you to create your own workflow. You can use the process that is suitable for your project. You can go for centralized or master-slave or any other workflow.
3.     Fast: GIT is very fast when compared to other version control systems.
4.     Data Integrity: Since GIT uses SHA1, data is not easier to corrupt.
5.     Free: It is free for personal use. So many amateurs use it for their initial projects. It also works very well with large size project.
6.     Collaboration: GIT is very easy to use for projects in which collaboration is required. Many popular open source software across the globe use GIT.

10. What are the disadvantages of GIT?
GIT has very few disadvantages. These are the scenarios when GIT is difficult to use. Some of these are:
1.     Binary Files: If we have a lot binary files (non-text) in our project, then GIT becomes very slow. E.g. Projects with a lot of images or Word documents.
2.     Steep Learning Curve: It takes some time for a newcomer to learn GIT. Some of the GIT commands are non-intuitive to a fresher.
3.     Slow remote speed: Sometimes the use of remote repositories in slow due to network latency. Still GIT is better than other VCS in speed.

11.What are the main differences between GIT and SVN?
The main differences between GIT and SVN are:
1.     Decentralized: GIT is decentralized. You have a local copy that is a repository in which you can commit. In SVN you have to always connect to a central repository for check-in.
2.     Complex to learn: GIT is a bit difficult to learn for some developers. It has more concepts and commands to learn. SVN is much easier to learn.
3.     Unable to handle Binary files: GIT becomes slow when it deals with large binary files that change frequently. SVN can handle large binary files easily.
4.     Internal directory: GIT creates only .git directory. SVN creates .svn directory in each folder.
5.     User Interface: GIT does not have good UI. But SVN has good user interfaces.

12. How will you start GIT for your project?
We use git init command in an existing project directory to start version control for our project. After this we can use git add and git commit commands to add files to our GIT repository.

13. What is git clone in GIT?
In GIT, we use git clone command to create a copy of an existing GIT repository in our local. This is the most popular way to create a copy of the repository among developers. It is similar to svn checkout. But in this case the working copy is a full-fledged repository.

14. How will you create a repository in GIT?
To create a new repository in GIT, first we create a directory for the project. Then we run ‘git init’ command. Now, GIT creates .git directory in our project directory. This is how our new GIT repository is created.

15. What are the different ways to start work in GIT?
We can start work in GIT in following ways:
New Project: To create a new repository we use git init command.
Existing Project: To work on an existing repository we use git clone command.

16. GIT is written in which language?
Most of the GIT distributions are written in C language with Bourne shell. Some of the commands are written in Perl language.

17. What does ‘git pull’ command in GIT do internally?
In GIT, git pull internally does a git fetch first and then does a git merge. So pull is a combination of two commands: fetch and merge. We use git pull command to bring our local branch up to date with its remote version.

18. What does ‘git push’ command in GIT do internally?
In GIT, git push command does following two commands:
1. fetch: First GIT, copies all the extra commits from server into local repo and moves origin/master branch pointer to the end of commit chain.
2. merge: Then it merges the origin/master branch into the master branch. Now the master branch pointer moves to the newly created commit. But the origin/master pointer remains there.

19. What is git stash?
In GIT, sometimes we do not want to commit our code but we do not want to lose also the unfinished code. In this case we use git stash command to record the current state of the working directory and index in a stash. This stores the unfinished work in a stash, and cleans the current branch from uncommitted changes. Now we can work on a clean working directory. Later we can use the stash and apply those changes back to our working directory. At times we are in the middle of some work and do not want to lose the unfinished work, we use git stash command.

20. What is the meaning of ‘stage’ in GIT?
In GIT, stage is a step before commit. To stage means that the files are ready for commit. Let say, you are working on two features in GIT. One of the features is finished and the other is not yet ready. You want to commit and leave for home in the evening. But you can commit since both of them are not fully ready. In this case you can just stage the feature that is ready and commit that part. Second feature will remain as work in progress.

21. What is the purpose of git config command?
We can set the configuration options for GIT installation by using git config command.

22. How can we see the configuration settings of GIT installation?
We can use ‘git config --list’ command to print all the GIT configuration settings in GIT installation.

23. How will you write a message with commit command in GIT?
We call following command for commit with a message:
$/> git commit –m <message>

24. What is stored inside a commit object in GIT?
GIT commit object contains following information:
SHA1 name: A 40 character string to identify a commit
Files: List of files that represent the state of a project at a specific point of time
Reference: Any reference to parent commit objects

25. How many heads can you create in a GIT repository?
There can be any number of heads in a repository. By default there is one head known as HEAD in each repository in GIT.

26. Why do we create branches in GIT?
If we are simultaneously working on multiple tasks, projects, defects or features, we need multiple branches. In GIT we can create a separate branch for each separate purpose.
Let say we are working on a feature, we create a feature branch for that. In between we get a defect to work on then we create another branch for defect and work on it. Once the defect work is done, we merge that branch and come back to work on feature branch again. So working on multiple tasks is the main reason for using multiple branches.

27. What are the different kinds of branches that can be created in GIT?
We can create different kinds of branches for following purposes in GIT:
Feature branches: These are used for developing a feature.
Release branches: These are used for releasing code to production.
Hotfix branches: These are used for releasing a hotfix to production for a defect or emergency fix.

28. How will you create a new branch in GIT?
We use following command to create a new branch in GIT:
$/> git checkout –b <branchname>

29. How will you add a new feature to the main branch?
We do the development work on a feature branch that is created from master branch. Once the development work is ready we use git merge command to merge it into master branch.

30. What is a pull request in GIT?
A pull request in GIT is the list of changes that have been pushed to GIT repository. Generally these changes are pushed in a feature branch or hotfix branch. After pushing these changes we create a pull request that contains the changes between master and our feature branch. This pull request is sent to reviewers for reviewing the code and then merging it into develop or release branch.

31.What is merge conflict in GIT?
A merge conflict in GIT is the result of merging two commits. Sometimes the commit to be merged and current commit have changes in same location. In this scenario, GIT is not able to decide which change is more important. Due to this GIT reports a merge conflict. It means merge is not successful. We may have to manually check and resolve the merge conflict.

32. How can we resolve a merge conflict in GIT?
When GIT reports merge conflict in a file, it marks the lines as follows:
E.g. the business days in this week are
<<<<<<< HEAD
five
=======
six
>>>>>>> branch-feature
To resolve the merge conflict in a file, we edit the file and fix the conflicting change. In above example we can either keep five or six. After editing the file we run git add command followed by git commit command. Since GIT is aware that it was merge conflict, it links this change to the correct commit.

33. What command will you use to delete a branch?
After the successful merge of feature branch in main branch, we do not need the feature branch. To delete an unwanted branch we use following command:
git branch –d <branchname>

34. What command will you use to delete a branch that has unmerged changes?
To forcibly delete an unwanted branch with unmerged changes, we use following command:
git branch –D <branchname>

35. What is the alternative command to merging in GIT?
Another alternative of merging in GIT is rebasing. It is done by git rebase command.

36. What is Rebasing in GIT?
Rebasing is the process of moving a branch to a new base commit. It is like rewriting the history of a branch. In Rebasing, we move a branch from one commit to another. By this we can maintain linear project history. Once the commits are pushed to a public repository, it is not a good practice to use Rebasing.

37. What is the ‘Golden Rule of Rebasing’ in GIT?
The golden rule of Rebasing is that we should never use git rebase on public branches. If other people are using the same branch then they may get confused by looking at the changes in Master branch after GIT rebasing. Therefore, it is not recommended to do rebasing on a public branch that is also used by other collaborators.

38. Why do we use Interactive Rebasing in place of Auto Rebasing?
By using Interactive rebasing we can alter the commits before moving them to a new branch. This is more powerful than an automated rebase. It gives us complete control over the branch’s commit history. Generally, we use Interactive Rebasing to clean up the messy history of commits just before merging a feature branch into master.

39. What is the command for Rebasing in Git?
Git command for rebasing is:
git rebase <new-commit>

40. What is the main difference between git clone and git remote?
The main difference between git clone and git remote is that git clone is used to create a new local repository whereas git remote is used in an existing repository. git remote adds a new reference to existing remote repository for tracking further changes. git clone creates a new local repository by copying another repository from a URL.

41.What is GIT version control?
GIT version control helps us in managing the changes to source code over time by a software team. It keeps track of all the changes in a special kind of database. If we make a mistake, we can go back in time and see previous changes to fix the mistake. GIT version control helps the team in collaborating on developing a software and work efficiently. Every one can merge the changes with confidence that everything is tracked and remains intact in GIT version control. Any bug introduced by a change can be discovered and reverted back by going back to a working version.

42. What GUI do you use for working on GIT?
There are many GUI for GIT that we can use. Some of these are:
GitHub Desktop
GITX-dev
Gitbox
Git-cola
SourceTree
Git Extensions
SmartGit
GitUp

43. What is the use of git diff command in GIT?
In GIT, git diff command is used to display the differences between 2 versions, or between working directory and an index, or between index and most recent commit. It can also display changes between two blob objects, or between two files on disk in GIT. It helps in finding the changes that can be used for code review for a feature or bug fix.

44. What is git rerere?
In GIT, rerere is a hidden feature. The full form of rerere is “reuse recorded resolution”. By using rerere, GIT remembers how we’ve resolved a hunk conflict. The next time GIT sees the same conflict, it can automatically resolve it for us.

45. What are the three most popular version of git diff command?
Three most popular git diff commands are as follows:
git diff: It displays the differences between working directory and the index.
git diff –cached: It displays the differences between the index and the most recent commit.
git diff HEAD: It displays the differences between working directory and the most recent commit

46. What is the use of git status command?
In GIT, git status command mainly shows the status of working tree. It shows following items:
1.     The paths that have differences between the index file and the current HEAD commit.
2.     The paths that have differences between the working tree and the index file
3.     The paths in the working tree that are not tracked by GIT.
Among the above three items, first item is the one that we commit by using git commit command. Item two and three can be committed only after running git add command.

47. What is the main difference between git diff and git status?
In GIT, git diff shows the differences between different commits or between the working directory and index. Whereas, git status command just shows the current status of working tree.

48. What is the use of git rmcommand in GIT?
In GIT, git rm command is used for removing a file from the working tree and the index. We use git rm –r to recursively remove all files from a leading directory.

49. What is the command to apply a stash?
Sometimes we want to save our unfinished work. For this purpose we use git stash command. Once we want to come back and continue working from the last place where we left, we use git stash apply command to bring back the unfinished work. So the command to apply a stash is:
git stash apply
Or we can use
git stash apply <stashname>

50. Why do we use git log command?
We use git log command to search for specific commits in project history. We can search git history by author, date or content. It can even list the commits that were done x days before or after a specific date.

Comments

Popular posts from this blog

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful hardware with multipl

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin

Cloud Computing Interview Questions

Cloud Computing 1. What are the benefits of Cloud Computing? There are ten main benefits of Cloud Computing: Flexibility : The businesses that have fluctuating bandwidth demands need the flexibility of Cloud Computing. If you need high bandwidth, you can scale up your cloud capacity. When you do not need high bandwidth, you can just scale down. There is no need to be tied into an inflexible fixed capacity infrastructure. Disaster Recovery : Cloud Computing provides robust backup and recovery solutions that are hosted in cloud. Due to this there is no need to spend extra resources on homegrown disaster recovery. It also saves time in setting up disaster recovery. Automatic Software Updates : Most of the Cloud providers give automatic software updates. This reduces the extra task of installing new software version and always catching up with the latest software installs. Low Capital Expenditure : In Cloud computing the model is Pay as you Go. This means there is very