Git Basics
1. What is Git, and why is it used?
Git is a distributed version control system used for tracking changes in source code. It allows multiple developers to collaborate efficiently.
2. What is the difference between Git and GitHub?
Git is a version control system, while GitHub is a cloud-based hosting service for Git repositories.
3. What are the basic Git commands?
- git init - Initializes a new repository
- git clone <repo> - Clones a repository
- git add <file> - Stages a file for commit
- git commit -m "message" - Commits staged changes
- git push - Pushes local commits to the remote repository
- git pull - Fetches and merges changes from a remote repository
4. What is the difference between git pull and git fetch?
git fetch retrieves changes from a remote repository but does not merge them, whereas git pull fetches and merges changes into the current branch.
5. What is a Git branch, and how do you create one?
A branch is a separate line of development. You can create a branch using git branch <branch-name> and switch to it using git checkout <branch-name>.
6. What is the difference between git merge and git rebase?
git merge combines two branches while preserving the history, whereas git rebase integrates changes by reapplying commits in a linear order.
7. How do you undo a commit?
- git reset --soft HEAD~1 - Moves HEAD back to the previous commit but keeps changes staged.
- git reset --hard HEAD~1 - Completely removes the last commit and changes.
8. What is .gitignore and its purpose?
.gitignore is a file that specifies which files and directories should be ignored by Git to prevent unnecessary tracking.
9. How do you delete a branch in Git?
- git branch -d <branch-name> deletes a local branch.
- git push origin --delete <branch-name> deletes a remote branch.
10. What is Git stash, and how do you use it?
git stash temporarily stores changes without committing them. Use git stash pop to reapply the changes.