Git Interview Questions: Git is a powerful version control system widely used in software development to track changes in code, collaborate with other developers, and manage software releases. As a result, It has become an essential skill for software developers and engineers seeking employment in the tech industry.
If you are applying for a position in a software development company, it is highly likely that you will be asked some Git-related questions during your GIT interview. These questions can range from basic concepts to advanced techniques, and they can be used to assess your knowledge and proficiency with Git. In this context, preparing for Git interview questions can significantly increase your chances of landing your dream job in the software development industry.
Git Interview Questions
- Set of commands to upload a new file in git which is created in your local?
- committed 2 sides, do you want the branch to be reverted to the previous one? how?
- How can we see the most recent commits in GIT?
- How can we know if a branch is already merged into a master in GIT?
- What is the purpose of git stash drop?
- What is the HEAD in GIT?
- What is the most popular branching strategy in GIT?
- What is SubGit?
- What is the use of git instaweb?
- What are git hooks?
- What is GIT?
- What is a repository in GIT?
- What are the main benefits of GIT?
- What are the disadvantages of GIT?
- What are the main differences between GIT and SVN?
- How will you start GIT for your project?
- What is a git clone in GIT?
- How will you create a repository in GIT?
- What are the different ways to start work in GIT?
- GIT is written in which language?
- What does the ‘git pull’ command in GIT do internally?
- What does the ‘git push’ command in GIT do internally?
- What is git stash?
- What is the meaning of ‘stage’ in GIT?
- What is the purpose of the git config command?
- How can we see the configuration settings of the GIT installation?
- How will you write a message with a commit command in GIT?
- What is stored inside a commit object in GIT?
- How many heads can you create in a GIT repository?
- Why do we create branches in GIT?
- What are the different kinds of branches that can be created in GIT?
- How will you create a new branch in GIT?
- How will you add a new feature to the main branch?
- What is a pull request in GIT?
- What is merge conflict in GIT?
- How can we resolve a merge conflict in GIT?
- What command will you use to delete a branch?
- What command will you use to delete a branch that has unmerged changes?
- What is the alternative command to merging in GIT?
- What is Rebasing in GIT?
- What is the ‘Golden Rule of Rebasing’ in GIT?
- Why do we use Interactive Rebasing in place of Auto Rebasing?
- What is the command for Rebasing in Git?
- What is the main difference between git clone and git remote?
- What is GIT version control?
- What GUI do you use to work on GIT?
- What is the use of the git diff command in GIT?
- What is git rerere?
- What are the three most popular versions of the git diff command?
- What is the use of the git status command?
- What is the main difference between git diff and git status?
- What is the use of the git rm command in GIT?
- What is the command to apply a stash?
- Why do we use the git log command?
- Why do we need the git add command in GIT?
- Why do we use the git reset command?
- What does a commit object contain?
- How can we convert git log messages to a different format?
- What are the programming languages in which git hooks can be written?
- What is a commit message in GIT?
- How does GIT protect the code in a repository?
- How does GIT provide flexibility in version control?
- How can we change a commit message in GIT?
- Why is it advisable to create an additional commit instead of amending an existing commit?
- What is a bare repository in GIT?
- How do we put a local repository on the GitHub server?
- How will you delete a branch in GIT?
- How to resolve conflict in Git.
- What is a pull request?
- How to reverse your code in git
- Git and its commands
- Which Code Repository are you using (Ex: GitHub)
GitHub Interview Questions
- How can we set up a Git repository to run code sanity checks and UAT tests just before a commit?
- How can we revert a commit that was pushed earlier and is public now?
- In GIT, how will you compress the last n commits into a single commit?
- How will you switch from one branch to a new branch in GIT?
- How can we clean unwanted files from our working directory in GIT?
- What is the purpose of the git tag command?
- What is cherry-pick in GIT?
- What is a short log in GIT?
- How can you find the names of files that were changed in a specific commit?
- How can we attach an automated script to run when a new commit is made using the push command?
- What is the difference between pre-receive, update and post-receive hooks in GIT?
- Do we have to store Scripts for GIT hooks within the same repository?
- How can we determine the commit that is the source of a bug in GIT?
- How can we see the differences between two commits in GIT?
- What are the different ways to identify a commit in GIT?
- When we run git branch <branchname>, how does GIT know the SHA-1 of the last commit?
- What are the different types of Tags you can create in GIT?
- How can we rename a remote repository?
- Some people use git checkout and some use git co for checkout. How is that possible?
- How can we see the last commit on each of our branches in GIT?
- Is the origin a special branch in GIT?
- How can we configure GIT to not ask for the password every time?
- What are the four major protocols used by GIT for data transfer?
- What is GIT protocol?
- How can we work on a project where we do not have push access?
- What is git grep?
- How can you reorder commits in GIT?
- How will you split a commit into multiple commits?
- What is a filter-branch in GIT?
- What are the three main trees maintained by GIT?
- What are the three main steps of working GIT?
- What are our and theirs merge options in GIT?
- How can we ignore merge conflicts due to Whitespace?
- What is git blame?
- What is a submodule in GIT?
GIT Commands
GIT Commands are a set of instructions that developers use to interact with the Git version control system. These commands enable developers to perform various operations, such as creating and cloning repositories, managing branches and commits, and merging changes from different branches.
Knowing these commands is essential for developers who want to effectively manage their code changes and collaborate with other team members using Git.
Git Create Command
- Create a new local Repository: $ git init
- Clone an existing repository: $ git clone path(ssh://username@domain.com/repository name.git)
Git Local Changes
- Changed files in your working directory: $ git status
- Changes to Tracked files: $ git diff
- Add all current changes to the next commit: $ git add.
- Add some changes in the file to the next commit: $ git add -p <filename>
- Commit all local changes in tracked files: $ git commit -a
- Commit previous commit changes: $ git commit
- Change the last commit: $git commit –amend
Check GIT Commit History
- Show all commits: $ git log
- Show changes over time for a specific file: $ git log -p <filename>
- To see who changed what and when in File: $ git blame
Git Branches & Tags
- List all existing branches: $ git branch -av
- Switch to HEAD branch: $ git checkout <branch name>
- Create a new Branch based on your current HEAD: $ git branch <new-branch>
- Create a new tracking branch based on a remote branch: $ git checkout –track <remote/branch name>
- Delete a local branch: $ git branch -d <branch>
- Mark the current commit with a tag: $ git tag <tag-name>
Git Update & Publish
- List all currently configured Remotes: $ git remote -v
- Show Information about a remote: $ git remote show <remote>
- Add new remote repository: $ git remote add <short name> <url>
- Download all changes from remote but don’t Integrate into HEAD: $ git fetch <remote>
- Download all changes and directly merge/integration into HEAD: $ git pull <remote> <branch>
- Publish local changes on a remote: $ git push <remote> <branch>
- Delete a branch on the remote: $ git branch -d <remote/branch>
- Publish your tags: $ git push –tags
Git Merge & Rebase
- Merge branch into your current HEAD: $ git merge <branch>
- Rebace your current HEAD onto the branch(don’t replace published comments): $ git rebase <branch>
- Abort a rebace: $ git rebace –abort
- Continue a rebase after resolving conflicts: $ git rebase –continue
- Use your configured merge tool to solve conflicts: $ git mergetool
- Use your editor to manually solve conflicts and (after resolving ) mark the file as resolved: $ git add <resolved file> $ git rm <resolved file>
Git Undo Command
- Discard all local changes in your working directory: git reset –hard HEAD
- Discard local changes in a specific file: $ git checkout HEAD <file>
- Revert a commit: $ git revert <commit>
- Reset your Head pointer to a previous commit and discard all changes since then: $ git reset –hard <commit> and preserve all changes as unstaged changes $ git reset <commit> and preserve uncommitted local changes $ git reset –keep <commit>
GitHub Interview Questions And Answers
GitHub is a popular platform for hosting and collaborating on code repositories using the Git version control system. It is widely used by software development teams and individual developers to manage code changes and collaborate on software projects. If you are applying for a position that requires experience with GitHub, you will likely be asked some GitHub-related questions during your interview.
How do you revert a commit that has already been pushed and made public?
Ans: One or more commits can be reverted through the use of git revert. This command, in essence, creates a new commit with patches that cancel out the changes introduced in specific commits. In case the commit that needs to be reverted has already been published or changing the repository history is not an option, git revert can be used to revert commits. Running the following command will revert the last two commits:
Git revert HEAD~2.HEAD
Alternatively, one can always check out the state of a particular commit from the past, and commit it anew.
How do you squash the last N commits into a single commit?
Ans: Squashing multiple commits into a single commit will overwrite history, and should be done with caution. However, this is useful when working in feature branches. To squash the last N commits of the current branch, run the following command (with {N} replaced with the number of commits that you want to squash):
git rebase – I HEAD~{N}
Upon running this command, an editor will open with a list of these N commit messages, one per line. Each of these lines will begin with the word “pick.” Replacing “pick” with “squash” or “s” will tell Git to combine the commit with the commit before it. To combine all N commits into one, set every commit in the list to be squash except the first one. Upon exiting the editor, and if no conflict arises, git rebase will allow you to create a new commit message for the newly combined commit.
How do you find a list of files that have changed in a particular commit?
Ans: git diff-tree -r {hash}
Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.
The output will also include some extra information, which can be easily suppressed by including a couple of flags:
git diff-tree –no-commit-id –name-only -r {hash}
Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the filenames, instead of their paths.
How do you set up a script to run every time a repository receives new commits through push?
Ans: To configure a script to run every time a repository receives new commits through push, one needs to define either a pre-receive, update or post-receive hook depending on when exactly the script needs to be triggered.
The pre-receive hook in the destination repository is invoked when commits are pushed to it. Any script bound to this hook will be executed before any references are updated. This is a useful hook to run scripts that help enforce development policies.
The update hook works similarly to the pre-receive hook and is also triggered before any updates are made. However, it is called once for every commit that has been pushed to the destination repository.
Finally, a post-receive hook in the repository is invoked after the updates have been accepted into the destination repository. This is an ideal place to configure simple deployment scripts, invoke some continuous integration systems, dispatch notification emails to repository maintainers, etc.
Hooks are local to every repository and are not versioned. Scripts can either be created within the hooks directory inside the “.git” directory or elsewhere, and links to those scripts can be placed within the directory.
What is git bisect? How can you use it to determine the source of a (regression) bug?
Ans: Git provides a rather efficient mechanism for finding bad commits. Instead of making the user try every single commit to find the first one that introduced some particular issue into the code, git bisect allows the user to perform a sort of binary search on the entire history of a repository.
By issuing the command git bisect start, the repository enters bisect mode. After this, all you have to do is identify a bad and a good commit:
git bisect bad # marks the current version as bad
git bisect good {hash, or tag} #marks the given hash or tag as good, ideally of some earlier commit
Once this is done, Git will then have a range of commits that it needs to explore. At every step, it will check out a certain commit from this range, and require you to identify it as good or bad. After which the range will be effectively halved, and the whole search will require a lot fewer steps than the actual number of commits involved in the range. Once the first bad commit has been found, or the bisect mode needs to be ended, the following command can be used to exit the mode and reset the bisection state:
git bisect reset
What are the different ways you can refer to a commit?
Ans: Each commit is given a unique hash. These hashes can be used to identify the corresponding commits in various scenarios (such as while trying to check out a particular state of the code using the git checkout {hash} command).
Additionally, it maintains a number of aliases for certain commits, known as refs. Every tag that you create in the repository effectively becomes a ref (and that is exactly why you can use tags instead of committing hashes in various git plumbing commands). It also maintains a number of special aliases that change based on the state of the repository, such as HEAD, FETCH_HEAD, MERGE_HEAD, etc.
It also allows commits to be referred to as relative to one another. For example, HEAD1 refers to the committed parent to HEAD, HEAD2 refers to the grandparent of the HEAD, and so on. In the case of merge commits, where the commit has two parents, ^ HEAD1 can be used to select one of the two parents; for example, HEAD2 can be used to follow the second parent.
Finally, respect. These are used to map local and remote branches together. However, they can also refer to commits that reside on remote branches, allowing one to control and manipulate them from a local environment.
What is git rebase, and how can it resolve conflicts in a feature branch before the merge?
Ans: In simple words, git rebase allows one to move the first commit of a branch to a new starting location. For example, if a feature branch was created from master, and since then the master branch has received new commits, git rebase can be used to move the feature branch to the tip of master. The command effectively will replay the changes made in the feature branch at the tip of the master, allowing conflicts to be resolved in the process. When done with care, this will enable the feature to branch and be merged into the master with relative ease and sometimes as a simple, fast-forward operation.
How do you configure the repository to run code sanity checking tools right before making commits, and preventing them if the test fails?
Ans: This can be done with a simple script bound to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script, one can run other tools, such as linters, and perform sanity checks on the changes being committed into the repository. For example, the following script:
#!/bin/sh
files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)
if [ -z files ]; then
exit 0
fi
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
fi
echo “Some .go files are not fmt’d”
exit 1
… checks to see if any .go file that is about to be committed needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.
What is a ‘bare repository’ in Git?
Ans: You are expected to distinguish between a “working directory” and a “bare repository.”
A “bare” repository contains the version control information and no working files (no tree), and it doesn’t contain the special. Git sub-directory. Instead, it contains all the contents of the .git sub-directory directly in the main directory itself, whereas the working directory consists of:
- A .git subdirectory with all the related revision history of your repo.
- A working tree or checked-out copies of your project files.
How do you revert a commit that has already been pushed and made public?
Ans: There can be two answers to this question and make sure that you include both because any of the below options can be used depending on the situation:
- Remove or fix the bad file in a new commit and push it to the remote repository. This is the most natural way to fix an error. Once you have made necessary changes to the file, commit it to the remote repository for that I will use
git commit -m “commit message.” - Create a new commit that undoes all changes that were made in the bad commit.to do this, I will use a command
git revert <name of bad commit>
What does a commit object contain?
Ans: Commit object contains the following components; you should mention all the three points presented below:
- A set of files representing the state of a project at a given point in time
- Reference to parent commit objects
- An SHAI name, a 40 character string that uniquely identifies the commit object.
What is Git bisect? How can you use it to determine the source of a (regression) bug?
Ans:
- I will suggest you first to give a small definition of Git bisect.
- Git bisect is used to find the commit that introduced a bug by using binary search. The command for Git bisect is
git bisect <subcommand> <options>
How will you know in Git if a branch has already been merged into master?
Ans:
- The answer is pretty direct.
- To know if a branch has been merged into the master or not, you can use the below commands:
- Git branch –merged It lists the branches that have been merged into the current branch.
Git branch –no-merged. It lists the branches that have not been merged.
- Now since you have mentioned the command above, explain to them what this command will do.
- This command uses a binary search algorithm to find which commit to your project’s history introduced a bug. You use it by first telling it a “bad” commit that is known to contain the bug, and a “good” commit that is known to be before the bug was introduced. Then Git bisect picks a commit between those two endpoints and asks you whether the selected commit is “good” or “bad.” It continues narrowing down the range until it finds the exact commit that introduced the change.
Name a few Git repository hosting services
Ans:
- Pikacode
- Visual Studio Online
- GitHub
- GitEnterprise
- SourceForge.net
What is the function of ‘git stash apply’?
Ans:
- When you want to continue working where you have left your work, ‘git stash apply’ command is used to bring back the saved changes to the working directory.
What is Source Code Management?
It is a process through which we can store and manage any code. Developers
write code, Testers write test cases, and DevOps engineers write scripts. This code, we can store and manage in Source Code Management. Different teams can store code simultaneously. It saves all changes separately. We can retrieve this code at any point of time.
What are the Advantages of Source Code Management?
- Helps in Achieving teamwork
- Can work on different features simultaneously
- Acts like pipeline b/w offshore & onshore teams
- Track changes (Minute level)
- Different people from the same team, as well as different teams, can store code simultaneously (Save all changes separately)
Available Source Code Management tools in the market?
There are so many Source Code Management tools available in the market. Those are
- Git
- SVN
- Perforce
- Clear case
Out of all these tools, Git is the most advanced tool in the market, and it offers many advantages compared to other Source Code Management tools.
What is Git?
Git is one of the Source Code Management tools where we can store any type of code. Git is the most advanced tool in the market now. We also call Git is version control system because every update stored as a new version. At any point of time, we can get any previous version. We can go back to previous
versions. Every version will have a unique number. That number we call commit-ID. By using this commit ID, we can track each change i.e. who did what at what time. For every version, it takes incremental backup instead of taking the whole backup. That’s why Git occupies less space. Since it is occupying less space, it is very fast.
What are the advantages of Git?
- Speed:- Git stores every update in the form of versions. For every version, it takes incremental backup instead of taking the whole backup. Since it is taking less space, Git is very fast. That incremental backup we call “Snapshot”
- Parallel branching:- We can create any number of branches as per our requirement. No need to take prior permission from any one, unlike other Source Code Management tools. Branching is for parallel development. Git branches allow us to work simultaneously on multiple features.
- Fully Distributed:- A backup copy is available in multiple locations in each and everyone’s server instead of keeping in one central location, unlike other Source Code Management tools. So even if we lose data from one server, we can recover it easily. That’s why we call GIT as DVCS (Distributed Version Control System)
What are the stages in Git?
There are a total of 4 stages in Git
- Workspace: This is the place where we can physically create and modify files. As Git users, we work in this space.
- Staging area/Indexing area: In this area, Git takes a snapshot for every version. It is a buffer zone between the workspace and the local repository. We can’t see this region because it is virtual.
- Local repository: This is where Git stores all commits locally. It is a hidden directory so that no one can accidentally delete it. Every commit has a unique commit ID.
- Central repository:- It is the place where Git stores all commit centrally. It belongs to everyone who is working in your project. Git Hub is one of the central repositories. Used for storing the code and sharing the code to others in the team.
What is the common branching strategy in Git?
- The product is the same, so one repo. But different features.
- Each feature has one separate branch
- Finally, merge (code) all branches
- For Parallel development
- Can create any no of branches
- Can create one branch on the basis of another branch
- Changes are personal to that particular branch
- Can put files only in branches (not in repo directly)
- The default branch is “Master”
- Files created in a workspace will be visible in any of the branch
- workspaces until you commit. Once you commit, then that file belongs to that particular branch.
How many types of repositories available in Git?
There are two types of repositories available in Git
- Bare Repositories (Central): These repositories are only for Storing & Sharing the code All central repositories are bare repositories
- Non – Bare Repositories (Local): In these repositories, we can modify the files All local /user repositories are Bare Repositories
Can you elaborate commit in Git?
- Storing file permanently in the local repository we call commit.
- For every commit, we get one commit ID
- It contains 40 long Alpha-numeric characters
- It uses the concept “Check some” (It’s a tool in Linux that generates binary values equal to the data present in the file)
- Even if you change one dot, Commit-ID will get changed
- Helps in tracking the changes
What do you mean by “Snapshot” in Git?
- It is a backup copy for each version git stores in a repository.
- Snapshot is an incremental backup copy (only backup for new changes)
- The snapshot represents some data of a particular time so that we can get data of a particular time by taking that particular snapshot
- This snapshot will be taken in Staging area in Git which is present between Git workspace and Git local repository.
Git Interview Questions
To initialize a repository or create a new git repository in your local system
Command: git init
What are the new changes and files that have been updated in your code?
Command: git status
How do you add files to the staging area?
Command: git add * / git add
How do you commit code to the local git repo? (local repository not yet pushed to remote)
Command: git commit -m
How to push code to a remote repository?
Ans:
i. First you need to authenticate yourself by using a token or basic authentication
ili. After this, you can create a repository on your GitHub account
ili. Copy the http URI, then use the following
Command: git remote add origin < remote_urt>
Now, finally pushing the code to the remote repository
Command: git push origin
How do you review the current branch?
Command: git branch (it reflects all existing branches and the current working branch)
How do you create and update a working branch?
Command: git branch -M
How to update the remote repository URL?
Command: git remote set-URL origin
How to update the local repository?
Command: git pull origin
How to delete a branch on local (after commit)?
Command: git branch -D < branch_name>
How to force delete a branch?
Command: git branch -D < branch_name>
How do you remove files from the staging area?
Command: git reset
How to create a git-ignore file?
Command: touch gitignore(works in git-bash)
Terminal: git add .gitignore
How to get a new repository in your local system?
Command: git clone <remote_repo_url›
How to search for text in the current working repo/directory?
Command: git grep ‹text›
How to create a more branch working directory (local)?
Command: git pull origin master (branch_name)
How do you fetch the latest changes from remote to local?
Command: git pull origin master (branch_name)
How do you save local changes made without committing or moving them to the remote repository?
Command: git stash
Hint: Used when you want to save local changes temporarily that are not yet committed. Its primary use is when you want to switch a branch or work on a different task.
How do you use stashed changes in the git local repo so as to commit to the remote repository?
Command: git stash apply
Hint: It helps to use the changes that were stashed earlier. Make sure that before applying the changes, you switch to the repository for which changes were made.