CS FundamentalsHardProduct-company placement · Git interview
On main with tip D (history A-B-C-D) you accidentally run git reset --hard HEAD~2. Which command brings the branch back to D, assuming nothing else has been done?
- A. git revert HEAD~2
- B. git checkout HEAD~2
- C. git reset --hard HEAD@{1}
- D. git pull --rebase
Correct answer: C. git reset --hard HEAD@{1}
The reflog records where HEAD was: HEAD@{0} is now B and HEAD@{1} is D, the position before the reset. Resetting to it recovers the 'lost' commits, which still exist until garbage collection.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A merge commit M on main brought in a feature that turns out to be broken. Running git revert M fails. Why, and what is the fix?
- feature has commits X, then Y, then Z (oldest to newest). On main you run git cherry-pick X..Z. Which commits are applied?
- Your working tree has a modified tracked file f.txt and a new, never-added file u.txt. You run git stash. What does git status show next?
- A .gitignore contains the two lines logs/ and !logs/keep.log. A developer expects logs/keep.log to be tracked, but git status never shows…
- A team lead adds a pre-commit hook to .git/hooks/pre-commit in her repository and pushes. Teammates who clone the repository report that…
- After rebasing your branch you must update the remote branch. A colleague may have pushed to the same branch meanwhile. Why prefer git…