Top 100 Git and GitHub MCQ Questions and Answers

Git and GitHub are essential tools in modern software development for version control, collaboration, and project management. These 100 MCQs are designed to help you better understand Git commands, workflows, and the integration with GitHub.

1. What is Git?

a) A distributed version control system
b) A centralized version control system
c) A text editor
d) A compiler

Answer:

a) A distributed version control system

Explanation:

Git is a distributed version control system that allows multiple developers to collaborate on a project. Each developer has a full copy of the project history locally. It is designed to track changes in source code and manage project versions.

Unlike centralized systems, Git allows developers to work offline and merge changes efficiently when they're back online. This makes it ideal for large projects and remote collaboration.

Git is widely used in software development because of its flexibility, speed, and distributed architecture.

2. What is the primary purpose of GitHub?

a) To host Git repositories
b) To write code
c) To debug programs
d) To compile code

Answer:

a) To host Git repositories

Explanation:

GitHub is a web-based platform used for hosting Git repositories. It enables collaboration by providing tools for version control, issue tracking, and project management, making it easier for teams to work together.

GitHub also integrates with various development tools and offers features like pull requests and code reviews, which streamline the development workflow.

GitHub is widely used in both open-source and enterprise projects, allowing developers to share, contribute to, and manage code efficiently.

3. What is the command to initialize a new Git repository?

a) git init
b) git start
c) git create
d) git begin

Answer:

a) git init

Explanation:

The git init command initializes a new Git repository in the current directory. It creates a hidden .git folder that stores the repository’s metadata, including history and configuration.

This command is used when starting a new project or converting an existing directory into a Git repository. Once the repository is initialized, you can start tracking files and committing changes.

Understanding git init is fundamental for using Git, as it is the starting point for any new repository.

4. What is a commit in Git?

a) A snapshot of changes
b) A branch
c) A remote repository
d) A bug fix

Answer:

a) A snapshot of changes

Explanation:

A commit in Git is a snapshot of the project at a specific point in time. It captures the changes made to files and directories, storing them in the repository's history. Each commit has a unique identifier (SHA) and contains metadata such as the author, date, and commit message.

Commits form the foundation of Git's version control system, allowing you to track progress, revert to previous states, and collaborate with others by sharing these snapshots.

Effective use of commits helps maintain a clean and understandable project history, enabling better collaboration and project management.

5. How do you check the status of your Git repository?

a) git check
b) git status
c) git info
d) git log

Answer:

b) git status

Explanation:

The git status command displays the state of your working directory and staging area. It shows which files are staged for the next commit, which are modified but not yet staged, and which are untracked by Git.

This command is crucial for keeping track of changes in your project and ensuring that you know what will be committed or what still needs to be added to the staging area.

Regular use of git status helps prevent mistakes by giving you a clear picture of the current state of your repository.

6. How do you add changes to the staging area in Git?

a) git add
b) git commit
c) git stage
d) git push

Answer:

a) git add

Explanation:

The git add command is used to add changes to the staging area in Git. This prepares the files for the next commit. You can add individual files, multiple files, or all changes using commands like git add filename or git add . to stage everything.

Staging allows you to control which changes will be committed. It's a useful feature for managing and organizing changes before creating a commit.

Using git add effectively ensures that only the intended changes are included in each commit, maintaining a clean project history.

7. How do you commit changes in Git?

a) git add
b) git commit
c) git push
d) git save

Answer:

b) git commit

Explanation:

The git commit command is used to save changes to the local repository. After staging changes with git add, you can use git commit -m "message" to create a commit with a message describing the changes.

Each commit is a snapshot of the project’s state at a particular point in time, allowing you to track changes and revert to earlier versions if needed.

Frequent, descriptive commits make it easier to manage and collaborate on projects, providing a clear history of development.

8. What is the command to view the commit history in Git?

a) git history
b) git log
c) git commit
d) git show

Answer:

b) git log

Explanation:

The git log command displays the commit history of the repository. It shows details like the commit hash, author, date, and the commit message. By default, it lists all the commits in reverse chronological order.

You can use options like git log --oneline to view a condensed version or git log -p to show the differences introduced by each commit.

Understanding the commit history is important for tracking changes, debugging issues, and reviewing the project’s development over time.

9. How do you remove a file from the staging area in Git?

a) git reset
b) git delete
c) git remove
d) git rm --cached

Answer:

d) git rm --cached

Explanation:

The git rm --cached command is used to remove a file from the staging area without deleting it from the working directory. This command only unstages the file, so it will not be included in the next commit, but it remains in the directory.

For example, if you’ve added a file by mistake, you can use git rm --cached filename to unstage it. The file will still exist locally but won't be committed until it is re-added.

This command is useful when you want to exclude files from a commit or undo a mistaken git add operation without losing the file entirely.

10. What is a branch in Git?

a) A pointer to a specific commit
b) A remote repository
c) A bug fix
d) A tag

Answer:

a) A pointer to a specific commit

Explanation:

A branch in Git is a pointer to a specific commit in the repository. It allows developers to work on different features, bug fixes, or experiments independently without affecting the main project.

For example, when you create a new branch with git branch new-feature, it starts from the current commit. Any changes you make on this branch will not affect the main branch until they are merged.

Branches provide a flexible way to manage multiple streams of work in parallel, making Git a powerful tool for collaborative development.

11. What is the command to create a new branch in Git?

a) git branch new-branch
b) git new-branch
c) git checkout branch
d) git switch branch

Answer:

a) git branch new-branch

Explanation:

The git branch new-branch command creates a new branch named "new-branch" in the current repository. This branch is based on the current commit and allows you to work on changes independently from other branches.

Once the new branch is created, you can switch to it using git checkout new-branch or git switch new-branch to start working on it. This separation allows multiple developers to work on different features without affecting each other's work.

Branches are fundamental to collaborative workflows, enabling teams to develop features in parallel and later merge them into the main project.

12. What is the command to switch to another branch in Git?

a) git checkout branch-name
b) git switch branch-name
c) Both a and b
d) git change branch-name

Answer:

c) Both a and b

Explanation:

In Git, you can switch to another branch using either git checkout branch-name or git switch branch-name. The checkout command was traditionally used for this purpose, but newer versions of Git introduced the switch command for clarity.

Both commands achieve the same result, but switch is more intuitive for switching branches, whereas checkout can be used for other operations like switching to a specific commit.

Switching branches allows you to move between different streams of development and work on multiple features without losing progress on any of them.

13. How do you merge a branch into the current branch in Git?

a) git merge branch-name
b) git merge commit
c) git combine branch-name
d) git rebase branch-name

Answer:

a) git merge branch-name

Explanation:

The git merge branch-name command is used to merge another branch into the current branch. For example, if you're on the main branch and want to merge a feature branch, you would run git merge feature-branch.

