In this article, we will discuss GIT INTERVIEW QUESTIONS.(Part-2)
Please check part-1
1. What are different state of git for commiting the code in remote repository
a. modified(local modification)
b. stagged(adding the local code to stagging area,command:git add .)
c. commited(Commit to remote repo, git commit -m “commit”, git push origin branch name)
2. What is the difference between tag and branch, when to create tag and when branch
Tags are created to stop further commits or it is called code freeze.
Tag are created for the intermediate releases to freeze the code
Before creating tag, make sure from which branch u are creating tag
Then checkout that branch(git checkout master) if want to create a tag from master
then create tag(git tag 10.0.1) if u want to create tag with 10.0.1
git tag -a 1.0.0 -m “commit message”
git push origin tag 1.0.0
Branches are created as part of the development process, and git branching strategy
git checkout -b 6.0.0
git push origin refs/heads/6.0.0:refs/heads/6.0.0
3. How git store the files or source code
Git stored the source code in layared manner like docker.
it will stored the delta changes in layars.
Suppose size of the branch 10mb, and if the repo has 5 branches, it doesn’t mean like the repo size is 50mb approx.
The repository size will be little higher than 10mb
4. What is the default branch in git while checkout, how to change the default branch to any other branch
Master branch is the typical default branch is in git. But it can be changed in repository settings (settings —>Change the default branch to the new branch you want)
5. How restore a specific commit code and command used for this
This action can be performed by Git Cherry picking and its used to address the wrong commits by mistake or so.
And you can merge from one branch to another of a specific commit (i.e commit id/SHA code like(a3c7e5aeabffb6c20bad5239a38949acb9ce00c2))
Each and every commit has a commit id .
a. Make sure there is no conflicts and if any needs to resolve first.
b. Checkout to the branch where u want to merge(git checkout branch name, ex:git checkout master)
c. Take the commit id/SHA code(a3c7e5aeabffb6c20bad5239a38949acb9ce00c2) of the other branch and run command: git cherry-pick SHA code(ex: git cherry-pick a3c7e5aeabffb6c20bad5239a38949acb9ce00c2)
d. git push origin the branch name(Ex: git push origin master)
6. What is code review/peer review in git and how it used to work
Code Review/Peer review is the process of moving the code from the lower branch to higher(feature–>develop–>master) and its part of development process.
Each and every branches have different owner, everybody can’t push code for every branch, like developer can’t push the code to develop or master branch.
Once developer do the changes in feature branch and its get the QA sign off, needs to push code the the develop branch, no access to develop branch.
To push the developer need to create a pull request(PR). And owner of the development branch will review the code and can reject also based on the code review.
Once approved , feature branch code will merged in development branch. This process is called code review or peer review.
7. You are working in your local machine ,what is the command to get the list of branches avaialable for the repo?
Ans: Command: git branch -a
Meanwhile somebody remove/deleted one branch from the remote repo .if u r giving the above command(git branch -a) still u will get all the branches as the repo is still local.
What is the command check the deleted branch
Command:git remote show origin
HEAD branch: master
refs/remotes/origin/test1 stale (use ‘git remote prune’ to remove)
Local branch configured for ‘git pull’:
master merges with remote master
Local ref configured for ‘git push’:
master pushes to master (up to date)
and remove the branch from local machine
command:git remote prune origin
will remove from your local machine/repo
8. What is the difference between git pull, pull request and git push?
Git pull is used to get the remote changes of the remote repo to the local.
Pull Request(PR) is a process of merging the code from one branch to another(feature to develop or develop to master) or one repo to another repo(repo1 to repo2, but base should be one repo) in Distributed Source code system(DVS) when you don’t have permision.
git push is used to push the local change to the remote repo(like git push origin master)
9. What are tracked files and untracked files in git?
Tracked files are the files which git knows with the changes, Un-tracked files are not known to the git.
Suppose you are adding new files to the local repo and those files are called untracked files.
To change the untracked files to track files use command :git add .
10. Command to delete branch from local and remote repo
local: git branch -d hotfix
remote: git branch -d remote hotfix