Interviews Questions - (Git)
Git Fundamentals
What is Git?
- Git is a distributed version control system that tracks changes to computer files and coordinates work on those files among multiple people.
Why is Git popular?
- Speed: Git is incredibly fast for most operations.
- Data Integrity: Git uses the SHA-1 hash to ensure the integrity of your project.
- Support for Non-linear Workflows: Git is excellent at handling branching and merging.
- Large Projects: Git scales well to handle very large projects.
- Open Source: Git is free and open-source.
What are the basic Git commands?
git init: Initializes a new Git repository.git clone: Creates a local copy of a remote repository.git add: Stages changes to be committed.git commit: Creates a new commit with the staged changes.git push: Pushes commits to a remote repository.git pull: Fetches and merges changes from a remote repository.git status: Shows the working tree status.git log: Shows the commit history.git diff: Shows the differences between files.git branch: Creates, lists, or deletes branches.git checkout: Switches between branches.git merge: Merges changes from one branch into another.git rebase: Re-applies a series of commits on top of another base tip.
What is the difference between working directory, staging area, and Git repository?
- Working Directory: The files on your local machine that you're currently working on.
- Staging Area (Index): A temporary area where you stage changes before committing them.
- Git Repository: The .git folder that contains all the metadata and objects for your project.
What is a commit in Git?
- A commit is a snapshot of the changes made to the files in your project at a specific point in time.
What is a branch in Git?
- A branch is a separate line of development within a Git repository. It allows you to work on new features or experiment without affecting the main codebase.
What is the difference between Git and GitHub/GitLab/Bitbucket?
- Git is the version control system.
- GitHub, GitLab, and Bitbucket are platforms that provide hosting for Git repositories, along with additional features like issue tracking, code review, and collaboration tools.
Git Workflow
Explain the typical Git workflow.
- Create a branch: Create a new branch for your feature or bug fix.
- Make changes: Make changes to the files in your working directory.
- Stage changes: Add the changes you want to commit to the staging area.
- Commit changes: Create a commit with a descriptive message.
- Push changes: Push your commits to the remote repository.
- Create a pull request: Create a pull request to merge your changes into the main branch.
- Review and merge: Review the code and merge the pull request if it's approved.
What is a pull request?
- A pull request is a mechanism for proposing changes to a repository. It allows you to submit your changes for review before they are merged into the main branch.
What is a merge conflict?
- A merge conflict occurs when two or more people make changes to the same part of a file simultaneously. Git cannot automatically determine which changes to keep, so it requires manual resolution.
How do you resolve merge conflicts?
- Manually edit the conflicting files to resolve the conflicts.
- Use a merge tool to help resolve conflicts.
What is rebasing?
- Rebasing rewrites the commit history to make it appear as if the branch was created on top of the latest commit on the target branch.
When should you use rebasing and when should you use merging?
- Rebasing: Useful for cleaning up the commit history before pushing to a shared branch. However, it should be used with caution, especially on public branches, as it rewrites the history.
- Merging: The safer option for merging changes, as it preserves the original commit history.
Git Remote Repositories
What is a remote repository?
- A remote repository is a copy of your local repository stored on a server (e.g., GitHub, GitLab, Bitbucket).
How do you add a remote repository in Git?
- Use the
git remote add <name> <url>command.
- Use the
How do you fetch changes from a remote repository?
- Use the
git fetch <remote>command.
- Use the
How do you pull changes from a remote repository?
- Use the
git pull <remote> <branch>command.
- Use the
Git Advanced Concepts
What are Git hooks?
- Git hooks are scripts that run automatically at specific points during the Git workflow (e.g., before a commit, after a push).
What is Git submodules?
- Git submodules allow you to include another Git repository as a subdirectory within your project.
What is Git LFS (Large File Storage)?
- Git LFS is an extension to Git that allows you to store large files (like images, videos, and audio) outside of the main Git repository.
What is the difference between
git stashandgit reset?git stash: Temporarily stores uncommitted changes in a stack.git reset: Discards uncommitted changes or resets the current branch to a specific commit.
Git Best Practices
- Write frequent and meaningful commit messages.
- Keep your commits small and focused.
- Use branches for all new features and bug fixes.
- Review code carefully before merging.
- Use pull requests for all code changes.
- Resolve merge conflicts promptly.
- Keep your local branches up-to-date with the remote branches.
- Use a consistent branching strategy.
- Back up your repositories regularly.
Git Troubleshooting
How do you undo a commit?
- Use
git revertto create a new commit that undoes the changes introduced by the previous commit. - Use
git resetto move the current branch to a previous commit (but be careful, as this can discard uncommitted changes).
- Use
How do you recover from accidental deletions?
- If the deleted files are still in the Git repository, you can use
git checkoutto restore them. - If the deleted files are not in the Git repository, you may be able to recover them from backups.
- If the deleted files are still in the Git repository, you can use
How do you find the SHA-1 hash of a commit?
- Use the
git logcommand to view the commit history, which includes the SHA-1 hash for each commit.
- Use the
Git and GitHub
How do you create a new repository on GitHub?
- Go to the GitHub website and click on the "New repository" button.
How do you connect your local repository to a remote repository on GitHub?
- Use the
git remote add origin <remote_url>command.
- Use the
How do you create a pull request on GitHub?
- Go to the GitHub repository, navigate to the "Pull requests" tab, and click on the "New pull request" button.
How do you review and merge pull requests on GitHub?
- Leave comments on the pull request to provide feedback.
- Once the code is reviewed and approved, merge the pull request.
Git and Collaboration
How do you collaborate with others on a Git project?
- Use branches to work on separate features or bug fixes.
- Create pull requests to submit your changes for review.
- Use code review tools to provide and receive feedback.
- Communicate effectively with your team members.
What are some best practices for collaborating with others on Git?
- Keep your branches up-to-date with the main branch.
- Resolve merge conflicts promptly.
- Communicate effectively with your team members.
- Use a consistent branching strategy.
Git and CI/CD
What is CI/CD?
- CI/CD stands for Continuous Integration and Continuous Delivery/Deployment.
- It's a set of practices that enable developers to frequently and reliably deliver code changes.
Git Fundamentals
What is Git?
- Git is a distributed version control system that tracks changes to computer files and coordinates work among multiple people.
Why is Git popular?
- Speed: Git is incredibly fast for most operations.
- Data Integrity: Git uses SHA-1 hashing to ensure data integrity.
- Support for Non-linear Workflows: Git excels at branching and merging, making it easy to experiment and collaborate.
- Large Community and Support: A vast community and extensive documentation are available.
What is the difference between Git and GitHub/GitLab/Bitbucket?
- Git: The underlying version control system.
- GitHub/GitLab/Bitbucket: Platforms that provide hosting for Git repositories, along with features like issue tracking, code review, and collaboration tools.
What is the basic workflow of using Git?
- Create: Create a new repository.
- Clone: Clone an existing repository to your local machine.
- Stage: Add changes to the staging area (also known as "indexing").
- Commit: Commit the staged changes to your local repository.
- Push: Push your local commits to a remote repository.
- Pull: Fetch and merge changes from a remote repository into your local branch.
What is the difference between working directory, staging area, and Git repository?
- Working Directory: The files on your local machine that you are currently working on.
- Staging Area: A temporary area where you stage changes before committing them to the Git repository.
- Git Repository: The central location where all the versions of your project are stored.
Git Commands
What is the command to create a new Git repository?
git init
What is the command to clone an existing Git repository?
git clone <repository_url>
What is the command to stage changes for a commit?
git add <file_name>orgit add .(to stage all changes)
What is the command to commit changes to the local repository?
git commit -m "<commit_message>"
What is the command to push changes to a remote repository?
git push <remote_name> <branch_name>(e.g.,git push origin main)
What is the command to fetch and merge changes from a remote repository?
git pull <remote_name> <branch_name>(e.g.,git pull origin main)
What is the command to view the history of commits?
git log
What is the command to create a new branch?
git branch <branch_name>
What is the command to switch to a different branch?
git checkout <branch_name>
What is the command to create a new branch and switch to it?
git checkout -b <branch_name>
What is the command to merge changes from one branch to another?
git merge <branch_name>
What is the command to delete a branch?
git branch -d <branch_name>
What is the command to undo the last commit?
git revert <commit_hash>
What is the command to discard unstaged changes?
git checkout -- <file_name>
What is the command to discard all unstaged and staged changes?
git reset --hard(use with caution as this will discard all uncommitted changes)
Git Concepts
What is a Git repository?
- The central location where all the versions of your project are stored.
What is a Git branch?
- A separate line of development within a Git repository.
What is a Git commit?
- A snapshot of your project at a particular point in time.
What is a Git tag?
- A lightweight pointer to a specific commit, often used to mark releases.
What is a Git remote?
- A connection to a remote repository (e.g., on GitHub, GitLab).
What is a Git fork?
- A copy of a repository on your own account.
What is a Git merge conflict?
- Occurs when Git cannot automatically merge changes from two different branches due to conflicting modifications.
How do you resolve a merge conflict?
- Manually edit the conflicting files to resolve the conflicts, then stage and commit the changes.
Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.