1. What is Git, and how does it differ from SVN or Mercurial?
Git is a distributed version control system that tracks changes in code and enables multiple developers to collaborate on projects. It differs from SVN and Mercurial in that Git is decentralized, meaning every user has a full copy of the repository and its history, while SVN and Mercurial are typically centralized systems, relying on a central repository for version control.
2. What is the difference between git pull
and git fetch
?
git fetch
: This command downloads changes from a remote repository, but does not apply them to your local working directory. It updates your local view of the remote repository but leaves your current working branch unchanged.git pull
: This command performs bothgit fetch
(downloads changes) and merges them into your current branch, effectively updating your local branch with remote changes.
3. How do you resolve a merge conflict in Git?
To resolve a merge conflict, Git marks the conflicting files. You need to open these files, look for the conflict markers (<<<<<<<, =======, >>>>>>>), and manually choose which changes to keep or combine them. After resolving the conflicts, stage the files and commit the changes to complete the merge.
4. What is Git branching, and why is it useful in a development workflow?
Git branching allows you to create isolated environments to work on different features, bug fixes, or experiments without affecting the main codebase. Branches make it easier to work on multiple things simultaneously, and they are essential for collaboration in a team environment.
5. Explain the difference between git merge
and git rebase
.
git merge
: Combines two branches, creating a merge commit that preserves the history of both branches.git rebase
: Moves or combines a sequence of commits from one branch onto another. It rewrites history, making the project history linear and cleaner.
6. How do you undo a commit in Git?
You can undo a commit using different commands depending on the situation:
- Use
git reset --soft HEAD~1
to undo the last commit while keeping the changes staged. - Use
git reset --hard HEAD~1
to undo the last commit and discard the changes completely. - If you want to preserve the changes but undo the commit, use
git reset --mixed HEAD~1
.
7. What is git stash
, and how do you use it?
git stash
is a command used to temporarily save changes that are not yet committed. This allows you to switch branches or work on something else without committing your current work. To use it, simply run git stash
to save the changes and git stash apply
to retrieve them later.
8. What is the difference between git clone
and git fork
?
git clone
: Creates a local copy of a remote repository.git fork
: Typically used on platforms like GitHub, it creates a personal copy of a repository under your own account, enabling you to make changes without affecting the original project.
9. How do you view the commit history in Git?
To view commit history, you can use git log
, which shows a list of all commits. The --oneline
option condenses the history into a single line per commit, and --graph
adds a visual representation of the commit tree. You can also search the commit messages using git log --grep="search_term"
.
10. What is a pull request (PR), and how is it used in collaboration?
A pull request is a request to merge changes from one branch into another, typically used when collaborating with others. It allows team members to review code, suggest changes, and discuss the modifications before merging them into the main codebase.
11. How would you resolve an issue where the git pull
command gives a “merge conflict” error?
When a merge conflict occurs after running git pull
, you need to open the conflicting files, examine the sections marked with conflict markers, and manually resolve the differences. After resolving the conflicts, stage the changes and commit the merge.
12. What is git cherry-pick
and when would you use it?
git cherry-pick
is used to apply a specific commit from one branch to another. This is useful if you want to apply a particular change without merging the entire branch or history.
13. How can you switch to a different branch in Git?
To switch to a different branch, use git checkout <branch_name>
. With newer versions of Git, you can also use git switch <branch_name>
, which is a simpler and more intuitive command.
14. What is the role of .gitignore
file in Git?
The .gitignore
file tells Git which files or directories should not be tracked. It is useful for excluding temporary or build files, IDE configurations, and other files that do not need to be committed to the repository.
15. How do you create and switch to a new branch in Git?
To create a new branch and switch to it in a single command, use git checkout -b <new-branch-name>
. This creates the new branch and automatically checks it out for use.
16. What is the purpose of git remote
and how do you use it?
git remote
is used to manage remote repositories. You can use it to add a remote repository (git remote add
), list all remotes (git remote -v
), or remove a remote (git remote remove <name>
).
17. How would you undo a git push
?
To undo a git push
, you can use git reset
to move the commit pointer and then force push to the remote repository with git push --force
. If you want to undo a commit but preserve history, use git revert
to create a new commit that undoes the changes.
18. How do you create a tag in Git, and what is it used for?
A tag in Git is used to mark a specific commit, often used for marking release points like v1.0
. Tags can be created using git tag <tag_name>
for a lightweight tag, or git tag -a <tag_name> -m "message"
for an annotated tag.
19. What is the difference between git reset
and git revert
?
git reset
: Resets the current branch to a previous commit and can optionally modify the index or working directory.git revert
: Creates a new commit that undoes the changes made by a previous commit. This does not alter the commit history and is used to safely undo changes in a public repository.
20. Explain the git log --oneline
command and its use.
The git log --oneline
command shows the commit history in a compact, single-line format. Each commit is displayed with its abbreviated commit hash and commit message, making it easy to see a summarized view of the history, especially for large projects with many commits.