Scenario-Based Questions

Estimated reading: 5 minutes 23 views

Scenario 1

You’re working on a feature branch, and you need to integrate the latest changes from the main branch. How do you proceed?

  • Answer: To incorporate the latest changes from the main branch into your feature branch, you can either:
    • Merge:
				
					git merge main

				
			
  • Rebase:
				
					git rebase main

				
			

Scenario 2

You’ve committed sensitive information (e.g., API keys) to the repository. How do you remove it from the commit history?
Answer: You can use git rebase or git filter-branch to rewrite history. One option is:

				
					git rebase -i <commit_hash>

				
			

Then delete the commit and force-push the changes:

				
					git push origin <branch_name> --force

				
			

Scenario 3

You’ve made local changes, but the remote branch has changes that you want to incorporate. What is the safest way to avoid overwriting your local changes?
Answer: You can pull the latest changes and rebase them onto your local branch:

				
					git pull --rebase origin <branch_name>

				
			

Scenario 4

You are working on a branch and someone else pushes a commit to the main branch. How do you incorporate their changes into your feature branch?
Answer: You should fetch the latest changes from the main branch and merge or rebase them into your branch:

  • Merge:
				
					git pull origin main

				
			
  • Rebase:
				
					git fetch origin
git rebase origin/main

				
			

Scenario 5

You want to temporarily switch to a different branch, but you don’t want to lose your uncommitted changes. What should you do?
Answer: You can stash your changes temporarily and switch branches:

				
					git stash
git checkout <branch_name>

				
			
  • Once you’re done, retrieve your stashed changes:
				
					git stash apply

				
			

Scenario 6

You’ve made a commit, but now you realize you want to undo the last few commits while keeping your changes in the working directory. How do you do this?
Answer: You can use git reset with the --soft option to undo the commits but retain the changes in the working directory:

				
					git reset --soft HEAD~<number_of_commits>

				
			

Scenario 7

You accidentally deleted a file and want to recover it from the last commit. How do you recover the file?
Answer: You can recover the file from the last commit by using:

				
					git checkout HEAD -- <file_name>

				
			

Scenario 8

You want to see the differences between two commits. How do you compare them?
Answer: You can use the git diff command to compare two commits:

				
					git diff <commit_hash_1> <commit_hash_2>

				
			

Scenario 9

You want to unstage a file that has been added to the staging area but keep the changes in your working directory. How do you do this?
Answer: You can unstage the file without losing the changes using:

				
					git reset <file_name>

				
			

Scenario 10

You want to make sure that a file is ignored by Git and never tracked or committed again. How can you achieve this?
Answer: Add the file to your .gitignore file and remove it from the Git index if it has been tracked before:

				
					git rm --cached <file_name>

				
			

Then commit the changes to .gitignore:

				
					git add .gitignore
git commit -m "Add <file_name> to .gitignore"

				
			

Scenario 11

You’ve staged some changes, but now you want to commit only a subset of them. How do you commit only the selected changes?
Answer: Use git add -p to interactively select the changes to commit:

				
					git add -p
git commit -m "Commit selected changes"

				
			

Scenario 12

You accidentally committed a large file that shouldn’t have been included in the repository. How do you remove the large file from the history of the repository?
Answer: You can use git filter-branch or the BFG Repo-Cleaner to remove large files from the history:

				
					git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <file_name>" --prune-empty --tag-name-filter cat -- --all

				
			

Scenario 13

You’ve pushed a commit that is causing problems, and you need to undo it without affecting the rest of the commit history. How can you do this?
Answer: Use git revert to create a new commit that undoes the changes of a previous commit:

				
					git revert <commit_hash>

				
			

Scenario 14

You want to see the differences between your working directory and the last commit (including staged and unstaged changes). How can you view the changes?
Answer: You can use:

				
					git diff

				
			

Scenario 15

You made a commit that you don’t want to include anymore, and you want to remove it completely from the repository. How do you delete this commit?
Answer: You can use git reset with the --hard option to remove the commit and reset the repository to a previous state:

				
					git reset --hard <commit_hash>

				
			

Scenario 16

You’ve made a commit but realized you forgot to include a file. How do you add that file to the previous commit?
Answer: Use the --amend flag to include the file in the previous commit:

				
					git add <file_name>
git commit --amend --no-edit

				
			

Scenario 17

You have created a new branch and made several commits on it. However, you want to apply those changes to an already existing branch. How do you do that?
Answer: You can merge or rebase the new branch onto the existing branch:

  • Merge:
				
					git checkout <existing_branch>
git merge <new_branch>

				
			
  • Rebase:
				
					git checkout <existing_branch>
git rebase <new_branch>

				
			

Scenario 18

You’re working on a project where others are also pushing changes. After pushing your changes, you find there are conflicts with others’ changes. How do you resolve these conflicts?
Answer: To resolve conflicts, you need to manually fix the conflict markers in the files, then add and commit the resolved files:

				
					git pull origin <branch_name>  # Get the latest changes
# Resolve conflicts manually
git add <resolved_file>
git commit -m "Resolve merge conflict"

				
			

Leave a Comment

Share this Doc

Scenario-Based Questions

Or copy link

CONTENTS