Git
Git
Git
Working on a Project
Advantages of Versioning
Collaboration
When working on a huge project with many files which is being developed
by multiple people it is hard to manually keep track of the changes.
Versioning
There are several tools that help us manage versions of the source code of
software.
Git
Subversion
Mercurial
Git
Git is a free, open-source and most widely used distributed version control
system.
Repository
Snapshots
Tracking Files
Git creates a snapshot of all the changes that are part of staging
area.
Tracked Files: The set of files which are watched by Git for any changes.
Modified Files: These are the files which are modified after the latest
snapshot.
Committed Files: These are the unmodified files which are same
since the latest commit.
Everyone has a copy of the entire repository with the entire version
history.
Git Repository
There are several cloud-based repository hosting services which let you
maintain a copy of repository
GitHub
Bitbucket
Create Account
Creating a Repository
Installing git in your system to manage & work with Git Repositories.
In Linux :
In MAC :
Configure who gets credit for the changes made from your device by
setting the author details from your terminal.
To set automatic command line coloring for Git for easy reviewing.
git config -l
Sample output :
user.name=User
color.ui=auto
Cloning Repository
git clone
The
.git/
folder present in the repository contains all these versions and information
required for version control.
Example :
git remote -v
Sample output :
Apart from cloning a repo, you can also initialize a git repo in an existing
folder.
git init
initializes an empty git repository in the working directory.
Syntax :
git init
Syntax :
Example :
Warning
git clone, git push and git pull commands with password. You need to
create a personal access token and use it for gitcommands instead of
password. The steps to create your personal access token are mentioned
in this link: Creating a Personal Access Token
Submit Feedback
Cheat Sheet
Creating a Commit
Working Directory
alice.txt
and
bob.txt
touch alice.txt
touch bob.txt
Git’s View of Repository
Inspecting a Repository
git status
Making Changes
alice.txt
and
bob.txt
files.
alice.txt:
Hi
Is it going good?
bob.txt:
Hi
git add
Syntax :
Example :
commit
Syntax :
Here
message
Example :
git log
git log
Sample output :
Commit Id
Commit IDs are unique strings(hashes) that are created whenever a new
commit is recorded.
8f00aaa0248bcdc38a8d8ba6267167a0478f5a63
Unstaged Changes
git diff
git diff
Sample output :
--- a/alice.txt
+++ b/alice.txt
@@ -0,0 +1,2 @@
+Hi
Uncommitted Changes
git diff --staged
Pushing Commits
git push
Syntax :
Warning
git clone, git push and git pullcommands with password. You need to
create a personal access token and use it for gitcommands instead of
password. The steps to create your personal access token are mentioned
in this link: Creating a Personal Access Token
You can also edit file and commit changes through github Website
Editing File
Pull Commits
git pull
is used to pull latest commits from a remote repository to your local
repository.
Warning
git clone
git push
and
git pull
git
Submit Feedback
Marked as complete
Notes
In this Git interview questions article, we will provide Top GitHub interview
questions and answers designed for both freshers and experienced
developers. To help you get ready for interviews, we have gathered
the top 50 Git interview questions for freshers, intermediate, and
experienced candidates. Going through these questions will boost your
chances of getting a job at top MNCs.
Table of Content
1. What is Git?
A Git repository (or repo) is like a file structure that stores all the files for a
project. It continues track changes made to these files over time, helping
teams work together evenly. Git can control both local repositories (on
your own machine) and remote repositories (usually hosted on platforms
like GitHub, GitLab, or Bitbucket), allowing teamwork and backup.
Git GitHub
In Git, "origin" states to the default name offered to the remote repository
from which local repository was cloned. It is used as a reference to control
fetches, pulls, and pushes.
The '.gitignore' file tells Git which files and folders to ignore when tracking
changes. It is used to avoid attaching unneeded files (like logs, temporary
files, or compiled code) to your repository. This saves repository clean and
targeted on important files only.
The 'git pull' command updates the current local branch with changes
from a remote repository and combining it with a local repository.
The 'git clone' forms a copy of a remote repository upon your local
machine. It downloads all files, branches, and history, enabling you to
start working on the project or contribute to it. With git clone -b , you can
download and work on an individual branch of a repository.
11. What is the difference between git init and git clone?
'git init' develops a new, empty Git repository in the present directory,
while 'git clone' copies an existing remote repository, containing all files
and history, to a local directory.
The 'git add' command marks changes in your project for the next
commit. It tells Git which files to involve in the later update, making them
ready to be saved in the repository. This is the early step in recording
changes in the Git repository.
The 'git status' command shows the recent status of your Git repository. It
tells you which files have changed, which ones are ready to be committed,
and which ones are new and unobserved. This benefits you monitor your
work's growth and see what changes want to be set up or committed.
The 'git clean' command is used to erase ignored files from the working
directory of Git repository. Its motive is to clean up the workspace by
deleting files that are not being saved by Git, checking a clean state with
only observed files present.
In Git, the "Index" (also called as the "Staging Area") is a place where
alterations are temporarily store before committing them to the repository.
It permits you to select and prepare specific alterations from your working
directory before properly saving them as part of the project's history.
To change the preceding commit in Git, use 'git commit --amend' after
making changes, stage them with 'git add' , and save with the editor.
To switch branches in Git, use 'git checkout ' to move to a present branch.
On the other hand, use git switch in newer Git versions for the same
objective. This permits you to work on different versions or features of
your project stored in separate branches.
GitHub
GitLab
SourceForge.net
Bitbucket
22. What is the difference between git fetch and git pull?
'git fetch' fetches updates from a remote repository but does not combine
them into your local repository. It fetches all the new data from the remote
repository that you don’t have yet, but it stores it in a separate area,
permitting you to review the changes before merging them into your
working directory.
'git pull' fetches the updates from the remote repository and instantly
strives to merge them into your current branch. It is basically a union of
'git fetch' followed by 'git merge' .
Run 'git init' in the project folder. This will create a '.git' folder,
showing your repository is set.
25. What differentiates between the commands git remote and git
clone?
Teams can together work on distinct parts of the system and later
combine their changes using pull requests. This way boosts team
capability.
A Git bundle is a collective file that wraps all data from Git repository,
such as commits, branches, and tags. It acts as a handy approach for
relocating a repository offline or sharing upgrades when network
connection is not available. To form a git bundle, perform the following
command:
30. How do you revert a commit that has already been pushed
and made public?
To revert a commit that has been pushed and made public, follow these
steps:
Checkout the Branch: Switch to the branch where you want to revert
the commit.
Find the Commit to Revert: Use 'git log' to find the commit hash of
the commit you want to revert.
git log
Revert the Commit: Use 'git revert' followed by the commit hash of
the commit you want to revert.
Review Changes: Git will open your default text editor to confirm the
revert message. Save and close the editor to proceed.
Push the Revert: Finally, push the reverted commit to the remote
repository.
Git Reflog: It stands for "reference log". It records changes to the HEAD
(current branch pointer) and permits you to see a timeline of recent
movements within the repository, like commits, checkouts, merges, etc. It
is effective for recovering lost commits or branches that are no longer
visible in the regular log.
In Git, HEAD is a source to the current branch or commit you are working
on. HEAD normally shows the recent commit of the current branch and
moves when you switch branches or check out exact commits.
The intent of 'git tag -a' is to form an annotated tag in Git. Annotated tags
are tags that contain additional metadata such as the tagger's name,
email, date, and a message. They are valuable for labeling important
points in history, like releases, and give another context compared to
lightweight tags made with 'git tag' .
Working Tree: Includes the actual files you are currently working on or
modifying.
Discover Conflicting Files: Find the files where conflicts have take place
because of changes from unique branches.
Correct Files to Repair Conflicts: Alter the files to sync conflicting
changes and block future conflicts.
Stage solved Files: Use 'git add' to add the resolved files to the staging
area. Commit the Changes: Complete the process by committing the
changed files applying 'git commit' .
37. Explain the difference between `git merge` and `git rebase`
and when you would use each?
'git merge' merges changes from one branch into another, keeping
distinguish branch histories. It forms merge commits that directly show
where branches came together. 'git rebase' reforms history by using
commits from one branch onto another, causing in a linear sequence of
commits. Prefer 'merge' to keep distinct branch timelines and use 'rebase'
for enhancing commit history before merging branches.
Use `git add <file_name>` to add a file to the staging area, forming it
ready for a commit.
'git diff' is a command in Git that presents the differences between varied
states of files in a repository. It equates changes between the working
directory, the staging area (index), and the last commit. It assists track
changes, additions, and deletions before committing changes to the
repository.
The Git object model comprises four major types: blobs (which store file
data), trees (which store directory structures), commits (which store
repository snapshots), and tags (which store references to commits).
These objects are the pillar of Git's version control system, permitting for
capable tracking and management of changes.
42. Explain `git rebase` and when you would use each?
'git rebase' moves commits from one branch to another, making a
straight, linear history. Use rebase to enhance and clean up the commit
history before merging.
43. What is a git hook and how might you use it?
A Git hook is a pattern script that instantly runs at exact points in the Git
workflow, such as before or after commits, merges, or pushes. You can use
hooks to apply coding rules, run tests, check for security faults, or simplify
tasks. For an example, a pre-commit hook can run and tests to verify code
quality before changes are committed.
46. Explain the difference between git reset, git revert, and git
checkout?
To handle large files in Git, use Git LFS (Large File Storage). It tracks large
files severally from your repository, storing them on a remote server. This
prevents bloating your repository size and secures improved performance
while operations like cloning and fetching.
The git cherry-pick command uses exact commits from one branch to
another, allowing selective merging of changes without merging entire
branches.
49. What is ‘bare repository’ in Git?