Merging combines the changes from both branches, allowing you to integrate new features or updates into the main branch. If there are conflicts between the branches, Git will prompt you to resolve them before completing the merge.

Merging is a common operation in Git, as it allows developers to bring their independent work together into a single, unified project.

14. What is a conflict in Git?

a) When two branches have changes to the same lines of code
b) When a file is deleted
c) When a file is added
d) When the repository is corrupted

Answer:

a) When two branches have changes to the same lines of code

Explanation:

A conflict in Git occurs when two branches have made changes to the same lines of code or the same file, and Git is unable to automatically merge them. This typically happens when different developers make changes to the same part of the codebase.

When a conflict occurs, Git will mark the conflicting sections in the file, and it's up to the developer to manually resolve the conflict by choosing which changes to keep.

Understanding how to resolve conflicts is crucial for collaborative development, ensuring that all team members’ changes are properly integrated into the project.

15. What is a pull request in GitHub?

a) A request to merge changes from one branch to another
b) A request to delete a branch
c) A request to clone a repository
d) A request to reset a branch

Answer:

a) A request to merge changes from one branch to another

Explanation:

A pull request in GitHub is a request to merge changes from one branch into another, typically from a feature branch to the main branch. It allows developers to review, discuss, and approve changes before they are merged into the main project.

Pull requests facilitate collaboration by providing a formal process for reviewing code changes and ensuring that all team members have an opportunity to give feedback or suggest improvements.

Using pull requests in GitHub is an essential part of modern development workflows, promoting better code quality and collaboration.

16. How do you clone a Git repository?

a) git clone
b) git pull
c) git fetch
d) git copy

Answer:

a) git clone

Explanation:

The git clone command is used to copy a Git repository from a remote server to your local machine. It downloads the entire history of the repository and creates a working directory for you to start working on the project.

For example, running git clone https://github.com/user/repo.git will clone the repository to your local system. This command is typically used to start working on an existing project.

Understanding how to clone repositories is essential for collaborative development, as it allows you to work on projects hosted on GitHub or other Git hosting services.

17. What is the command to fetch changes from a remote repository without merging them?

a) git pull
b) git fetch
c) git merge
d) git update

Answer:

b) git fetch

Explanation:

The git fetch command retrieves updates from a remote repository but does not automatically merge them into your working directory. This allows you to review the changes before deciding whether to merge them into your branch.

After fetching changes, you can use git merge to integrate the fetched changes into your local branch. This is useful for safely incorporating updates without automatically applying them.

Using git fetch gives you more control over the update process, especially when working on critical code that requires careful review before merging.

18. How do you push your changes to a remote repository?

a) git commit
b) git push
c) git send
d) git upload

Answer:

b) git push

Explanation:

The git push command is used to upload local repository changes to a remote repository. It transfers commits from your local branch to the corresponding branch on the remote server, making your changes available to other collaborators.

For example, git push origin main will push your local changes to the main branch on the remote repository named origin.

Understanding git push is crucial for sharing your work with others and collaborating on projects hosted on platforms like GitHub.

19. What is a fork in GitHub?

a) A copy of a repository in your GitHub account
b) A branch of a repository
c) A pull request
d) A reset of a branch

Answer:

a) A copy of a repository in your GitHub account

Explanation:

A fork in GitHub is a personal copy of someone else's repository that you create in your GitHub account. Forking a repository allows you to experiment with changes without affecting the original project.

After forking a repository, you can make changes to it and later submit a pull request to the original repository if you want to contribute your changes. This is commonly used in open-source projects to encourage collaboration.

Forking is an essential feature of GitHub, enabling developers to work on independent copies of projects and contribute back to the main repository.

20. How do you pull changes from a remote repository and merge them into your local branch?

a) git fetch
b) git pull
c) git merge
d) git checkout

Answer:

b) git pull

Explanation:

The git pull command is used to fetch changes from a remote repository and automatically merge them into your local branch. It combines the functionality of git fetch and git merge in a single step.

For example, git pull origin main will pull the latest changes from the main branch of the origin remote repository and merge them into your local branch.

Using git pull ensures that your local branch stays up-to-date with the remote repository, especially when working collaboratively on shared projects.

21. How do you delete a branch locally in Git?

a) git branch -d branch-name
b) git remove branch-name
c) git delete branch-name
d) git erase branch-name

Answer:

a) git branch -d branch-name

Explanation:

The git branch -d branch-name command is used to delete a branch locally in Git. It removes the specified branch from your local repository but does not affect any remote branches.

For example, git branch -d feature-branch deletes the feature-branch from your local environment. If you want to force-delete a branch, you can use -D instead of -d.

Deleting branches is important for keeping your repository organized, especially after a feature branch has been merged or is no longer needed.

22. What does git stash do in Git?

a) Temporarily saves uncommitted changes
b) Merges branches
c) Deletes a branch
d) Resets the repository

Answer:

a) Temporarily saves uncommitted changes

Explanation:

The git stash command temporarily saves uncommitted changes in your working directory without committing them. It allows you to clean your working directory without losing changes and later restore them when needed.

For example, if you are working on some code but need to switch branches, you can run git stash to save your changes. After switching branches, you can use git stash pop to restore the stashed changes.

git stash is useful for temporarily shelving work when you need to focus on something else or switch branches without committing unfinished changes.

23. How do you list all the branches in a Git repository?

a) git list
b) git branches
c) git show-branches
d) git branch

Answer:

d) git branch

Explanation:

The git branch command lists all the branches in your repository. It shows the currently checked-out branch with an asterisk (*) next to it.

For example, running git branch displays a list of all local branches. To view both local and remote branches, you can use git branch -a.

Listing branches helps you keep track of different streams of work in your repository and easily switch between them when necessary.

24. How do you reset the current branch to a specific commit?

a) git revert commit-hash
b) git reset commit-hash
c) git delete commit-hash
d) git undo commit-hash

Answer:

b) git reset commit-hash

Explanation:

The git reset commit-hash command resets the current branch to a specific commit. It modifies the commit history by moving the current branch pointer to the specified commit and optionally changing the working directory and index.

For example, running git reset --hard abc123 will reset the branch to commit abc123 and remove any changes made after that commit.

git reset is useful when you need to undo recent changes or return the repository to a previous state.

25. What is the command to undo the last commit in Git?

a) git reset --hard HEAD^
b) git revert HEAD
c) Both a and b
d) git delete HEAD

Answer:

c) Both a and b

Explanation:

In Git, you can undo the last commit using either git reset --hard HEAD^ or git revert HEAD. The git reset command removes the commit and changes, while git revert creates a new commit that undoes the changes made by the last commit.

For example, git reset --hard HEAD^ moves the HEAD pointer to the previous commit and discards all changes. On the other hand, git revert HEAD is safer because it doesn't alter the commit history, making it ideal for shared branches.

