Kickstart your career with best deals on top training courses NY10 Click to Copy

Git Interview Questions

Practice and test yourself with the top GIT interview questions. These are answered by the experts and will be your best guide to surviving the trickiest GIT interviews. Convert your next GIT interview into a sure job offer.

  • 4.7/5 Rating
  • 20 Question(s)
  • 33 Mins of Read
  • 3266+ Reader(s)

Beginner

Git is a distributed and decentralized source control system(SCM); however, it can be centralized. Effectively, most of our operations are going to be local; the only time we need a network connection is when we're pushing our changes up to the remote repositories.

Answer: In Git, we think in terms of version 'snapshots', not versioning any specific 'files or folders' and this is a big difference from other version control systems.

The Git repository contains all the files, the history and configuration, all these things that are managed by Git.

A commit is the group of -

  1.      Saved changes to the Git repository
  2.      This impacts history
  3.      Uniquely identified by a SHA-1 hash

Branches can be thought of as a timeline with commits. By default, the master branch is the main, primary branch that we usually work with. HEAD is a pointer to the last commit on the current branch. Remote is simply a refine, or a pointer, to a related repository somewhere that's not local and that could be within local network or enterprise network or somewhere out on the internet. Some examples of places that might be hosting remote repositories would include GitHub, Gitlab, Bitbucket etc. 

On the terminal/cmd using 'git version' command; it will show the installed git's version. Using command 'git help'; we can display the list of commands supported by the git. Using 'git help <command-Name>' we can get the detailed information about specific git command; on windows it will be displayed in the web-browser but on Mac/Linux it will be displayed on the terminal itself. Command 'git help -a' gives a list of subcommands.

Using 'git status' command in the repository working directory, when this command is used it will display-

  • The current branch name
  • If it is an initial commit or not
  • List of the unstage/untracked files(which are newly added or modified but not added to staging/index) List of the files pending to commit(these files are previously added to stating/index)

If there is no file/folder to add or commit then it will just display name of the branch and message "nothing to commit, working directory clean"

There are two ways to add or stage the files or the changes to the git repository.

Using 'git add' command, this will add the file to staging

Using 'git commit -am <your custom message>', the '-a' option with commit command will add the files to staging(not to 'newly added files' but only that have been modified and deleted) and commit too. basically 2 in 1, it adds then commit the files. '-m' options for providing the custom commit message.

 If the git status command displays 'myfile.txt' as file pending to commit then

  1. To unstage a file(which is not yet committed but in staging stage); command 'git reset <file-name>' is used. After using 'git reset HEAD myfile.txt' command; 'git status' command will not show myfile.txt in the list of files pending to commit, now it will be displayed under the list of the unstage/untracked files.
  2.   Now to discard the changes('myfile.txt') in working directory; command 'git checkout -- <file-name> is used. After using 'git checkout -- myfile.txt' command; 'git status' command will not show myfile.txt in the list of unstage/untracked files as this file 'myfile.txt' will be replaced with the last committed version from the repository.

When you are working in your working directory and done few changes so few items in staging, and now if you have to provide any hot fix based on the last commit on the remote repository then to do it the best way is save the current state of the working directory and the index/staging (something like on a stack), for that we use command 'git stash' or 'git stash save'. After using this command, the current working directory will be having the same match as on the remote last commit means locally no changes exist to stage or commit. Now you can do your stuff on the clean copy of the remote; git stash will not cover git ignored files and the newly created files but not 'staged' yet in current working directory.

