Git Interview Questions and Answers for 2 years experience
-
What is Git?
- Answer: Git is a distributed version control system (DVCS) used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project, managing different versions and merging their work efficiently.
-
Explain the difference between Git and GitHub.
- Answer: Git is a version control system; GitHub is a web-based hosting service that uses Git for version control. Git is the underlying technology, while GitHub provides a platform for collaboration, code review, and project management features.
-
What are the three main states of a file in Git?
- Answer: Modified, Staged, and Committed. Modified means the file has been changed but not yet saved to the repository. Staged means the changes are ready to be committed. Committed means the changes are permanently stored in the repository's history.
-
What is the Git repository?
- Answer: A Git repository (or repo) is a directory containing all the project files, along with a hidden `.git` directory that stores the entire version history.
-
Explain the command `git init`.
- Answer: `git init` initializes a new Git repository in the current directory. It creates the hidden `.git` directory and sets up the necessary files for version control.
-
What does `git clone` do?
- Answer: `git clone
` creates a local copy of a remote Git repository. This downloads the entire repository's history and files to your machine.
- Answer: `git clone
-
How do you add changes to the staging area?
- Answer: Use the command `git add
` to add individual files or `git add .` to add all modified files in the current directory and its subdirectories to the staging area.
- Answer: Use the command `git add
-
What is the purpose of `git commit`?
- Answer: `git commit -m "Your commit message"` saves the staged changes to the local repository. The commit message provides a description of the changes made.
-
Explain `git status`.
- Answer: `git status` shows the current state of your working directory and staging area. It indicates which files have been modified, staged, or are untracked.
-
How do you view the commit history?
- Answer: `git log` displays the commit history. Options like `--oneline` or `--graph` can provide a more concise or visual representation.
-
What is a branch in Git?
- Answer: A branch is an independent line of development. It allows developers to work on new features or bug fixes without affecting the main codebase. This promotes parallel development.
-
How do you create a new branch?
- Answer: `git branch
` creates a new branch. To switch to the new branch, use `git checkout `.
- Answer: `git branch
-
How do you merge a branch into another branch?
- Answer: Checkout the target branch (`git checkout
`), then merge the source branch (`git merge `). Resolve any merge conflicts that may arise.
- Answer: Checkout the target branch (`git checkout
-
What is a merge conflict?
- Answer: A merge conflict occurs when two branches have made changes to the same lines of the same file. Git cannot automatically determine which changes to keep, requiring manual resolution.
-
How do you resolve a merge conflict?
- Answer: Manually edit the conflicted files, indicating which changes to keep. Then stage the resolved files (`git add
`) and commit the merge (`git commit`).
- Answer: Manually edit the conflicted files, indicating which changes to keep. Then stage the resolved files (`git add
-
What is `git rebase`?
- Answer: `git rebase` rewrites the commit history by moving a branch's commits onto another branch. This creates a cleaner, linear history but should be used cautiously, especially on shared branches.
-
What is the difference between `git merge` and `git rebase`?
- Answer: `git merge` preserves the branch history, creating a merge commit. `git rebase` rewrites the history, creating a linear history but potentially altering shared history, which can cause issues in collaborative environments.
-
What is a remote repository?
- Answer: A remote repository is a version of your project hosted on a server, such as GitHub, GitLab, or Bitbucket. It allows for collaboration and backups.
-
How do you add a remote repository?
- Answer: `git remote add
` adds a remote repository with a given name and URL.
- Answer: `git remote add
-
How do you fetch changes from a remote repository?
- Answer: `git fetch
` downloads changes from the remote repository without merging them into your local branches.
- Answer: `git fetch
-
How do you pull changes from a remote repository?
- Answer: `git pull
` fetches changes from the remote repository and merges them into your current local branch.
- Answer: `git pull
-
How do you push changes to a remote repository?
- Answer: `git push
` uploads your local commits to the remote repository.
- Answer: `git push
-
What is a tag in Git?
- Answer: A tag is a pointer to a specific commit, usually used to mark important points in the project's history, such as releases (e.g., v1.0, v2.0).
-
How do you create a tag?
- Answer: `git tag
` creates an annotated tag. `git tag -a -m "Your tag message"` creates an annotated tag with a message.
- Answer: `git tag
-
How do you delete a local branch?
- Answer: `git branch -d
` deletes a local branch. Use `-D` to force delete a branch that hasn't been merged.
- Answer: `git branch -d
-
How do you delete a remote branch?
- Answer: `git push origin --delete
` deletes a remote branch. Or, `git push origin : `.
- Answer: `git push origin --delete
-
What is `git stash`?
- Answer: `git stash` temporarily saves your uncommitted changes, allowing you to switch branches or clean up your working directory without losing your work. You can later apply the stashed changes using `git stash pop`.
-
Explain `git cherry-pick`.
- Answer: `git cherry-pick
` applies a specific commit from one branch to another. This is useful for applying a single fix or feature from a different branch without merging the entire branch.
- Answer: `git cherry-pick
-
What is `.gitignore`?
- Answer: `.gitignore` is a file that specifies patterns of files or directories that should be ignored by Git. This prevents accidental tracking of temporary files, build artifacts, or sensitive data.
-
How do you undo your last commit?
- Answer: `git reset --soft HEAD~1` moves the HEAD pointer back one commit, keeping the changes in the staging area. `git reset --hard HEAD~1` discards the changes completely. Use caution with `--hard`.
-
How do you revert a commit?
- Answer: `git revert
` creates a new commit that undoes the changes introduced by a specific commit. This is safer than `git reset --hard` as it preserves the original commit history.
- Answer: `git revert
-
What is Git Hooks?
- Answer: Git hooks are scripts that run automatically before or after Git events, such as commit, push, or receive. They can be used for various purposes, including enforcing coding standards, running tests, or automating deployments.
-
What is a Gitflow workflow?
- Answer: Gitflow is a branching model that uses separate branches for developing features, releasing software, and managing hotfixes. It provides a structured approach to managing releases and maintaining a stable main branch.
-
What is GitHub Actions?
- Answer: GitHub Actions is a CI/CD platform that allows you to automate workflows directly within your GitHub repository. You can automate tasks such as building, testing, and deploying your code.
-
Explain the concept of a HEAD pointer in Git.
- Answer: The HEAD pointer refers to the currently checked-out branch or commit. It indicates which version of the code you are currently working on.
-
What are detached HEAD states?
- Answer: A detached HEAD state occurs when your HEAD pointer is pointing to a specific commit, rather than a branch. This can happen when checking out a tag or a specific commit hash. You should create a new branch to avoid losing your work.
-
What is a shallow clone?
- Answer: A shallow clone downloads only a portion of the repository's history, typically the most recent commits. This is useful for large repositories where you only need the recent changes, saving time and disk space.
-
How to find a specific commit in git?
- Answer: Use `git log --grep="
"` to search for commits containing a specific keyword. You can also search by author, date, or commit message using various `git log` options.
- Answer: Use `git log --grep="
-
How to revert changes in a file?
- Answer: `git checkout --
` discards changes in a file and reverts it to the last committed version.
- Answer: `git checkout --
-
What is Git LFS (Large File Storage)?
- Answer: Git LFS is an extension to Git that allows you to manage large files more efficiently. Instead of storing large files directly in the repository, it stores pointers and manages the files externally, keeping the repository smaller and faster.
-
How to create a patch file in Git?
- Answer: `git diff > patch_file.patch` creates a patch file containing the differences between your current working tree and the last commit. This patch can be applied to another repository.
-
How to apply a patch file in Git?
- Answer: `git apply patch_file.patch` applies a patch file to your working tree. Use `git am` for applying patches created with `format-patch` which is more robust for collaborative work.
-
What is the difference between `git fetch` and `git pull`?
- Answer: `git fetch` downloads the changes from a remote repository but doesn't merge them. `git pull` is equivalent to `git fetch` followed by `git merge`.
-
What is the command to view the differences between two commits?
- Answer: `git diff
` shows the differences between two commits. You can use commit hashes or branch names.
- Answer: `git diff
-
Explain the concept of a working directory, staging area, and repository in Git.
- Answer: The working directory is where you make changes to your files. The staging area is where you prepare changes to be committed. The repository is where the complete version history is stored.
-
How to check out a specific commit?
- Answer: `git checkout
` checks out a specific commit. This puts you in a detached HEAD state. Remember to create a branch if you want to make changes to this commit.
- Answer: `git checkout
-
How to find the author of a specific commit?
- Answer: Use `git log -p --author=
` or inspect the output of `git show ` to find the author of a commit.
- Answer: Use `git log -p --author=
-
What is the significance of the `.git` directory?
- Answer: The `.git` directory is a hidden directory that contains all the internal data of the Git repository. It should not be modified manually.
-
How to ignore specific files or directories when committing?
- Answer: Add the patterns of the files or directories you want to ignore in the `.gitignore` file. Git will then ignore those files when you commit.
-
How do you rename a branch in Git?
- Answer: You cannot directly rename a branch. You must create a new branch with the desired name and then delete the old branch.
-
How to undo a `git add` command?
- Answer: `git reset HEAD
` removes a file from the staging area without removing the changes from the working directory.
- Answer: `git reset HEAD
-
How to see the difference between the working directory and the last commit?
- Answer: `git diff` shows the difference between your current working directory and the last commit.
-
How to move a file in Git?
- Answer: `git mv
` moves and renames a file. This updates Git's tracking of the file.
- Answer: `git mv
-
What is `git reflog`?
- Answer: `git reflog` shows a log of all changes to the HEAD pointer, even those not reflected in the commit history. This is useful for recovering lost commits or branches.
-
How to create a new repository on GitHub?
- Answer: Create a new repository through the GitHub website interface, providing a repository name, description, and optionally, choosing to initialize the repository with a README file.
-
How to fork a repository on GitHub?
- Answer: Click the "Fork" button on the GitHub repository page. This creates a copy of the repository in your own GitHub account.
-
How to create a pull request on GitHub?
- Answer: After forking and making changes in your fork, click "New pull request" on GitHub to propose your changes to the original repository.
-
What is a Git submodule?
- Answer: A Git submodule allows you to include another Git repository as a part of your project. This keeps the submodule's history separate but allows you to include its code in your project.
Thank you for reading our blog post on 'Git Interview Questions and Answers for 2 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!