Understanding both methods is essential for correcting mistakes in Git while preserving the integrity of your project’s commit history.

26. What is the purpose of a GitHub repository's README.md file?

a) To describe the project and provide instructions
b) To store sensitive information
c) To commit code changes
d) To manage branches

Answer:

a) To describe the project and provide instructions

Explanation:

A README.md file in a GitHub repository is used to describe the project, provide installation instructions, usage details, and any other relevant information. It is written in Markdown and typically serves as the first document users see when they visit the repository.

For example, a README.md file might contain instructions on how to install and use the project, as well as contribution guidelines.

Having a well-written README.md is crucial for making your repository more accessible to contributors and users, improving the overall project documentation.

27. How do you remove a file from both the working directory and Git index?

a) git delete file
b) git rm file
c) git remove file
d) git reset file

Answer:

b) git rm file

Explanation:

The git rm file command removes a file from both the working directory and the Git index (staging area). This means the file will no longer be tracked by Git, and it will also be deleted from your local file system.

For example, running git rm filename will stage the file for deletion in the next commit and remove it from your local directory.

Use this command when you want to permanently remove a file from the project and stop tracking it in future commits.

28. What is a Git tag?

a) A label for a specific commit
b) A branch of a repository
c) A commit message
d) A pull request

Answer:

a) A label for a specific commit

Explanation:

A Git tag is a label used to mark a specific commit in the repository's history. Tags are typically used to indicate important milestones, such as software releases (e.g., v1.0).

You can create a tag using git tag tagname, which points to the current commit. Tags are helpful for referencing a specific point in the repository’s history without worrying about changes to branches.

Using tags makes it easier to manage and reference specific versions of a project, particularly when releasing software to users.

29. How do you create an annotated tag in Git?

a) git tag -a tagname
b) git create tagname
c) git annotate tagname
d) git label tagname

Answer:

a) git tag -a tagname

Explanation:

The git tag -a tagname command creates an annotated tag in Git. Annotated tags include additional metadata such as the author’s name, date, and a message, making them more informative than lightweight tags.

For example, running git tag -a v1.0 -m "Version 1.0 release" creates an annotated tag for version 1.0 with a message describing the tag.

Annotated tags are useful for marking releases and milestones, providing more context and history than lightweight tags.

30. How do you view the history of tags in Git?

a) git show-tags
b) git tag
c) git log --tags
d) git history

Answer:

b) git tag

Explanation:

The git tag command lists all tags in the repository. Tags are useful for marking important commits, such as release points, and the git tag command helps you see all available tags in one place.

You can use options like git tag -l "v1.*" to list tags matching a specific pattern (e.g., all version 1 tags). This is helpful when navigating through different versions of the project.

Tags are crucial for version control and release management, providing a way to reference specific points in the project’s history.

31. What is a Git remote?

a) A repository hosted on a server
b) A local branch
c) A tag
d) A commit

Answer:

a) A repository hosted on a server

Explanation:

A Git remote is a reference to a repository hosted on a server, such as GitHub, GitLab, or a private server. It allows you to collaborate with others by pushing and pulling changes between your local repository and the remote one.

For example, origin is a common name for the default remote repository when you clone a repository. You can add additional remotes using git remote add.

Understanding remotes is key to collaborating with other developers and managing distributed workflows in Git.

32. How do you add a remote repository in Git?

a) git add remote
b) git remote add
c) git remote new
d) git push remote

Answer:

b) git remote add

Explanation:

The git remote add command is used to add a new remote repository to your Git configuration. It links your local repository to the remote, allowing you to push and pull changes between them.

For example, git remote add origin https://github.com/user/repo.git adds the origin remote, which points to the specified GitHub repository.

Adding a remote is essential for collaborating on projects, as it enables you to synchronize your work with a central repository.

33. What is the purpose of the git diff command?

a) To display the differences between two commits
b) To switch branches
c) To reset the repository
d) To delete a branch

Answer:

a) To display the differences between two commits

Explanation:

The git diff command is used to display the differences between commits, branches, or the working directory and staging area. It shows what has changed between two states, helping you review code before committing or merging changes.

For example, git diff compares the working directory with the staging area, while git diff commit1 commit2 compares two specific commits.

Understanding git diff is important for reviewing changes, spotting errors, and ensuring that only the intended modifications are committed.

34. How do you rename a Git branch?

a) git rename branch new-name
b) git branch -m old-name new-name
c) git branch new-name
d) git switch branch new-name

Answer:

b) git branch -m old-name new-name

Explanation:

The git branch -m old-name new-name command is used to rename a branch in Git. It changes the name of the branch both locally and remotely if you push the updated branch to the remote repository.

For example, running git branch -m feature-branch awesome-feature will rename feature-branch to awesome-feature.

Renaming branches helps keep your repository organized, especially when the branch name no longer reflects the current work or feature.

35. What does the git log --oneline command do?

a) Displays a condensed view of the commit history
b) Shows the current branch
c) Displays the differences between commits
d) Displays the working directory status

Answer:

a) Displays a condensed view of the commit history

Explanation:

The git log --oneline command displays the commit history in a condensed format, showing only the commit hash and message in a single line for each commit. This provides a more readable and compact view of the repository’s history.

For example, running git log --oneline outputs a simplified list of all commits, making it easier to quickly review past changes.

This command is useful when you want a quick overview of the repository’s history without detailed commit information.

36. How do you rebase a branch in Git?

a) git merge branch
b) git rebase branch
c) git pull branch
d) git sync branch

Answer:

b) git rebase branch

Explanation:

The git rebase branch command is used to apply the commits from one branch on top of another. It rewrites the commit history to make it appear as though the branch was created from the latest commit in the base branch.

For example, git rebase main will reapply your branch’s commits on top of the latest commits in the main branch.

Rebasing is useful for keeping a clean and linear project history, but it should be used carefully, especially on shared branches.

37. What is the difference between git pull and git fetch?

a) git pull fetches and merges changes, while git fetch only downloads changes
b) git fetch deletes remote changes, while git pull updates your repository
c) git fetch resets the repository, while git pull clones a new repository
d) Both commands do the same thing

Answer:

a) git pull fetches and merges changes, while git fetch only downloads changes

Explanation:

The difference between git pull and git fetch is that git pull fetches changes from a remote repository and merges them into your local branch, while git fetch only downloads the changes without merging them.

After fetching changes, you can manually merge them into your local branch. Pulling automatically merges the changes, saving you a step.

Knowing when to use each command helps you control the update process and avoid unintended merges.

38. How do you compare two commits in Git?

a) git diff commit1 commit2
b) git compare commit1 commit2
c) git log commit1 commit2
d) git show commit1 commit2

Answer:

a) git diff commit1 commit2

Explanation:

The git diff commit1 commit2 command compares the differences between two commits. It shows the changes made in each commit, helping you understand what has been modified between two points in the project’s history.

