Git Made Easy: 100 Questions with Solutions
Discover 100 Fundamental Git Questions and Their Solutions
1. What is Git?
➡️ Git is a distributed version control system that tracks changes in files and allows multiple developers to collaborate on a project.
2. How do you initialize a Git repository?
git init
➡️ Initializes a new Git repository.
3. How do you clone a remote Git repository?
git clone <repository_url>
➡️ Clones a remote repository to your local machine.
4. How do you check the current status of your repository?
git status
➡️ Displays the current state of your working directory and staging area.
5. How do you add changes to the staging area?
git add <file_name>
git add .
➡️ Adds changes to the staging area for the next commit.
6. How do you commit changes to Git?
git commit -m "Your commit message"
➡️ Commits changes in the staging area with a descriptive message.
7. How do you view the commit history?
git log
➡️ Shows the history of commits in the current branch.
8. How do you push commits to a remote repository?
git push <remote> <branch>
➡️ Pushes commits from the local repository to the remote repository.
9. How do you pull changes from a remote repository?
git pull <remote> <branch>
➡️ Fetches and integrates changes from the remote repository.
10. How do you create a new branch?
git branch <branch_name>
➡️ Creates a new branch.
11. How do you switch to a different branch?
git checkout <branch_name>
➡️ Switches to an existing branch.
12. How do you merge two branches?
git checkout <target_branch>
git merge <source_branch>
➡️ Merges changes from the source branch into the target branch.
13. How do you create and switch to a new branch at the same time?
git checkout -b <new_branch>
➡️ Creates and switches to a new branch.
14. What is a fast-forward merge?
➡️ A fast-forward merge happens when there are no divergent commits between two branches, allowing Git to move the pointer forward without creating a merge commit.
15. How do you delete a branch locally?
git branch -d <branch_name>
➡️ Deletes a branch locally.
16. How do you delete a branch remotely?
git push <remote> --delete <branch_name>
➡️ Deletes a branch from the remote repository.
17. How do you stash changes in Git?
git stash
➡️ Temporarily saves changes that are not ready to be committed.
18. How do you apply stashed changes?
git stash apply
➡️ Applies the latest stashed changes.
19. How do you list all stashes?
git stash list
➡️ Lists all stashed changes.
20. How do you delete a specific stash?
git stash drop <stash@{n}>
➡️ Deletes a specific stash.
21. How do you rebase a branch onto another branch?
git checkout <branch_name>
git rebase <target_branch>
➡️ Reapplies commits on top of another base commit.
22. What is a Git tag?
➡️ A Git tag is a reference to a specific commit, typically used to mark release points.
23. How do you create a tag in Git?
git tag <tag_name>
➡️ Creates a tag for the current commit.
24. How do you push a tag to a remote repository?
git push <remote> <tag_name>
➡️ Pushes a tag to the remote repository.
25. How do you list all tags in a Git repository?
git tag
➡️ Lists all tags in the current repository.
26. How do you check out a specific tag?
git checkout <tag_name>
➡️ Checks out a specific tag.
27. How do you create an annotated tag?
git tag -a <tag_name> -m "Tag message"
➡️ Creates an annotated tag with a message.
28. How do you undo the last commit but keep the changes?
git reset --soft HEAD~1
➡️ Reverts the last commit but keeps the changes in the staging area.
29. How do you revert a commit?
git revert <commit_hash>
➡️ Reverts a specific commit by creating a new commit with the inverse changes.
30. What is the difference between git reset
and git revert
?
➡️ git reset
undoes changes by moving the branch pointer backward, whereas git revert
creates a new commit that undoes a previous commit.
31. How do you show the changes made in a specific commit?
git show <commit_hash>
➡️ Shows the changes introduced by a specific commit.
32. How do you discard changes in a file?
git checkout -- <file_name>
➡️ Reverts uncommitted changes in a specific file.
33. How do you remove a file from the staging area?
git reset <file_name>
➡️ Removes a file from the staging area without deleting it.
34. How do you delete a file in Git?
git rm <file_name>
➡️ Deletes a file from the working directory and stages its deletion.
35. How do you rename a file in Git?
git mv <old_name> <new_name>
➡️ Renames a file in Git.
36. How do you set a global username in Git?
git config --global user.name "Your Name"
➡️ Sets your global Git username.
37. How do you set a global email in Git?
git config --global user.email "your.email@example.com"
➡️ Sets your global Git email.
38. How do you configure the default text editor for Git?
git config --global core.editor <editor_name>
➡️ Sets the default text editor for Git.
39. What is the .gitignore
file?
➡️ The .gitignore
file specifies which files and directories should be ignored by Git.
# Example .gitignore
*.log
node_modules/
40. How do you undo a merge in Git?
git merge --abort
➡️ Aborts a merge and returns to the pre-merge state.
41. How do you stage only part of a file’s changes?
git add -p <file_name>
➡️ Interactively stages selected parts of a file.
42. How do you squash multiple commits into one?
git rebase -i HEAD~<number_of_commits>
➡️ Squashes multiple commits into one using an interactive rebase.
43. What is a conflict in Git?
➡️ A conflict occurs when changes from different branches cannot be merged automatically.
44. How do you resolve a conflict in Git?
➡️ Edit the conflicted file, stage the resolved changes, and commit.
git add <file_name>
git commit
45. What is the difference between git stash pop
and git stash apply
?
➡️ git stash pop
applies the stashed changes and removes them from the stash, while git stash apply
applies the changes but keeps them in the stash.
git stash pop
git stash apply
46. How do you list all remotes in a Git repository?
git remote -v
➡️ Lists all remote repositories associated with the project.
47. How do you fetch changes from a remote repository?
git fetch <remote>
➡️ Fetches changes from the remote repository without merging them.
48. How do you check out a remote branch?
git checkout -t <remote>/<branch_name>
➡️ Checks out a remote branch to track it locally.
49. How do you check the differences between two commits?
git diff <commit_1> <commit_2>
➡️ Shows the differences between two commits.
50. How do you see the differences between the working directory and the last commit?
git diff HEAD
➡️ Shows the changes between the working directory and the last commit.
51. What is the purpose of the git config
command?
➡️ The git config
command is used to set configuration options for Git, such as user information and repository settings.
52. How do you check your Git configuration?
git config --list
➡️ Lists all Git configuration settings.
53. How do you change the URL of a remote repository?
git remote set-url <remote> <new_url>
➡️ Changes the URL of a specified remote repository.
54. What is a bare repository in Git?
➡️ A bare repository is a repository that does not have a working directory. It is used primarily as a remote repository.
55. How do you create a bare repository?
git init --bare <repository_name>
➡️ Initializes a new bare repository.
56. How do you find out which branch you are currently on?
git branch
➡️ Displays the current branch with an asterisk next to it.
57. How do you view the differences between the staging area and the working directory?
git diff --cached
➡️ Shows the differences between the staging area and the working directory.
58. How do you remove untracked files from your working directory?
git clean -f
➡️ Removes untracked files from your working directory.
59. What is the purpose of the HEAD
pointer in Git?
➡️ The HEAD
pointer indicates the current branch and the latest commit in that branch.
60. How do you view the current branch and commit?
git rev-parse --abbrev-ref HEAD
git rev-parse HEAD
➡️ Shows the current branch name and the latest commit hash.
61. What is the difference between git pull
and git fetch
?
➡️ git fetch
retrieves changes from a remote repository without merging, while git pull
fetches and merges the changes.
62. How do you add a remote repository?
git remote add <remote_name> <repository_url>
➡️ Adds a new remote repository.
63. How do you compare two branches?
git diff <branch_1> <branch_2>
➡️ Compares the differences between two branches.
64. How do you cherry-pick a commit?
git cherry-pick <commit_hash>
➡️ Applies the changes from a specific commit to the current branch.
65. How do you view the commit history in a graph format?
git log --graph --oneline --all
➡️ Displays the commit history in a graphical format.
66. How do you revert changes made in a specific commit?
git revert <commit_hash>
➡️ Reverts the changes made in a specific commit.
67. What does the command git diff HEAD~1
do?
➡️ It shows the differences between the current working directory and the last commit.
68. How do you create a new branch and immediately switch to it?
git checkout -b <new_branch>
➡️ Creates a new branch and switches to it in one command.
69. How do you configure Git to remember your credentials?
git config --global credential.helper cache
➡️ Caches your credentials for a set amount of time.
70. How do you view a list of commits that have been made on a branch?
git log <branch_name>
➡️ Displays the commit history for a specified branch.
71. What does the command git reflog
do?
➡️ git reflog
shows a log of all the actions (including commits) that have moved the HEAD
pointer.
72. How do you reset a file to its last committed state?
git checkout -- <file_name>
➡️ Resets the specified file to the last committed state.
73. What is a merge commit?
➡️ A merge commit is a commit that combines the changes from two branches and has more than one parent.
74. How do you revert a merge commit?
git revert -m 1 <merge_commit_hash>
➡️ Reverts a merge commit, specifying which parent to keep.
75. How do you squash commits during a rebase?
git rebase -i HEAD~<number_of_commits>
➡️ Interactively squashes multiple commits into one during a rebase.
76. What is the purpose of the .git
directory?
➡️ The .git
directory contains all the metadata and object database for a Git repository.
77. How do you view the configuration of a specific remote?
git remote show <remote_name>
➡️ Displays detailed information about a specific remote.
78. How do you rename a remote?
git remote rename <old_name> <new_name>
➡️ Renames a remote repository.
79. How do you force push changes to a remote repository?
git push --force
➡️ Forces a push even if there are conflicts with the remote repository.
80. How do you check the commit history of a specific file?
git log <file_name>
➡️ Shows the commit history for a specific file.
81. How do you delete a Git tag?
git tag -d <tag_name>
git push <remote> :refs/tags/<tag_name>
➡️ Deletes a tag both locally and on the remote.
82. What is git archive
?
git archive --format=zip --output=<filename.zip> <branch_name>
➡️ Creates a zip archive of a specific branch or commit.
83. How do you check which files are tracked by Git?
git ls-files
➡️ Lists all files tracked by Git.
84. How do you stop tracking a file but keep it in the working directory?
git rm --cached <file_name>
➡️ Removes a file from Git tracking but keeps it in the working directory.
85. How do you create a patch file with Git?
git format-patch <commit_hash>
➡️ Creates a patch file for a specific commit.
86. How do you apply a patch in Git?
git apply <patch_file>
➡️ Applies a patch file to your current working directory.
87. How do you revert a pushed commit?
git revert <commit_hash>
git push <remote> <branch_name>
➡️ Reverts a pushed commit and applies the changes to the remote.
88. How do you move the last commit to a new branch?
git branch <new_branch_name>
git reset --hard HEAD~1
git checkout <new_branch_name>
➡️ Moves the last commit to a new branch and removes it from the current branch.
89. What is git clean
?
git clean -f
➡️ Removes untracked files from your working directory.
90. How do you list all remote URLs?
git remote -v
➡️ Lists all the remote repositories and their URLs.
91. How do you show the commit message of the last commit?
git log -1 --pretty=%B
➡️ Shows the commit message of the most recent commit.
92. How do you configure Git to use SSH?
➡️ Set up SSH keys and add them to your Git hosting service (like GitHub, GitLab).
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
93. How do you see the changes in a specific commit?
git show <commit_hash>
➡️ Displays the changes introduced in a specific commit.
94. **How do you create a
new commit with a message?**
git commit -m "Your commit message"
➡️ Creates a new commit with the specified message.
95. How do you view detailed information about a commit?
git log -p <commit_hash>
➡️ Shows detailed information about a specific commit, including changes made.
96. How do you remove a branch locally?
git branch -d <branch_name>
➡️ Deletes a specified local branch.
97. How do you rebase a branch onto another branch?
git checkout <branch_to_rebase>
git rebase <target_branch>
➡️ Rebases the current branch onto the specified target branch.
98. What is the purpose of .gitignore
?
➡️ The .gitignore
file specifies files and directories that Git should ignore and not track.
99. How do you see the changes in the staging area?
git diff --cached
➡️ Shows the changes staged for the next commit.
100. How do you initialize a new Git repository?
git init
➡️ Initializes a new Git repository in the current directory.
Thank you for reading up till this point🙌