Scenario-Based Questions Estimated reading: 5 minutes 23 views Scenario 1You’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 2You’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 Then delete the commit and force-push the changes: git push origin --force Scenario 3You’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 Scenario 4You 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 5You 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 Once you’re done, retrieve your stashed changes: git stash apply Scenario 6You’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~ Scenario 7You 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 -- Scenario 8You 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 Scenario 9You 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 Scenario 10You 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 Then commit the changes to .gitignore: git add .gitignore git commit -m "Add to .gitignore" Scenario 11You’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 12You 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 " --prune-empty --tag-name-filter cat -- --all Scenario 13You’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 Scenario 14You 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 15You 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 Scenario 16You’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 git commit --amend --no-edit Scenario 17You 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 git merge Rebase: git checkout git rebase Scenario 18You’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 # Get the latest changes # Resolve conflicts manually git add git commit -m "Resolve merge conflict"