For example, git diff abc123 def456 compares the changes between the commits with hashes abc123 and def456.

Understanding how to compare commits is essential for reviewing code, debugging, and tracking the evolution of your project.

39. What is the purpose of git blame?

a) To show who made changes to each line of a file
b) To delete a commit
c) To reset the repository
d) To merge branches

Answer:

a) To show who made changes to each line of a file

Explanation:

The git blame command shows who made changes to each line of a file, along with the commit hash and timestamp for each change. This is useful for identifying when and by whom specific changes were introduced.

For example, running git blame filename will display each line of the file, along with the author and commit details responsible for that line.

git blame is helpful for tracking down the source of bugs or understanding the history of changes to a particular file.

40. What is the purpose of .gitignore?

a) To specify files and directories that Git should ignore
b) To reset the repository
c) To clone a repository
d) To view the commit history

Answer:

a) To specify files and directories that Git should ignore

Explanation:

The .gitignore file is used to specify files and directories that Git should ignore. This prevents certain files (such as temporary files, build outputs, or sensitive information) from being tracked or included in commits.

For example, a typical .gitignore might include entries like *.log to ignore all log files or node_modules/ to ignore the node_modules directory in a JavaScript project.

Using .gitignore helps keep your repository clean by excluding unnecessary or sensitive files from version control.

41. What does the command git remote -v do?

a) Lists all remote repositories with URLs
b) Verifies the connection to the remote repository
c) Deletes a remote repository
d) Displays commit history from the remote repository

Answer:

a) Lists all remote repositories with URLs

Explanation:

The git remote -v command lists all remote repositories associated with your local Git repository, along with their URLs. The -v flag stands for "verbose," and it displays both the fetch and push URLs for each remote.

This command is useful for checking the configuration of your remotes, especially when working with multiple remotes or verifying that your repository is correctly linked to its GitHub counterpart.

It’s an essential tool for managing remote repositories in collaborative workflows.

42. How do you undo changes in the working directory that haven’t been committed yet?

a) git revert
b) git reset --hard
c) git checkout -- file
d) git delete

Answer:

c) git checkout -- file

Explanation:

The command git checkout -- file is used to undo changes in the working directory for a specific file, reverting it to the state from the last commit. It does not affect staged changes, only changes in the working directory.

For example, if you’ve made changes to file.txt but haven’t committed them yet, running git checkout -- file.txt will discard the modifications.

This is useful when you want to revert uncommitted changes and reset the file to its previous state.

43. What does the command git clean -f do?

a) Deletes untracked files from the working directory
b) Resets all staged changes
c) Resets the repository to its last commit
d) Deletes remote branches

Answer:

a) Deletes untracked files from the working directory

Explanation:

The git clean -f command removes untracked files from the working directory. These are files that are not part of the version control system. The -f flag stands for "force" and is required to perform this operation.

For example, running git clean -f will delete any untracked files, helping to clean up your working directory when you no longer need those files.

This command is useful for tidying up your workspace, but it should be used with caution, as deleted files cannot be recovered from Git.

44. How do you discard changes from the staging area in Git?

a) git remove file
b) git reset HEAD file
c) git revert HEAD file
d) git clean -f file

Answer:

b) git reset HEAD file

Explanation:

The git reset HEAD file command removes a file from the staging area without deleting the changes in the working directory. This allows you to unstage the file but keep the changes intact locally.

For example, if you’ve staged file.txt and decide you don’t want to include it in the next commit, you can run git reset HEAD file.txt to remove it from the staging area.

This command is useful for managing the content that will be included in the next commit without losing any changes in the file.

45. What does git reflog display?

a) A history of all reference updates
b) A list of untracked files
c) A history of all pull requests
d) A list of all merged branches

Answer:

a) A history of all reference updates

Explanation:

The git reflog command displays a history of all reference updates in your Git repository, including branch checkouts, commits, and resets. It shows all movements of the HEAD pointer, even those that are not recorded in the commit history.

For example, running git reflog will list every change made to HEAD, allowing you to recover lost commits or branches that were reset or modified.

git reflog is especially useful for undoing mistakes and tracking the repository's history more comprehensively than the regular git log.

46. How do you amend the last commit in Git?

a) git commit --amend
b) git reset --amend
c) git change HEAD
d) git fix commit

Answer:

a) git commit --amend

Explanation:

The git commit --amend command is used to modify the last commit. This allows you to change the commit message or include additional changes without creating a new commit.

For example, if you forgot to include a file in your last commit, you can stage the file and run git commit --amend to add it to the previous commit.

This command is useful for making minor corrections to your last commit without cluttering the commit history.

47. How do you merge changes from the remote repository into your local branch?

a) git pull
b) git fetch
c) git merge
d) git commit

Answer:

a) git pull

Explanation:

The git pull command is used to fetch changes from a remote repository and automatically merge them into your local branch. It combines the functionality of git fetch and git merge into a single command.

For example, git pull origin main will fetch changes from the main branch of the origin remote and merge them into your local branch.

This command is essential for keeping your local repository synchronized with the remote repository, especially when working collaboratively on a project.

48. What does git cherry-pick do?

a) Applies a specific commit from one branch to another
b) Deletes a specific commit
c) Creates a new branch
d) Reverts changes made in a commit

Answer:

a) Applies a specific commit from one branch to another

Explanation:

The git cherry-pick command applies the changes from a specific commit to the current branch. This allows you to take one or more commits from another branch and apply them individually, without merging the entire branch.

For example, running git cherry-pick abc123 will apply the commit with hash abc123 to the current branch.

This command is useful for selectively bringing changes into a branch, especially when you only need certain updates or fixes from another branch.

49. What does the command git bisect do?

a) Helps find the commit that introduced a bug
b) Splits a commit into multiple parts
c) Creates a new commit
d) Shows the differences between two commits

Answer:

a) Helps find the commit that introduced a bug

Explanation:

The git bisect command helps you find the commit that introduced a bug by performing a binary search through the commit history. It repeatedly checks for bad commits by asking you to mark them as "good" or "bad" until the problematic commit is identified.

For example, running git bisect start initiates the bisect process, and Git will guide you through checking each commit in the search range.

This is a powerful debugging tool that allows you to quickly locate the commit where an issue was introduced.

50. How do you list all tags in a Git repository?

a) git tags
b) git tag --list
c) git tag -a
d) git list-tags

Answer:

b) git tag --list

Explanation:

The git tag --list command lists all tags in the repository. Tags are typically used to mark specific points in the repository's history, such as releases or important milestones.

You can also use git tag without options to list all tags, or apply filters like git tag -l "v1.*" to list specific versions.

Tags are an important part of version control, allowing you to reference key points in your project's history.

51. What does the git shortlog command do?

