Get success in GIT interview!
What you will learn
Understand GIT concepts
Learn GIT Interview Questions
Best practices of GIT
Tricky questions on GIT
Gain deep GIT knowledge
Demand higher salary or promotion based on the knowledge gained!
Why take this course?
Noteβ Make sure your ππππ¦π² cart has only this course you're going to enroll it now, Remove all other courses from the ππππ¦π² cart before Enrolling!
- What is GIT?
GIT is a distributed version control system created by Linus Torvalds, originally designed to handle kernel development for the Linux operating system and other software projects with an emphasis on speed, data integrity, and support for distributed and non-linear workflows. - What is a repository in GIT?
A repository (often shortened to “repo”) is a storage location under version control that holds an entire directory tree along with its complete history. In Git, every repository can function as both a server and a client, making it a distributed system. - What are the main benefits of GIT?
- Distributed nature: Each developer has their own copy of the codebase, leading to robustness and reliability because the history of changes is local (and thus backed up) in each repository.
- Data integrity: Git uses a data model that ensures the cryptographic integrity of historical data. It’s impossible to change past commits without breaking the repository.
- Branching and merging are very simple in Git, so it’s easier to adopt and explore different approaches or features simultaneously.
- Speed: Git is designed from the ground up to be fast. The performance of a slow disk is often the limiting factor rather than CPU power.
- Offline usage: You can work on your code locally and synchronize with a server when you’re ready.
- What are the disadvantages of GIT?
- Learning curve: New users might find Git’s terminology and branching model more complex than other version control systems like Subversion (SVN).
- Repository size: Because each commit is stored as a complete snapshot, repositories can become quite large over time.
- Lack of coarse-grained access control mechanisms compared to SVN or centralized systems.
- Merge conflicts can be challenging to resolve for those new to Git.
- What are the main differences between GIT and SVN?
- Git is distributed, while SVN is centralized.
- Git allows you to work offline, whereas SVN traditionally does not.
- Git’s branching and merging are more flexible and powerful, allowing for complex workflows and parallel development without the need for “locking” files.
- Git has a shallower learning curve once understood, as it is based on content addressing and allows you to see the history of changes in a more straightforward manner.
- SVN typically uses a single central repository, whereas each clone in Git is a full-fledged repository with a complete copy of the project’s history.
- How do you initialize a new GIT repository?
To create a new Git repository, navigate to your desired directory and rungit init
. This command initializes a new Git repository in the current working directory. - How to track files with Git?
To start tracking a file with Git, add it to the staging area usinggit add filename
orgit add .
to add all new and changed files. After adding, commit the changes usinggit commit -m "commit message"
. - How to clone an existing GIT repository?
Use thegit clone
command followed by the URL of the remote repository, e.g.,git clone https://github.com/username/repository.git
. - How to push changes to a remote repository in GIT?
After committing local changes withgit commit
, you can usegit push origin main
(assuming the branch is named ‘main’) to send your commits to the remote repository named ‘origin’. - How to resolve merge conflicts in GIT?
Resolving merge conflicts involves manually editing the files where conflicts occurred, choosing which changes to keep, and then staging and committing those resolutions usinggit add
andgit commit
. - How do you revert to a previous commit in GIT?
Usegit checkout <commit-sha>
or reset your branch to the previous state withgit reset --soft <commit-sha>
. - How to view the status of changes in GIT?
The commandgit status
will show you which files are modified, staged, or untracked. - How to create a new branch in GIT and switch to it?
Create a new branch withgit branch <branchname>
and then switch to that branch withgit checkout <branchname>
orgit switch <branchname>
. The latter command is an alias introduced in Git version 2.23. - How to delete a branch in GIT after merging it into the main branch?
After ensuring all changes have been merged into your main branch, you can delete a branch withgit branch -d <branchname>
or usegit delete
for remote branches. - What are hooks in GIT and how do you run a script on commit?
Git hooks are scripts that run before or after events such as commit (pre-commit
,post-commit
) and push (pre-push
,post-push
). To run a script on commit, place it in thehooks
directory of your repository. - How to determine if changes are staged, committed, or neither in GIT?
Usegit diff
to compare staged changes with the last commit, and usegit log --follow <file>
to trace the history of a file including renames. - What are some common Git commands and their uses?
git clone
: Creates a copy of a repository.git add
: Stages changes for the next commit.git commit
: Records changes to the repository.git push
: Updates remote repository with new commits.git pull
: Fetches and merges changes from a remote repository.git fetch
: Downloads all new commits from the remote repository but doesn’t merge them into your current branch.git branch
: Lists all local branches in the currently active repository.git checkout -b
: Creates a new branch and switches to it.git merge
: Joins two lines of development by recording changes from one branch into another.git rebase
: Reapply commits on top of another base tip. Useful for cleaning up history.
- How do you handle large files in GIT?
Large files can be handled by either excluding them from version control using.gitignore
, or by filtering them out withgit attribute
and rewriting the history withgit filter-branch
. - What is the difference between
git merge
andgit rebase
?
git merge
combines a series of commits from one branch into another, creating a new commit representing their difference.git rebase
takes existing commits and applies them on top of another base commit, resulting in a cleaner project history without merge commits. - How do you handle binary files or large blobs in GIT efficiently?
You can use Git Large File Storage (LFS) to handle binary files that are too big to store in the repository proper. This is done usinggit lfs
commands. - What is a detached HEAD state in GIT and how do you avoid it?
A detached HEAD occurs when you checkout an commit that’s not the beginning of a branch. To avoid this, either create or switch to an existing branch before checking out. You can return to a branch withgit checkout <branch-name>
. - How do you handle conflicts during a git pull or rebase in GIT?
During a rebase (git rebase
) or pulling from a remote (git pull
), if conflicts occur, Git will pause and allow you to resolve the conflicts manually by editing the conflicting files, then staging the resolved changes withgit add
, and finally continuing the rebase (or merging in the case of a pull) withgit rebase --continue
orgit merge --continue
. - What is the difference between
--track
and--no-track
options in git clone?
The--track
option creates remote-tracking references corresponding to each local branch, while--no-track
disables this behavior, making the cloned branches non-fast-forward only if they have different tips. - How do you view the commit history in GIT?
Usegit log
to see the commit history, andgit log --oneline
for a concise list of commits. Use--graph
to include a diffusion graph (dot-graphviz format) before the commit lists and--decorate
or--oneline
to show branch and tag information. - How to check if your local repository is up-to-date with the remote repository in GIT?
Usegit fetch --dash
(orgit status -uall
) to see all branches that have changes pushed to the remote repository but not yet pulled into your local repository. - How to create a new commit on top of the latest commit in a branch in GIT?
After staging your changes withgit add
, create a new commit withgit commit --amend
if you want to modify the last commit, or usegit commit
followed bygit rebase -i HEAD~n
wheren
is the number of commits you want to interactively rebase. - How to cherry-pick a commit from one branch and apply it to another in GIT?
Usegit cherry-pick <commit-sha>
after navigating to the target branch where you want to apply the commit. - What is a Git rebase vs. Bash diff approach for cleaning up history?
Rebasing rewrites the project history by moving commits to a new base commit. A Bash diff approach involves manually editing the patch files produced bygit diff
and then resolving them withgit apply
. Rebasing is generally more convenient for most use cases. - How do you manage different development environments (e.g., develop, release, hotfix) in GIT?
Use branching strategies like Gitflow or feature branch workflows to manage different development environments. Each type of work has its own branch prefix and lifecycle defined within the strategy. - How do you undo a commit in GIT?
You can usegit reset
to undo commits:git reset HEAD~n
(wheren
is the number of commits to undo) will unstage those commits, andgit reset --soft HEAD~n
will uncommit them without altering the staged changes. A--hard
option will both uncommit and unstage the changes, reverting your files as well. - How do you resolve a merge conflict that occurred during a git pull or rebase in GIT?
After pulling or rebasing, if conflicts occur, manually edit the conflicting files to resolve the issues. Then stage the resolved changes withgit add
, and continue the rebase (or finalize the pull) using the appropriate command (git rebase --continue
orgit merge --continue
). - What is a Git stash and how do you use it?
A stash is a way to save all modifications on your current branch and check them out in a detached HEAD state. You can stash your changes withgit stash
, apply them back withgit stash pop
, and reapply them if you’ve since made additional changes withgit stash apply
. - How to work on a feature branch and then merge it into the main branch in GIT?
After completing work on your feature branch, push your changes to the remote repository, and then merge your branch into the main branch usinggit checkout main
,git pull
, thengit merge feature-branch
(assuming “feature-branch” is the name of your feature branch). - How do you rename a branch in GIT?
You can’t directly rename a remote branch, but you can create and push a new branch with the desired name and then delete the old branch. Usegit branch -m old-name new-name
for a local branch andgit push origin -u new-name
to set the upstream for the newly named branch, then push your changes and delete the old branch. - What are Git hooks and how do you use them?
Git hooks are scripts that run before or after events, such as commit (pre-commit
andpost-commit
) or push (pre-push
andpost-push
), in a Git repository. They are used to enforce certain conditions before an operation occurs or to perform additional tasks afterward. - How do you handle submodules in GIT?
Submodules allow you to include other Git repositories within your project and track changes in those submodules. Initialize a submodule withgit submodule add [url] path
, update a submodule withgit submodule update --init [url] path
, and commit the submodule reference with agit commit
(the submodule’s state will be captured by its HEAD commit). - How to handle large binaries or files in GIT effectively?
Use Git LFS (Large File Storage) to handle binary files that are too big to store in the repository proper. Configure LFS withgit lfs init
and track files withgit lfs track "path/to/file"
orgit lfs track "glob pattern"
, then commit the file as a placeholder commit. Push your changes, and LFS will handle the actual file data on Git Hub or other Git hosting services. - What is the difference between
git diff
andgit log -p
?
git diff
shows the differences between two sets of files or commits in the working directory, staging area (index), or commit history, whilegit log -p
shows an incremental diff between each committed version of a file. - How do you view the differences between branches in GIT?
Usegit diff <branch1> <branch2>
to view the differences between two branches in your local repository. To compare with a remote branch, usegit diff origin/<branch2> <branch1>
. - How do you handle renaming files or directories in GIT?
Rename files or directories locally and commit the change as usual. If you want to rename a file remotely (e.g., after a Git LFS migration), you’ll need to add a renaming mapping in the.gitattributes
file, then usegit lfs migrate --renames
and commit the new mappings. - How to keep a GIT repository in sync with a remote repository?
Regularly pull changes from the remote repository (git pull origin main
) and push your local commits (git push origin main
). Ensure that you handle merges or rebases as necessary to integrate changes smoothly. - What are some best practices for maintaining a GIT repository?
Keep your repository clean by regularly pulling from and pushing to the remote repository, resolving conflicts promptly, using meaningful commit messages, keeping an organized branching strategy (e.g., Gitflow), enforcing code reviews before merging, and backing up your repository regularly. - How do you reset your local repository to match a remote branch?
You can reset your local branch to match the state of the remote branch by first fetching the latest changes (git fetch origin
), then resetting your branch (git reset --hard origin/<branch>
) or re-setting and merging changes (git reset --keep origin/<branch>
followed bygit merge origin/<branch>
). - What are Git reflogs and how do you use them?
Git maintains a log of every object it accesses, which can be used to recover lost commits or file a bug with a reproducible example. Usegit reflog
to list the reflogs, andgit reset --hard [reflog]
to restore a specific state from the reflog. - How do you handle a dead branch in GIT that has been integrated into another branch?
If a branch has been fully merged into another and is no longer needed, you can delete it usinggit push origin --delete <dead-branch>
. This will remove the branch from both your local repository and the remote repository. - How do you cherry-pick a commit from one branch to another in GIT?
Cherry-pick a specific commit from one branch to another usinggit cherry-pick <commit>
. First, make sure you’re on the branch where you want to apply the commit, then run the command. - What is the difference between
git fetch
andgit pull
in GIT?
git fetch
retrieves new data from a remote repository but does not modify your current working branch.git pull
fetches data and immediately updates your current branch with the changes fetched. - How do you manage and release tags in GIT?
Create and manage tags locally usinggit tag [tag-name]
to create a new tag andgit push origin --tags
to push all tags to the remote repository. To release a tag, simply push it to the remote repository as described above. - What is the difference between shallow and deep clones in GIT?
A shallow clone contains only the recent history of a repository, which can be useful for large projects or when you only need recent commits. A deep clone has the full history of the repository, as it would have been if cloned directly from the remote source without any filtering. - How do you handle merge conflicts that occur during a pull request in GIT?
When a pull request (PR) is opened and a conflict arises upon trying to merge, address the conflicts on the PR page by editing the files, staging the resolved changes, and creating a commit to resolve the conflict. Once resolved, mark the PR as ready for review.
English
language