Git Interview Questions

  • |
  • 21 September 2023
Post image

Git is a tool for keeping track of changes in computer programs. It’s free to use and lots of people in the software field use it. Now, let’s check out some questions that might come up in an interview.

Q: What is the difference between git init and git clone?

Both of them are used to create git repository.

  • git clone: clones a repository into a newly created directory. Mainly used to download after developers repositories'.
  • git init: creates an empty git repository. It is a directory in which contains .git directory.

Q: What do we mean by Three Trees of Git?

Three Trees of git actually refers to the Git’s internal state management system. These trees are node and pointer based data structures in which Git uses to tract a timeline of edits.

  • The working directory(first tree): This tree is sync with the local filesystem
    • This tree represents the immediate changes made to files and directories
  • Staging index(second tree): This tree tracks Working Directory changes in which have been with git add command.
    • And these changes will be stored in the next commit.
    • This tree includes complex internal caching mechanism.
    • If you want to list or view the changes in the staging index, you have to use known command with git prefix such as git ls-files
    • For instance git ls-files list all files in which git has aware of.
  • Commit history(third tree): When you use the git commit command, it takes the changes you made and puts them into a permanent snapshot that stays in the Commit History.
    • This snapshot also shows what was in the Staging Index when you made the commit.

Q: Explain the purpose of the git status command

  • It is used to display state of the repository and staging area.
  • With this command we can see the which changes have been staged which files aren’t being tracked by Git

Q: Explain git add and git commit commands

  • git add: Adds new or changed files in our working directory to the Git staging area
  • git commit: It creates a permanent copy of the currently staged changes.

Q: Explain the git checkout command

  • Updates file(s) in the working tree to match in the latest snapshot(latest point in the tree) or the specified point in the tree
  • This command is also used to create new branch with option -b => git checkout -b myNewBranch
    • If you use command without -b, then it switches you to specified branch

Q: How to switch between branches

Use git checkout:

# switch to master branch
git checkout master

# switch to branch called dummyBranch if exists
git checkout dummyBranch

Q: How to undo commit

There are some situations for this question:

Undo local changes that have not been committed:

  • These are the changes you did in your working directory but git has not been added to its index(no sha-1 hash value for these changes)
  • Then, just use git checkout command. Example => git checkout fileName

Undo last commit (that has not been pushed)

Let’s say you made a mistake in your your last commit but not pushed to remote repository.

  • You can revert your commit with the following command: git reset --soft HEAD~
    • With this command your latest commit will be undone. And your changes will go back to staged area.

Undo specific commit (that has not been pushed)

Let’s say you made local commits but not pushed to remote repository yet. And you want to change your commit or reset things back to previous good commit.

  • First find the commit unique hash (you may use git status), you have to find your unique hash such as 2cf1254ga…
  • Then run the following commands (there are ways to use git reset commit, you may search on the internet)
    • git reset 2cf1254ga or if you want git reset --hard 2cf1254ga

The safest option is the git reset (without –hard) , in this case, commits will be removed, but the changes will appear as uncommitted, giving you access to the code.

Undo specific commit (that has been pushed)

Let’s say you made a changes and pushed to the remote repository, but you want to revert it.

  • First find the commit hash value such 2cf1254ga..
  • Run the following command: git revert 2cf1254ga or git rever 2cf1254ga --no-edit
    • If you use to command without --no-edit, git expects a revert commit message from you. You can bypass the revert-commit message (git will automatically add default message)
    • After all, you will have new commit that is the opposite of the existing files
    • Finally don’t forget to push the changes: git push

Q: How to list all changes in git repo?

Use git log command:

$ git log

commit 21815196d4f85aa81a1aec93a112540827905e40 (HEAD -> master, origin/master)
Author: mehmetozanguven ...
Date:   Tue Sep ...

    Add new blog for git
...

Q: Explain git show

  • It is used to display detailed information about a specific Git object, such as a commit, tag, or blob (file content).
  • use it to view the changes introduced by a particular commit or to inspect the metadata associated with a Git object.

Q: Explain the purpose of the file .gitignore

  • Contains untracked files that Git should ignore. If file has already been tracked by Git, then that file are not affected.
  • It is mainly used to ignore some specific file from Git, such as .env file (includes secret key etc..), or directory(ies)
  • Each line in the .gitignore specifices a pattern

Q: How to add/merge one branch to another

  • Use git merge command

Let’s say you want to add your implementation in the branch called newFeature into your master branch. Then you may run the following commands:

# switch to master
git checkout master
# merge branch into master
git merge newFeature

Q: What is conflict in git

  • Conflict occurs when there are conflicting changes made to the same part of a file in different branches or commits.
  • typically arise during the process of merging or rebasing branches.
  • Git identifies these conflicts because it cannot automatically determine which changes should take precedence.

Q: What is remote repository in git

  • Remote repository is a Git repository that’s hosted on the internet or another network
  • Used for collaboration and version control in distributed software development.
  • Allow multiple developers to work on the same project

Let’s also talk about some key points:

  • Origin: The repository that we cloned from is called as origin
  • Fetching and pushing: To get the latest changes from a remote repository, use commands like git fetch & git pull
    • git fetch: retrieves changes from the remote repository without automatically merging them into local branch.
    • git pull: It does both fetching and merging a single step.

Q: Explain the git remote command

  • It lets us create, view and delete connections to other repositories.
  • git remote -v: list the remote connections:
$ git remote -v
origin  [email protected]:mehmetozanguven/mehmetozanguven.com.git (fetch)
origin  [email protected]:mehmetozanguven/mehmetozanguven.com.git (push)
  • git remote add <name> <url>: creates a new connection to a remote repository. You will be use <name> as a shortcut for the <url> in other Git commands
  • git remote rename <oldName> <newName>: rename a remote connection
  • git remote rm <name>: remove the connection to the remote repository

You May Also Like

Using Git Professionally
  • 19 Sep, 2023

Using Git Professionally

In this long blog, we are going to improve our git knowledge. I will try to explain the some concepts behing the basic things in git. Writing perfect …