a) Summarizes the commit history by author
b) Displays the full commit history
c) Shows a brief log of remote changes
d) Displays file-level changes

Answer:

a) Summarizes the commit history by author

Explanation:

The git shortlog command provides a summary of the commit history grouped by author. It displays the number of commits each contributor has made, along with their commit messages.

This command is useful for getting an overview of who contributed to the project and how much work they’ve done without going through the entire log.

Understanding git shortlog is helpful for assessing contributions, especially in collaborative projects.

52. What is a “detached HEAD” in Git?

a) A state where HEAD points to a commit instead of a branch
b) A corrupted branch
c) A repository without a HEAD
d) A branch without commits

Answer:

a) A state where HEAD points to a commit instead of a branch

Explanation:

A “detached HEAD” state in Git occurs when HEAD points directly to a commit rather than a branch. This can happen when you checkout a specific commit instead of a branch, meaning any new commits you make won’t be associated with a branch.

While in a detached HEAD state, you can still make changes, but they are not linked to a branch unless you explicitly create one.

Understanding the detached HEAD state is important for avoiding confusion when working with commits that are not linked to branches.

53. How do you reapply commits on top of another base branch in Git?

a) git merge
b) git rebase
c) git pull
d) git cherry-pick

Answer:

b) git rebase

Explanation:

The git rebase command re-applies commits from the current branch on top of another base branch. It allows you to integrate changes from the base branch and rewrite commit history as if the branch was originally created from the updated base.

For example, git rebase main will rebase the current branch on top of the main branch, applying your changes after the latest main commits.

Rebasing creates a linear history, making it cleaner but should be used cautiously to avoid conflicts in shared branches.

54. What does the git stash apply command do?

a) Applies the latest stashed changes without deleting them
b) Deletes stashed changes
c) Commits stashed changes
d) Creates a new stash

Answer:

a) Applies the latest stashed changes without deleting them

Explanation:

The git stash apply command applies the latest stashed changes back into your working directory without deleting them from the stash. This allows you to restore the saved state while keeping the stash intact for future use.

For example, if you have stashed changes and want to reapply them later, git stash apply will restore those changes while keeping the stash available.

Using git stash apply is helpful when you need to temporarily save and later restore changes during context switching.

55. What does the command git pull --rebase do?

a) Fetches and rebases changes from the remote branch
b) Fetches and creates a new branch
c) Deletes remote changes
d) Creates a new commit on the remote repository

Answer:

a) Fetches and rebases changes from the remote branch

Explanation:

The git pull --rebase command fetches changes from the remote repository and rebases them onto your local branch instead of merging them. This ensures a linear commit history by applying your local commits on top of the fetched changes.

This is useful when you want to avoid merge commits and keep the project history cleaner.

Rebasing during a pull avoids unnecessary merge conflicts and ensures that your local changes are reapplied after the latest remote updates.

56. What does the git archive command do?

a) Creates a zip or tar archive of the repository
b) Archives old commits
c) Deletes branches
d) Shows archived commits

Answer:

a) Creates a zip or tar archive of the repository

Explanation:

The git archive command is used to create an archive (zip, tar, etc.) of the contents of a Git repository. It doesn’t include the Git metadata but packages the working directory files at a particular point in time.

This is useful for generating distribution-ready archives of a specific version of the project without including the entire repository history.

You can also use git archive to export a particular branch or tag.

57. How do you view all stashes in a Git repository?

a) git stash list
b) git log --stash
c) git stash show
d) git reflog stash

Answer:

a) git stash list

Explanation:

The git stash list command shows all the stashes saved in the repository. Each stash is listed with a unique identifier, making it easier to reference and apply later.

For example, running git stash list will output a list of all saved stashes, including the latest one and any previously created stashes.

This command is useful for managing multiple stashes and reapplying or dropping them as needed.

58. What does the git diff --cached command show?

a) Differences between the index (staging area) and the last commit
b) Differences between working directory and the index
c) Differences between two commits
d) Differences between local and remote branches

Answer:

a) Differences between the index (staging area) and the last commit

Explanation:

The git diff --cached command shows the differences between the index (staging area) and the last commit. It helps you review the changes that are about to be committed, allowing you to see what has been staged.

For example, running git diff --cached before committing will show you exactly what changes have been staged for the next commit.

This command is crucial for ensuring that only the intended changes are included in a commit.

59. How do you tag a specific commit in Git?

a) git tag tagname commit-hash
b) git tag -m commit-hash
c) git annotate commit-hash
d) git commit -t

Answer:

a) git tag tagname commit-hash

Explanation:

The git tag tagname commit-hash command tags a specific commit in Git by providing the tag name and commit hash. This allows you to mark a specific commit with a tag, even if it’s not the latest commit on a branch.

For example, running git tag v1.0 abc123 tags the commit with hash abc123 as version 1.0.

Tagging specific commits is useful for marking important milestones, such as software releases, in your project.

60. What does git push origin --tags do?

a) Pushes all tags to the remote repository
b) Pushes only the latest tag
c) Deletes remote tags
d) Pushes tags and removes untracked files

Answer:

a) Pushes all tags to the remote repository

Explanation:

The git push origin --tags command pushes all tags from your local repository to the remote repository. Tags are not automatically pushed with commits, so this command is necessary to make your tags available on the remote server.

For example, if you’ve created several tags locally and want to share them on GitHub or another remote, you can run git push origin --tags to push them all at once.

This is important when managing versioned releases in a project where tags mark key points like software versions.

61. What does the git stash drop command do?

a) Deletes the latest stash
b) Applies the latest stash
c) Lists all stashes
d) Commits the latest stash

Answer:

a) Deletes the latest stash

Explanation:

The git stash drop command deletes the latest stash from the stash list. This is useful when you no longer need a stash and want to remove it to keep the stash list clean.

For example, running git stash drop will remove the most recent stash, while specifying a stash with its identifier (e.g., git stash drop stash@{1}) will remove a specific stash.

Keeping your stash list organized is essential for managing multiple sets of saved changes effectively.

62. What is the command to view the commit history of a file?

a) git log filename
b) git history filename
c) git show-log filename
d) git commit-log filename

Answer:

a) git log filename

Explanation:

The git log filename command shows the commit history of a specific file. It lists all commits that modified the file, allowing you to trace the evolution of that file through the project’s history.

This is useful for tracking changes made to a particular file, identifying when a bug was introduced, or understanding the file’s development history.

Using git log filename helps you analyze how and when specific changes were made to the file.

63. What does the git show command do?

a) Displays details about a specific commit
b) Shows a list of all branches
c) Shows the current branch status
d) Displays a list of stashes

Answer:

a) Displays details about a specific commit

Explanation:

The git show command displays details about a specific commit, including the commit message, changes made, and file differences. By default, it shows the most recent commit, but you can specify a commit hash to view any commit in the history.

For example, git show abc123 shows the details of the commit with hash abc123, including the author, date, and changes made.

This command is useful for reviewing individual commits and understanding their impact on the project.

64. How do you rename a file in Git?

a) git mv oldname newname
b) git rename oldname newname
c) git move oldname newname
d) git update-file oldname newname

Answer:

a) git mv oldname newname

Explanation:

Renaming the file is very important when we are working on a project. Git Rename is used to rename a file in the working directory. Suppose we are working in a team and any fellow developer created the file and we want to rename it using Git or Github so this command help us to do so. We may change the name of the file even though we created the file due to some reason. To rename any file or folder, use git mv command which takes two arguments. The first argument is the source and the second is the destination. We can easily rename any file using the git command and the new name will be assigned to that file. We can rename the file using GitHub or the command line.

Step 1: Open Git Bash.

Step 2: Open the repository.

Step 3: Rename the file using the command:

git mv old_filename new_filename

Step 4: Use the “git status” command to check the changes.

Step 5: Commit the renamed file.

git commit -m "Renamed_file"

Step 6: Push the changes using git push origin branch_name

65. What does git rm --cached do?

a) Removes a file from the staging area but keeps it in the working directory
b) Deletes a file from both the staging area and working directory
c) Moves a file to a new location
d) Reverts the last commit

Answer:

a) Removes a file from the staging area but keeps it in the working directory

Explanation:

The git rm --cached command removes a file from the staging area but keeps it in the working directory. This unstages the file, preventing it from being included in the next commit, but the file itself remains in your local directory.

For example, running git rm --cached filename will untrack the file but keep it in the project directory.

This command is useful when you want to remove files from version control without deleting them locally.

66. What is a Git hook?

a) A script that runs automatically before or after Git events
b) A tool for resolving merge conflicts
c) A command to check remote branches
d) A tag for annotating commits

Answer:

a) A script that runs automatically before or after Git events

Explanation:

A Git hook is a script that runs automatically before or after specific Git events, such as commits, merges, or pushes. Hooks allow you to automate tasks, such as running tests, formatting code, or enforcing coding standards.

For example, a pre-commit hook can be used to check for code formatting issues before a commit is made.

Hooks are powerful tools for ensuring code quality and enforcing development workflows.

67. How do you resolve merge conflicts in Git?

a) Manually edit the conflicting files and commit the changes
b) Run git resolve
c) Delete the conflicting files
d) Reset the repository to a previous state

Answer:

a) Manually edit the conflicting files and commit the changes

Explanation:

To resolve merge conflicts in Git, you must manually edit the conflicting files to reconcile the changes, then stage and commit the resolved files. Git marks the conflicting sections in the files, and it's up to you to decide which changes to keep.

After resolving the conflicts, you can commit the changes with a new commit.

This process is essential for integrating code from different branches when changes have been made to the same parts of a file.

68. What is the command to list all untracked files in Git?

a) git status
b) git ls-files --others
c) git show untracked
d) git log --untracked

Answer:

b) git ls-files --others

Explanation:

The command git ls-files --others lists all untracked files in your working directory. These are files that have not been added to the index (staging area) and are not being tracked by Git.

Alternatively, you can use git status to see untracked files along with other status information.

Knowing which files are untracked is important when deciding which files to include in your repository.

69. How do you squash commits in Git?

a) By using git rebase -i (interactive rebase)
b) By using git squash
c) By using git commit --squash
d) By using git combine

Answer:

a) By using git rebase -i (interactive rebase)

Explanation:

To squash commits in Git, you use the git rebase -i (interactive rebase) command. Squashing commits combines multiple commits into a single one, simplifying the commit history.

During the rebase process, you can choose which commits to squash, which is useful for cleaning up your project history before merging it into the main branch.

This technique is often used to maintain a more concise and understandable commit history.

70. What does git revert do?

a) Creates a new commit that undoes the changes of a previous commit
b) Deletes the most recent commit
c) Resets the repository to a specific commit
d) Moves the HEAD pointer to the previous commit

Answer:

a) Creates a new commit that undoes the changes of a previous commit

Explanation:

The git revert command creates a new commit that undoes the changes introduced by a previous commit. It leaves the commit history intact, unlike git reset, which modifies the history.

For example, running git revert abc123 will create a new commit that reverts the changes made in the commit with hash abc123.

This command is ideal when you need to undo changes but still want to preserve the commit history for future reference.

71. What does the command git checkout -b branchname do?

a) Creates and switches to a new branch
b) Merges a branch
c) Deletes a branch
d) Checks out the latest commit on the main branch

Answer:

a) Creates and switches to a new branch

Explanation:

The git checkout -b branchname command creates a new branch with the specified name and immediately switches to it. This is a shortcut for creating a branch with git branch branchname followed by git checkout branchname.

For example, git checkout -b feature-branch creates and switches to a branch named feature-branch.

72. How do you rename a Git remote?

a) git remote rename oldname newname
b) git rename remote oldname newname
c) git remote move oldname newname
d) git rename remote newname

Answer:

a) git remote rename oldname newname

Explanation:

The git remote rename oldname newname command renames a remote in your repository. For example, if you want to rename origin to upstream, you would run git remote rename origin upstream.

This command is useful when you need to change the name of the remote without affecting its configuration or URL.

Renaming remotes can help better organize multiple remote repositories, especially in collaborative projects.

73. What does the git fetch --prune command do?

a) Removes references to branches that no longer exist on the remote
b) Deletes all branches on the remote
c) Fetches all branches from the remote
d) Updates the current branch to match the remote

Answer:

a) Removes references to branches that no longer exist on the remote

Explanation:

The git fetch --prune command removes local references to remote branches that have been deleted from the remote repository. It ensures that your local representation of the remote branches is up-to-date and free of stale references.

This command is useful for cleaning up local branches that no longer exist on the remote and maintaining a clean repository.

Using git fetch --prune regularly ensures that your local repository reflects the state of the remote repository accurately.

74. What does the git diff command show?

a) The differences between the working directory and the staging area
b) The commit history
c) The list of branches
d) The differences between two commits

Answer:

a) The differences between the working directory and the staging area

Explanation:

The git diff command shows the differences between the working directory and the staging area. It helps you see the changes that have been made but not yet staged for commit.

For example, if you've edited files but haven't run git add yet, git diff will display the modifications you've made.

This command is essential for reviewing changes before staging or committing them to ensure only the intended changes are included.

75. How do you reset your working directory to the last commit in Git?

a) git reset --hard
b) git revert HEAD
c) git checkout --last
d) git reset HEAD

Answer:

a) git reset --hard

Explanation:

The git reset --hard command resets your working directory, index (staging area), and HEAD pointer to the state of the last commit. It discards all changes in the working directory and the staging area, effectively reverting the repository to the last committed state.

This command is useful when you want to discard all uncommitted changes and return to a clean working directory.

