Learn all the frequently asked interview questions on Git
1. What is Git?
Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). It can track changes to a file and allows you to revert back to any particular change. In Git, every developer’s working copy of the code is also a repository that can contain the full history of all changes. Git has the functionality, performance, security and flexibility that most teams and individual developers need. Git is a very well supported open source project and enjoys great community support and a vast user base.
2. Explain some basic Git commands?
3. In Git how do you revert a commit that has already been pushed and made public?
We can revert a particular commit b executing the following command:
git reset --hard <commitID>
4. What is Git rebase?
Rebasing is the process of moving or combining a sequence of commits to a new base commit. From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you’d created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base.
5. What is Git stash?
A developer working with a current branch wants to switch to another branch to work on something else, but the developer doesn’t want to commit changes to your unfinished work. The solution to this issue is Git stash. Git stash takes your modified tracked files and saves them on a stack of unfinished changes that you can reapply at any time.
6. How do you find a list of files that have been changed in a particular commit?
The command to get a list of files that have been changed in a particular commit is:
git diff-tree –r <commit Id>
7. What is a merge conflict in Git?
A Git merge conflict happens when you have merge branches with different commits, and Git needs your help to decide which changes to incorporate in the final merge.
8. Explain the difference between git fetch and git pull?
9. What is the difference between Git Merge and Git Rebase?
To incorporate the new commits into your feature branch, use Git merge.– Creates an extra merge commit every time you need to incorporate changes– But, it pollutes your feature branch history
As an alternative to merging, you can rebase the feature branch on to master.
10. What is the git command that downloads any repository from GitHub to your computer?
git clone <repository url>
11. What is the command you can use to write a commit message?
git commit -m "<commit message>"
12. What is the function of ‘git config’?
The ‘git config’ command is a convenient way to set configuration options for your Git installation. Behavior of a repository, user info, preferences etc. can be defined through this command.
Following command configures the author name of the commit:
git config --global user.name "Bijan Patel"
13. How can you create a repository in Git?
In Git, to create a repository, create a directory for the project if it does not exist, and then run command “git init”. By running this command .git directory will be created in the project directory, the directory does not need to be empty.
14. What is the function of ‘git diff ’ in git?
git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more. The git diff command is often used along with git status and git log to analyze the current state of a Git repo.
15. What is ‘git status’ command?
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history.
16. How to create a new local branch in Git?
To create a new branch and switch to it at the same time, you can run the git checkout command with the -b switch:
git checkout -b <branch name>
17. How to delete a local branch in Git?
To delete the local GIT branch we can try one of the following commands:
git branch -d branch_name
git branch -D branch_name
The -d option stands for --delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.
-D option stands for
--delete --force, which deletes the branch regardless of its push and merge status.
18. How to add an existing project to Git?
– Create a new repository in Git
– Initialize the local directory as a Git repository.
$ git init
– Add the files in your new local repository. This stages them for the first commit.
$ git add .
– Commit the files that you’ve staged in your local repository.
$ git commit -m "Initial commit"
– Add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin <remote repository URL>
# sets the new remote
$ git remote -v
# verifies the new remote URL
- Push the changes in your local repository in GitHub
$ git push origin master
# pushes the changes in your local repository upto the remote repository you specified as the origin
19. What is Git fork?. What is the difference between fork, branch and clone?
A fork is a remote, server-side copy of a repository, distinct from the original.
A clone is a local copy of some repository. When you clone, you are actually copying the entire source repository, including all the history and branches.
A branch is a mechanism to handle the changes within a single repository in order to eventually merge them with the rest of code.
20) Explain the difference between HEAD, working tree and index, in Git?
The working tree/working directory/workspace is the directory tree of (source) files that you see and edit.
The index/staging area is a single, large, binary file in <baseOfRepo>/.git/index, which lists all files in the current branch, their sha1 checksums, time stamps and the file name.
HEAD is a reference to the last commit in the currently checked-out branch.
Full Stack Test Automation Expert | Selenium Framework Developer | Certified Tosca Automation Specialist | Postman | DevOps | AWS | IC Agile Certified | Trainer | Youtuber | Blogger|