GIT Interview Questions & Answers

GIT Interview Questions & Answers

GIT is a distributed version control system and source code management (SCM) system

with an emphasis to handle small and large projects with speed and efficiency.

We work in our local machine and later we transfer the code to Centralised repository

(GitHub). We don’t need to connect to a centralised repository to work.

• GIT version control allows you to track the history of a collection of files (code files).

• It supports creating different versions of file collection. Each version captures a

snapshot of the files at a certain point of time and You can revert the collection of

files using the snapshot. (You can develop the code in different versions of java. and

you can merge in Git)

• VCS allows you to switch between these versions. These versions are stored in a

specific place, typically called a repository. (You can switch between different

versions of java in between development process)

                      SVN                          GIT
SVN is centralised repository, that meansdirectly we involved in the centralisedrepository.Git is distributed repository, first we areworking in our laptop after that we aretransferring the code from our laptop tocentralised repository. Git have threephases the phases are work space,staging/index, local repo.
We working on SVN means if we are facingany networking issue we can’t work on SVNbecause of we are directly involve into thecentralised repository.In git we are doing in local systems only sono need to internet connection, whenpushing the code from our system tocentralised repository at that time we neednetwork connection. Without network also,We can do some work.
Developed directly interact with thecentralised repositoryDevelopers not directly interact with theCentralised repository.

A Git repository contains the history of a file.

By using # git init command creates a local repository.

A bare repository in Git just contains the version control information and no working

files (no tree) and it doesn’t contain the special .git sub-directory.

# git config –global user.name “user_name”

# git config –global user.email “user_email”

# git config –global alias.lo “log –oneline” —–> To create an Alias to Command

# git config –global –unset alias.lo —–> To Remove an Alias

# git config –global –unset user.name —–> to remove username

To download an existing repository from Centralised (Github) to the local system.

# git clone <url>

To add files from the work area to the Index/staging/cache area.

# git add <file_name1> <file_name2>

staging area means “holding area”. Before the commits, it can be formatted and reviewed in

an intermediate area known as staging or Index Area.

To see the commits. Also, we can find specific commits in your project history- by author,

date, content or history.

# git log —–> To show the Git Commits

# git log -5 —–> To show Recent 5 Commits

# git log –oneline —–> To Display the each commit in one line

# git log –since=2018-01-21

# git log –until=2018-03-18

# git log –author=”user_name”

# git log –grep=”Index”

# git log –oneline –author=”user_name”

# git commit -a -m “Do Something once more”

# git commit –amend -m “This is your new Git Message”

# git reset –soft <previous_commit id>

# git reset head <file_name>

# git reset –mixed <previous commit id>

Reset the current HEAD state to a specific state.

A ‘head’ is simply a reference to a commit object. In every repository, there is a default

head referred as “Master”. A repository can contain any number of heads.

Keep the files names in .gitignore then that files not add and commit, just skip that files

while adding and committing.

# git diff <commit_id1>..<commit_id2>

# git checkout –<file_name>

# git branch <branch_name>

# git checkout <branch_name>

# git checkout -b <branch_name>

# git branch -m <old_branch_name> <new_branch_name>

# git branch

# git branch -r

Or

# git remote show origin

# git branch -a

# git branch -d <branch_name>

Or

# git branch -D <branch_name>

# git push origin -d <branch_name>

# git diff <branch1>..<branch2 >

git push is to push commits from your local repository to a remote repository.

#git push (you must be in master branch)

#git push origin <branch_name>

(or)

#git push –set-upstream <branch_name>

#git push <github_repository_path> <branch_name>

(or)

#git push –set-upstream <branch_name>

Git pulls downloads and merges a ‘branch data’ from remote repository to local repository.

It may also lead to ‘merge conflicts’ if your local changes are not yet committed. Use ‘git

stash’ command to Hide your local changes before git pull.

# git pull (git fetch + git merge.)

# git pull origin <branch_name>

# git fetch origin <branch_name>

# git checkout <downloaded_branchname>

git fetch is only downloads new data from a remote repository, but it doesn’t integrate