Be cautious when using git reset --hard as it permanently deletes uncommitted changes.

76. How do you check out a specific commit in Git?

a) git checkout commit-hash
b) git revert commit-hash
c) git checkout branch-name
d) git pull commit-hash

Answer:

a) git checkout commit-hash

Explanation:

The git checkout commit-hash command checks out a specific commit by its hash. This places your repository in a detached HEAD state, allowing you to view or work with the project at the state of that commit.

For example, running git checkout abc123 will check out the commit with hash abc123.

Be aware that if you make new commits in this state, they won’t be attached to any branch unless you create a new one.

77. What does git blame display?

a) Shows who made changes to each line of a file
b) Shows a list of uncommitted changes
c) Shows the commit history
d) Shows all remote branches

Answer:

a) Shows who made changes to each line of a file

Explanation:

The git blame command shows who last modified each line of a file, along with the commit hash and author details. It’s useful for tracing the origin of a particular change, especially when debugging or reviewing code history.

For example, running git blame filename will display a breakdown of each line of the file, with information about the commit that last modified it.

This command is crucial for understanding the history of changes made to specific files in a project.

78. What does the git describe command do?

a) Describes the most recent tag reachable from a commit
b) Shows the description of the repository
c) Adds a description to a commit
d) Lists the differences between commits

Answer:

a) Describes the most recent tag reachable from a commit

Explanation:

The git describe command provides a human-readable description of a commit by finding the most recent tag that is reachable from the commit. It uses the tag, the number of commits since the tag, and a shortened commit hash to describe the commit.

For example, running git describe might return v1.2-3-gabc1234, meaning the commit is three commits after tag v1.2, and its short hash is gabc1234.

This command is useful for summarizing the state of the project at a particular commit.

79. How do you initialize a new Git repository?

a) git init
b) git start
c) git new
d) git create

Answer:

a) git init

Explanation:

The git init command initializes a new Git repository in the current directory. It creates a hidden .git folder that contains all the metadata and version control history for the project.

This is the first step when starting a new project with Git, as it allows you to begin tracking changes and commits in your project.

Once a repository is initialized, you can start adding files and making commits to track your project history.

80. What does the git submodule command do?

a) Manages external repositories within a Git repository
b) Creates branches for external modules
c) Merges multiple repositories into one
d) Deletes external modules

Answer:

a) Manages external repositories within a Git repository

Explanation:

The git submodule command allows you to manage external repositories within a Git project. It adds other repositories as submodules, enabling you to track and update external code dependencies separately from your main project.

For example, using git submodule add URL adds an external repository as a submodule to your project.

Submodules are useful for projects that rely on external libraries or components that need to be versioned independently.

81. What does the git show-ref command do?

a) Lists references (branches or tags) in a Git repository
b) Displays detailed information about a commit
c) Shows the current status of the working directory
d) Displays a summary of the commit history

Answer:

a) Lists references (branches or tags) in a Git repository

Explanation:

The git show-ref command lists references in a Git repository, such as branches or tags. It shows the reference names along with their corresponding commit hashes, making it useful for tracking references to specific commits.

This command helps you see all the reference points (tags, branches) in your repository and their associated commits.

It’s particularly helpful for identifying where different branches or tags point to in the repository history.

82. How do you create an alias for a Git command?

a) git config --global alias.alias-name 'git-command'
b) git alias create alias-name
c) git alias git-command alias-name
d) git create alias alias-name

Answer:

a) git config --global alias.alias-name 'git-command'

Explanation:

The git config --global alias.alias-name 'git-command' command creates an alias for a Git command, allowing you to use a shorter or custom command instead of the full command.

For example, you can create an alias for git status by running git config --global alias.st 'status', and then use git st instead of typing git status.

Using aliases can make frequent or complex commands easier to remember and faster to execute.

83. How do you ignore a file in Git without deleting it?

a) Add the file to .gitignore
b) Use git rm --cached filename
c) Delete the file locally
d) Use git reset filename

Answer:

a) Add the file to .gitignore

Explanation:

To ignore a file in Git without deleting it, you can add the file to the .gitignore file. This prevents Git from tracking changes to the file in future commits but leaves the file intact in your working directory.

For example, adding config.php to .gitignore will prevent Git from tracking changes to config.php, while keeping it available for use locally.

This is useful for ignoring sensitive files or configuration files that should not be committed to the repository.

84. What does git push --force do?

a) Forces the remote repository to accept changes, overwriting conflicts
b) Deletes the remote branch
c) Merges a remote branch with the local branch
d) Pushes tags to the remote repository

Answer:

a) Forces the remote repository to accept changes, overwriting conflicts

Explanation:

The git push --force command forces the remote repository to accept your changes, even if it would overwrite commits on the remote branch. This is often used after rebasing or resetting a branch locally.

For example, after using git reset to modify the commit history, you may need to use git push --force to push those changes to the remote repository, overwriting the previous history.

Be cautious when using --force as it can overwrite others' work on shared branches.

85. How do you delete a remote branch in Git?

a) git push origin --delete branch-name
b) git remove branch-name
c) git branch -d branch-name
d) git reset origin branch-name

Answer:

a) git push origin --delete branch-name

Explanation:

To delete a remote branch in Git, you use the git push origin --delete branch-name command. This removes the specified branch from the remote repository (e.g., GitHub).

For example, running git push origin --delete feature-branch will delete the feature-branch from the remote repository.

Deleting remote branches is useful for cleaning up after merging a feature branch or when a branch is no longer needed.

86. What does the git config --list command show?

a) Displays all configuration settings
b) Shows the commit history
c) Lists all local branches
d) Displays the differences between commits

Answer:

a) Displays all configuration settings

Explanation:

The git config --list command displays all Git configuration settings for the repository, including user information, aliases, remote repository URLs, and other settings defined in the Git configuration files.

For example, running git config --list will show settings like your user email, the default branch name, and any custom aliases you’ve set up.

This command is useful for reviewing and troubleshooting Git configuration issues.

87. What does git reset --soft do?

a) Moves the HEAD pointer to a specific commit but keeps changes in the working directory
b) Deletes uncommitted changes
c) Deletes the commit history
d) Stashes changes

Answer:

a) Moves the HEAD pointer to a specific commit but keeps changes in the working directory

Explanation:

The git reset --soft command moves the HEAD pointer to a specific commit but leaves changes in the index (staging area) and working directory untouched. This allows you to re-commit changes with a new commit message or make further modifications.

It’s useful when you want to amend a commit without losing any changes in your working directory or staging area.

88. How do you view the commit history of a repository in a graph format?

a) git log --graph
b) git graph
c) git log --history
d) git show --graph

Answer:

a) git log --graph

Explanation:

The git log --graph command displays the commit history in a graphical representation, showing branch structures and merges. It helps visualize the relationships between commits and branches.