Advanced

  1.     For starting local, we initialize our current working project directory using 'git init' or 'git init <your-project-name>'(this will create a new directory with provided your-project-name as working directory) command and on the GitHub site set up your GitHub repository.
  2.      Now in the local working directory, we do changes (either adding/editing files etc)
  3.      Then we're going to 'stage' our changes using the "git add" command
  4.     Then commit our changes that are in the staging area using the 'git commit -m <your custom message here>'
  5.  Setup a remote repository (origin) using command 'git remote add origin git@github.com:User/UserRepo.git' (you can change it later using command 'git remote set-url origin git@github.com:User/UserRepo.git')
  1.    Once we're ready to collaborate with others on the main repository, we'll push our changes up to our remote repository on GitHub(or the hosted remote repository) using command 'git push -u origin master'; here -u is upstream(use it only for first push command just once), origin is remote name and master is the branch's name.
  2.    Once everyone else had pushed their changes to the remote repository,then we'll do a pull from the remote repository to our local git repository using 'git pull' or 'git pull <remote-name> <branch-name>' command
  3.     Later on whenever doing new working the local repo; do 'git pull' and then start new work.

for example using GitHub as a remote repository

  1.      On the GitHub set up your remote GitHub repository
  2.    Then do a git's clone using 'git clone' command to create a new repository on our local system.
  3.      Now in the local working directory we do changes(either adding/editing files etc)
  4.      Then we're going to 'stage' our changes using the "git add" command
  5.     Then commit your changes that are in the staging area using the 'git commit -m <your custom message here>'
  6.  Setup a remote repository(origin) using command 'git remote add origin git@github.com:User/UserRepo.git' (you can change it later using command 'git remote set-url origin git@github.com:User/UserRepo.git')
  7.    Once we are ready to collaborate with others on the main repository, we'll push our changes up to our remote repository on GitHub(or the hosted remote repository) using command 'git push -u origin master'; here -u is upstream(use it only for first push command just once), origin is remote name and master is the branch's name.
  8.    One everyone else had pushed their changes to the remote repository,then we'll do a pull from remote repository to our local git repository using 'git pull' or 'git pull <remote-name> <branch-name>' command
  9.      Later on whenever doing new working the local repo; do 'git pull' and then start new work.

When reading/getting, the values are read from the system, global and repository local configuration files by default, and options --system, --global, --local and --file <filename> can be used to tell the command to read from only that specific location. When writing, the new value is written to the repository local configuration file by default(--local), and options --system, --global, --file <filename> can be used to tell the command to write to that location.

Set a user's email-id (to be recorded in any newly created commits) : git commit --global user.email "sc.2017.india@gmail.com" 

Set a user's full name (to be recorded in any newly created commits) : git commit --global user.name "Sandeep Choudhary"

Set signed key for a signed tag or commit : git config --global user.signingkey <your-signed-key-here> Command 'git config --global --list' will show all of the current global settings/options.

Configuration saved using '--global' is stored in a file '.gitconfig' in the current user's home directory. User's name and email-id should be set before using git further.

Using command ‘git log"; this command displays commits list(in chronological order) from latest to old one in descending order of the committed date. For each commit record it displays- 

Commit id (SHA-1 has code)

Author's (user's) name and email-id Date and time

Commit message provided at time of commit

To make the output more compact or custom; we can use lot of other options along with command e.g. 'git log -- oneline'

To remove a file for example 'myfile.txt'; which is already committed and a part of repository, the first step is to 'staging' this file deletion using 'git rm myfile.txt' command. After this command if we use 'git status' it will show message e.g. 'deleted: myfile.txt' . Now as second step to make it permanently we have to commit this staging using 'git commit -m <your message>' command.

Another way is if we remove the file 'myfile.txt' from the working directory manually for example using terminal rm command or using menu as we delete file normally using operating system's GUI. post manual removal the 'git status' command will display the message 'Changes not staged for commit:' and file information like 'deleted: myfile.txt'. To stage these manual changes(we can say same behaviour as working directory changes of add/remove/update files) we have to use -u (recursively update) option with add e.g. 'git add -u'(pre git 2.0 version) or 'git add .'(git 2.0 and above version) command. Then use git commit command to make them permanent.

For example if we have a file called 'myfile.txt' to restructure/move in the sub directory 'mydir' under git's working directory; rather than cut and paste(manual mv command), we can use 'git mv <file-name> <target-dir-name>' e.g. 'git mv myfile.txt mydir'. This will move the file to provided directory and marks it to 'staging'. If we use 'git status' command now, it will display changes available to commit e.g. 'renamed: myfile.txt -> mydir/myfile.txt'. Now using git commit command make this restructuring permanently in the repository.

If we move file/folder out of the working directory (basically like permanently removing a committed file from the repository) then post movement we have to use 'git add -u' command to mark it staging and then commit command to make it permanently.

In the working directory, create a new file '.gitignore'; basically this file's each line will contain a entry(file or folder name or file extension with filter) to be ignored by git to add or commit in the repository. for example if we want ignore all of the file with '.debug' extension then add a entry '*.debug' in the file. Stage this file '.gitignore' using 'git add .' and then commit it to repository.

  1. To do SSH setup for git; we need to create a '.ssh' directory in the user's home directory.
  1. Then in inside (using 'cd' command) this (e.g. '/Users/SandeepChoudhary/.ssh') newly directory, use command 'ssh-keygen -t rsa -C "<your-email-id>"'. Here -t is used to specify type of key to create and -C(in upper case only) to provide the comment in this case the email-id.
  2. Then during execution of this command, provide inputs like name of file to save the key(just press enter so by default it will be saved in 'id_rsa' file, provide passphrase. Now a file 'id_rsa.pub' will be created in '.ssh. Directory.
  3. Now on the github under 'Account Settings' > 'SSH keys' > 'Add SSH key', provide custom title as this ssh key will be tightly coupled with your specific machine where ssh key is generated. Under key option, copy paste the content of the 'id_rsa.pub' file from your machine to here and click 'add key'.
  4. Now on your machine using command 'ssh -T git@github.com' you will establish a ssh connection/tunnel with github.

We add a remote repository(origin) using command 'git remote add origin git@github.com:User/UserRepo.git' (you can change it later using command 'git remote set-url origin git@github.com:User/UserRepo.git').

We can list the remote repositories using 'git remote -v' command and this will display the name and the url of the remote repositories.

We push our changes up to our remote repository using command 'git push -u origin master'; here -u is upstream(use -u only for first push command just once), origin is remote name and master is the branch's name.

Then we'll do a pull from remote repository to our local git repository using 'git pull' or 'git pull <remote-name> <branch-name>' command to receive all your remote changes (commits) from the remote repository.

Using the command 'git stash list'. The output of this command is a list of the saves states starting from zero the latest to the oldest one. Each entry has the stash index starting from zero e.g. stash@{n} where n is index, followed by the branch name and the summarized description.

If at any point of time we want to remove a single stashed state from the stash list, we use the command 'git stash drop'(it will remove the latest from list i.e. stash@{0}) or 'git stash drop stash@{n}'; here stash@{n} must be a valid stash log reference as displayed in stash list command's output.

Description

Git is an open-source distributed version control system specially designed to manage everything right from small to very large projects with speed and performance. For an individual, it is easy to learn and has a small footprint with fast performance. Git basically outclasses various tools like CVS, ClearCase, Perforce, and Subversion with features like convenient staging areas, cheap local branching.
 

Learning Git will fetch you a huge job opportunity because 99% of the reputed companies are using Git and GitHub. Learning Git will make you more hirable and help you differentiate yourself from others. People with Git knowledge are offered handsome annual packages.
 

Every candidate faces jitters when it comes to Git interview. If you are planning to build a career as a developer yet facing troubles in cracking the Git interview,  so here are the best samples of advanced Git interview questions. These interview questions on Git will aid you to crack your Git interview and help you in achieving your dream job. Git is a great field for those who seek advancement in their career and will help to build your concepts around Git and ace the GIT interview smoothly.
 

These were a few top git interview questions about Git that you need to prepare. Though Git is much deeper and vast, these interview questions will help you a lot to get through.


Crack your git interview easily today. All the best!

Read More