What We Are Learn On This Post
Git Interview Questions: This, we are trying to share the Git stash tutorial point & also few more Git Interview Questions, which helps all the job seekers and even the job seekers. For more interview questions, java tutorials, and selenium tutorials, visit our softwaretestingo blog.
Git Stash Tutorial Point & Git Interview Questions
What is GIT?
Ans: GIT is a distributed version control system and source code management (SCM) system with an emphasis on handling small and large projects with speed and efficiency.
What is a repository?
Ans: A repository contains a directory named. Git, where it keeps all of its metadata for the repository. The content of the .git directory is private to git.
What is the command you can use to write a commit message?
Ans: The command that is used to write a commit message is âgit commit âa.â The âa on the command line instructs git to commit the new content of all tracked files that have been modified. You can use âgit addâ before git commit âa if new files need to be committed for the first time.
What is the difference between GIT and SVN?
Ans: The difference between GIT and SVN is
- Git is less preferred for handling huge files or frequently changing binary files, while SVN can handle multiple projects stored in the same repository.
- GIT does not support âcommitsâ across multiple branches or tags. Subversion allows the creation of folders at any location in the repository layout.
- Gits are unchangeable, while Subversion allows committers to treat a tag as a branch and to create multiple revisions under a tag root.
What are the advantages of using GIT?
Ans:
- Data redundancy and replication
- High availability
- Only one.git directory per repository
- Superior disk utilization and network performance
- Collaboration friendly
- Any projects can use GIT
What language is used in GIT?
Ans: It is fast, and âCâ language makes this possible by reducing the overhead of runtimes associated with higher languages.
What is the function of âGIT PUSHâ?
Ans: âGIT PUSHâ updates remote refs along with associated objects.
GIT Official Website || Read Also:đŚ DevOps Interview Questions
Why GIT better than Subversion?
Ans: It is an open-source version control system; it will allow you to run âversionsâ of a project, which show the changes that were made to the code over time; also, it will enable you to keep the backtrack if necessary and undo those changes. Multiple developers can check out and upload changes, and each difference can then be attributed to a specific developer.
What is the âStaging Areaâ or âIndexâ?
Ans: Before completing the commits, it can be formatted and reviewed in an intermediate area known as âStaging Areaâ or âIndex.â
What is GIT stash?
Ans: It stash takes the current state of the working directory and index and puts in on the stack for later and gives you back a clean working directory. So in case if you are in the middle of something and need to jump over to the other job, and at the same time, you donât want to lose your current edits, then you can use GIT stash.
What is the GIT stash drop?
Ans: When you are done with the stashed item or want to remove it from the list, run the git âstash dropâ command. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.
How will you know if a branch has been already merged into master?
Ans: Git branchâmerged lists the branches that have been merged into the current branch
Git branchâno merged lists the branches that have not been merged
is the function of the git clone?
Ans: The git clone command creates a copy of an existing Git repository. To get a copy of a central repository, âcloningâ is the most common way used by programmers.
Check Also: git interview questions
What is the function of âgit configâ?
Ans: The âgit configâ command is a convenient way to set configuration options for your Git installation. The behavior of a repository, user info, preferences, etc. can be defined through this command.
What does a commit object contain?
Ans: 1. A set of files, representing the state of a project at a given point of time
2. Reference to parent commit objects
3. An SHAI name, a 40 character string that uniquely identifies the commit object.
How can you create a repository?
Ans: In Git, to create a repository, create a directory for the project if it does not exist, and then run the command âgit init.â By running this command, the .git directory will be created in the project directory, and the directory does not need to be empty.
Read Also: Task Management Tool Jira Interview Questions
What is âheadâ in git, and how many heads can be created in a repository?
Ans: A âheadâ is simply a reference to a commit object. In every repository, there is a default head referred to as âMaster.â A repository can contain any number of heads.
What is the purpose of branching?
Ans: The purpose of branching in GIT is that you can create your branch and jump between those branches. It will allow you to go to your previous work, keeping your recent work intact.
What is the common branching pattern?
Ans: The common way of creating a branch in GIT is to maintain one as the âMainâ branch and create another branch to implement new features. This pattern is particularly useful when multiple developers are working on a single project.
How can you bring a new feature to the main branch?
Ans: To bring a new feature to the main branch, you can use a command âgit mergeâ or âgit pull command.â
What is âconflictâ?
Ans: A âconflictâ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.
How can conflict in git resolve?
Ans: To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running âgit addâ after that to commit the repaired merge, run âgit commit.â Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.
To delete a branch, what the command that is used is?
Ans: Once your development branch is merged into the main branch, you donât need a development branch. To delete a branch use, the command âgit branch âd [head].â
What is another option for merging in git?
Ans: âRebasingâ is an alternative to merging in git.
What is the syntax for âRebasingâ?
Ans: The syntax used for rebasing is âgit rebase[new-commit] â
What is the difference between âgit remoteâ and âgit cloneâ?
Ans: âgit remote addâ creates an entry in your git config that specifies a name for a particular URL. While âgit cloneâ creates a new git repository by copying an existing one located at the URI.
What is GIT version control?
Ans: With the help of GIT version control, you can track the history of a collection of files and includes the functionality to revert the collection of files to another version. Each version captures a snapshot of the file system at a certain point in time. A collection of files and their complete history are stored in a repository.
Mention some of the best graphical GIT clients for LINUX?
Ans: Some of the best GIT client for LINUX is
- Git Cola
- Git-g
- Smart git
- Giggle
- Git GUI
- qGit
Read Also: svn interview questionsÂ
What is Subgit? Why use Subgit?
Ans: âSubgitâ is a tool for a smooth, stress-free SVN to Git migration. Subgit is a solution for a company-wide migration from SVN to Git that is:
- It is much better than git-svn
- No requirement to change the infrastructure that is already placed
- Allows to use all git and all sub-version features
- Provides genuine stress âfree migration experience.
What is the function of âgit diff â?
Ans: âgit diff â shows the changes between commits, commit and working tree, etc.
What is âgit statusâ is used for?
Ans: As âGit Statusâ shows you the difference between the working directory and the index, it helps understand a git more comprehensively.
What is the difference between the âgit diff âand âgit statusâ?
Ans: âgit diffâ is similar to âgit status,â but it shows the differences between various commits and also between the working directory and index.
What is the function of âgit checkoutâ?
Ans: A âgit checkoutâ command is used to update directories or specific files in your working tree with those from another branch without merging it into the whole branch.
What is the function of âgit rmâ?
Ans: To remove the file from the staging area and also of your disk, âgit rmâ is used.
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 the use of a âgit logâ?
Ans: To find specific commits in your project history- by author, date, content, or history âgit logâ is used.
What is âgit addâ is used for?
Ans: âgit addsâ adds file changes in your existing directory to your index.
What is the function of âgit resetâ?
Ans: The function of âGit Resetâ is to reset your index as well as the working directory to the state of your last commit.
Check Also: Jenkins Interview Questions
What is git Is-tree?
Ans: âgit Is-treeâ represents a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree.
How is git instaweb used?
Ans: âGit Instawebâ automatically directs a web browser and runs the webserver with an interface into your local repository.
What does âhooksâ consist of?
Ans: This directory consists of Shell scripts that are activated after running the corresponding commands. For example, IT will try to execute the post-commit script after you run a commit.
Explain what a commit message is?
Ans: Commit message is a feature of git which appears when you commit a change. It provides you with a text editor where you can enter the modifications made in commits.
How can you fix a broken commit?
Ans: To fix any broken commit, you will use the command âgit commitâamend.â By running this command, you can fix the broken commit message in the editor.
Why is it advisable to create an additional commit rather than amending an existing commit?
Ans: There are a couple of reason
1. The amend operation will destroy the state that was previously saved in a commit. If itâs just the commit message is changed, then thatâs not an issue. But if the contents are being amended, then chances of eliminating something important remain more.
2. Abusing âgit commit- amendâ can cause a small commit to grow and acquire unrelated changes.
What is âbare repositoryâ?
Ans: To co-ordinate with the distributed development and developers team, especially when you are working on a project from multiple computers, âBare Repositoryâ is used. A bare repository comprises a version history of your code.
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 a 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.
Update hook works similarly to pre-receive hook and is also triggered before any updates are made. However, the update hook 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 they can be created 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 to find bad commits. Instead of making the user try out every single commit to find out 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 also maintains a number of aliases to certain commits, known as refs. Also, 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, HEAD~1 refers to the committed parent to HEAD. HEAD~2 refers to the grandparent of the HEAD, and so on. In the case of merge commits, where the commit has two parents, ^ can be used to select one of the two parents, e.g., HEAD^2 can be used to follow the second parent.
And finally, respect. These are used to map local and remote branches together. However, these can be used to 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 be used to 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 to be merged into 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 âbare repositoryâ in Git?
Ans: You are expected to tell the difference between a âworking directoryâ and â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 of 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 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.
GIT Commands
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 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 branch(don’t rebace published comments): $ git rebace <branch>
- Abort a rebace: $ git rebace –abort
- Continue a rebace after resolving conflicts: $ git rebace –continue
- Use your configured merge tool to solve conflicts: $ git mergetool
- Use your editor to manually solve conflicts and (after resolving ) mark 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>
Leave a Reply