This command is useful for reviewing the project’s commit history and understanding how branches have diverged and merged over time.

89. What does the git cherry-pick command do?

a) Applies a specific commit from one branch to another
b) Deletes a commit from the history
c) Resets the current branch to a specific commit
d) Combines multiple branches

Answer:

a) Applies a specific commit from one branch to another

Explanation:

The git cherry-pick command applies a specific commit from one branch to another without merging the entire branch. This is useful when you want to bring specific changes (commits) into your branch without pulling in unrelated work.

For example, git cherry-pick abc123 will apply the commit with hash abc123 to the current branch.

This command is useful for selectively incorporating changes from other branches.

90. How do you configure Git to use a specific text editor?

a) git config --global core.editor "editor-name"
b) git set editor editor-name
c) git edit config editor-name
d) git commit --editor editor-name

Answer:

a) git config --global core.editor "editor-name"

Explanation:

You can configure Git to use a specific text editor by running the command git config --global core.editor "editor-name". This sets the default text editor for Git commands like git commit that open an editor.

For example, running git config --global core.editor "vim" sets Vim as the default text editor for Git.

This command ensures that Git uses the editor of your choice for editing commit messages and other text files.

91. How do you undo the last local commit without removing the changes?

a) git reset --soft HEAD^
b) git revert HEAD^
c) git reset --hard HEAD^
d) git checkout --last

Answer:

a) git reset --soft HEAD^

Explanation:

The git reset --soft HEAD^ command undoes the last commit while keeping the changes in the staging area. This allows you to modify the commit or make additional changes before committing again.

For example, running this command moves the HEAD pointer back to the previous commit but leaves all changes intact.

This is useful when you want to amend or adjust your last commit without losing any work.

92. What does the git stash pop command do?

a) Applies the latest stashed changes and removes them from the stash list
b) Removes all stashed changes
c) Displays the list of stashes
d) Saves uncommitted changes without removing them

Answer:

a) Applies the latest stashed changes and removes them from the stash list

Explanation:

The git stash pop command applies the latest stashed changes to your working directory and removes them from the stash list. It’s similar to git stash apply but with the added step of removing the stash once applied.

This is useful when you want to retrieve stashed changes and no longer need to keep them in the stash list.

Be cautious, as any conflicts when applying the stash will need to be resolved manually.

93. What does the git gc command do?

a) Optimizes the Git repository by cleaning up unnecessary files and compressing objects
b) Generates a commit graph
c) Commits all changes automatically
d) Deletes all untracked files

Answer:

a) Optimizes the Git repository by cleaning up unnecessary files and compressing objects

Explanation:

The git gc command performs garbage collection in a Git repository, optimizing it by cleaning up unnecessary files and compressing loose objects. It helps reduce the size of the repository and improves performance.

For example, Git might run git gc automatically after a certain number of commits, but you can also run it manually to clean up the repository.

This is important for maintaining a healthy repository, especially after many changes or merges.

94. How do you squash commits during a rebase?

a) Use git rebase -i and mark the commits you want to squash as "squash"
b) Use git squash after rebasing
c) Use git merge --squash
d) Use git squash rebase

Answer:

a) Use git rebase -i and mark the commits you want to squash as "squash"

Explanation:

To squash commits during a rebase, you use the git rebase -i (interactive rebase) command and mark the commits you want to combine with "squash." This combines multiple commits into one, keeping the commit history cleaner.

During the interactive rebase process, you’ll be prompted to squash or reword commits as necessary.

Squashing commits is helpful for simplifying commit history before merging branches.

95. How do you create a new branch from an existing branch in Git?

a) git checkout -b new-branch
b) git branch create new-branch
c) git switch --create new-branch
d) git merge --create new-branch

Answer:

a) git checkout -b new-branch

Explanation:

The git checkout -b new-branch command creates a new branch and immediately switches to it. This is a commonly used command when you want to start work on a new feature or task.

This command is equivalent to running git branch new-branch followed by git checkout new-branch in a single step.

Creating new branches is essential for isolating work on features, bug fixes, or experiments.

96. How do you fetch all remote branches without merging them into your local branches?

a) git fetch
b) git pull --no-merge
c) git fetch --merge
d) git clone --all

Answer:

a) git fetch

Explanation:

The git fetch command retrieves all remote branches, tags, and commits without merging them into your local branches. It updates your local repository with new information from the remote repository but doesn’t affect your working directory.

For example, running git fetch will download all changes from the remote, allowing you to review or merge them later.

This command is important for synchronizing your local repository with the remote without making any immediate changes to your local branches.

97. What does git reset --hard HEAD^ do?

a) Resets the current branch to the previous commit and deletes all uncommitted changes
b) Deletes the last commit but keeps the changes in the working directory
c) Reverts the last commit and stages the changes
d) Moves the HEAD pointer to the first commit

Answer:

a) Resets the current branch to the previous commit and deletes all uncommitted changes

Explanation:

The git reset --hard HEAD^ command resets the current branch to the previous commit and deletes all uncommitted changes in both the working directory and staging area. It’s a forceful reset that discards all local changes.

This is useful when you want to completely undo the last commit and any modifications in the working directory, returning to a clean state.

Be careful when using this command, as it permanently deletes changes that aren’t committed.

98. How do you view the commit history with only the commit messages?

a) git log --oneline
b) git history --short
c) git log --brief
d) git log --message

Answer:

a) git log --oneline

Explanation:

The git log --oneline command shows the commit history in a compact format, displaying only the commit hash and commit message on a single line for each commit. This makes it easier to quickly browse through the commit history.

For example, running git log --oneline gives a concise overview of the project’s commit history without overwhelming detail.

This command is helpful for reviewing commit messages and identifying relevant commits at a glance.

99. What does git clone do?

a) Copies a remote Git repository to your local machine
b) Deletes a remote repository
c) Merges a remote repository into the current branch
d) Pushes changes to the remote repository

Answer:

a) Copies a remote Git repository to your local machine

Explanation:

The git clone command creates a copy of a remote Git repository on your local machine. It downloads the repository’s history, branches, and files, allowing you to work on the project locally.

For example, running git clone https://github.com/user/repo.git will download the repository located at that URL to your machine.

This is the first step when you want to contribute to an existing Git project.

100. What does git config --global user.name do?

a) Sets the user’s name for all Git repositories on the system
b) Displays the current user name
c) Adds a username to the local repository
d) Configures the remote repository username

Answer:

a) Sets the user’s name for all Git repositories on the system

Explanation:

The git config --global user.name command sets the user’s name for all Git repositories on the system. This name will be associated with your commits and is used to identify you as the author of those commits.

For example, running git config --global user.name "John Doe" sets your name globally, so it will appear in the author field of all future commits.

Setting the username is a crucial step in configuring Git to ensure that your commits are properly attributed to you.

Comments