any of the downloaded data into your working files. All it does is provide a view of this

data.

# git fetch <branch_name>

# git fetch origin <branch_name>

• If you want to download the whole existing repository then use Git Clone.

• If you have already repository but you want to take new updates of existing

repository then use the git pull command.

Git merge is used to combine two branches.

# git merge <branch_name>

Note: you should be in the target branch. Then run the command

For example, if you and another person both edited the same file on the same lines in

different branches of the same Git repository, you’ll get a merge conflict error when you

try to merge these branches. You must resolve this merge conflict with a new commit

before you can merge these branches.

Will inform the developers regarding this merge conflict. They will change the code and

inform us. edit the files to fix the conflicting changes and then add & commit.

#git merge –abort

To remove the file from the work area/staging area and also from your disk ‘git rm’ is

used. You can revert a deleted file.

if it is deleted using ‘git rm’. If you deleted a file ‘rm’ command then you can’t get it.

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.

Git supports branching which means that you can work on different versions of your

collection of files. A branch allows the user to switch between these versions so that he

can work on different changes independently from each other.

We have developed one module in one branch and another module in another branch.

After the development, based on the requirement we merge these two branches.

Or One branch is the development branch, another branch is the test branch.

Feature branching

A feature branch model keeps all of the changes for a particular feature inside of a branch.

When the feature is fully tested and validated by automated tests, the branch is then

merged into master.

Task branching

In this model each task is implemented on its own branch with the task key included in the

branch name. It is easy to see which code implements which task, just look for the task key

in the branch name.

Release branching

Once the develop branch has acquired enough features for a release, you can clone that

branch to form a Release branch. Creating this branch starts the next release cycle, so no

new features can be added after this point, only bug fixes, documentation generation, and

other release-oriented tasks should go in this branch. Once it is ready to ship, the release

gets merged into master and tagged with a version number. In addition, it should be merged

back into the develop branch, which may have progressed since the release was initiated.

Stashing takes the Temporary stored state of your working directory.

# git stash save “<message>” ——> to store the data into stash

# git stash list ——> to see the stash list

# git stash apply <stash#> ——> to copy the data into branches

# git stash pop <stash#> ——> to move the data into branches

# git stash drop <stash#> ——> to delete the particular stash

# git stash clear ——> delete the entire stash list

• If you are checking out from one branch to another branch but you have

uncommitted file that you don’t want to move then keep that file in the stash area.

• When you are merging two branches and you don’t want some files to merge, then

we move those files to the stash area.

• When you are pulling (fetch + merge) a branch/file and you don’t want some files to

merge, then we move that files to the stash area.

# git rebasing command is an alternative to merging in git.

• git merge applies all unique commits from branch A into branch B in one commit with

final result.

• git rebase gets all unique commits from both branches and applies them one by one.

• git merge doesn’t rewrite commit history, just adds one new commit

• git rebase rewrite commit history but doesn’t create extra commit for merging

# git revert <commit_id>

# git remote set-url origin git://this.is.new.url

Take some changes from a particular branch and bring them into another branch.

Git is an open source version control system; it will allow you to run ‘version’ of a project.

Multiple developers can check out, and upload changes and each change can then be

attributed to a specific developer.

• On GitHub, navigate to the main page of the repository.

• Under your repository name, click Settings.

• In the left menu, click Branches.

• Select the branch you want to mark protected using the drop-down menu.

• Select Protect this branch.

• On GitHub, navigate to the main page of the repository.

• Under your repository name, click Settings.

• Scroll to the bottom of the page and you will find Delete this repository button

• When you click on that button, another pop up will appear, here you need to type in

the name of your repository name and click on the button bellow which says: I

understand the consequences, delete the repository.

You can invite users to become collaborators to your personal repository.

• Under your repository name, click Settings.

• In the left sidebar, click Collaborators.

• Under “Collaborators”, start typing the collaborator’s username.

• Select the collaborator’s username from the drop-down menu.

• Click Add collaborator.

• The user will receive an email inviting them to the repository. Once they accept your

invitation, they will have collaborator access to your repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin