Git plays a crucial role in software development, making it a frequent topic in developer job interviews. Prospective employers often test candidates on various Git scenarios to assess their problem-solving skills in version control. Here’s a curated list of top Git interview questions and answers, complete with explanations to help you confidently ace your next interview.

1. What to Do When You Accidentally Commit Sensitive Credentials to Git?

Accidentally committing sensitive credentials is a serious issue but can be fixed by removing the sensitive data from Git’s history:

  1. Use the git filter-repo tool (recommended for newer Git versions) or git filter-branch for older versions:
    # git filter-repo --path path/to/credentials --invert-paths
  2. Force-push your changes to overwrite the remote repository:
    # git push origin --force

Note: Let your team know about this change as rewriting Git history requires the repository to be re-cloned. This ensures the exposed credentials are completely removed from all systems.

2. How to Undo the Last Commit Without Losing Work?

If you’ve committed changes by mistake or too early and want to revise them, you can undo the last commit without losing your changes:

# git reset --soft HEAD~1

This command moves your changes back to the staging area so you can amend the commit or make additional edits before recommitting.

3. How Can You Discard Local Changes in a Specific File?

To discard local changes and revert a file to its last committed state, use:

# git checkout -- <file>

Alternatively, for newer versions of Git:

# git restore <file>

Note: These commands only affect tracked files. Untracked files are not impacted.

4. What Happens When You Run git reset --hard HEAD~1?

The git reset --hard HEAD~1 command performs the following actions:

  1. Resets the HEAD to the previous commit (HEAD~1): Essentially, it rewinds the repository to the state before the last commit.
  2. Deletes all uncommitted changes: Both staged and unstaged changes are permanently wiped out from the working directory.

Caution: This command should be used with care, as it can result in the irreversible loss of uncommitted changes.

5. How Can You Recover a Deleted Git Branch?

If you delete a branch by mistake, you can restore it using the branch’s commit history:

  1. Identify the branch's last commit using git reflog:# git reflog
  2. Use the commit SHA to recreate the branch:# git checkout -b <branch_name> <commit_SHA>

This command restores the branch, ensuring no work is lost.

6. How to Move Commits from the Wrong Branch to the Correct Branch?

If you accidentally committed changes to the wrong branch, follow these steps:

  1. Create a new branch from the current state:# git branch <correct-branch-name>
  2. Switch to the new branch:# git checkout <correct-branch-name>
  3. Return to the incorrect branch and reset it to remove the unintended commits:# git reset --hard HEAD~n

Replace n with the number of commits you want to remove from the incorrect branch.

7. How to Revert a Specific Commit Without Impacting Others?

To undo the changes introduced by a specific commit without altering the rest of your repository, use the git revert command:

# git revert <commit-hash>

This creates a new commit that undoes the selected commit, preserving a clean and consistent Git history.

8. What If You Clone a Repository Without Initializing Submodules?

If you forgot to include the --recurse-submodules flag when cloning a repository, you can initialize submodules later:

  1. Initialize the submodules:# git submodule init
  2. Update the submodules to sync them with the main repository:# git submodule update

This ensures all submodule dependencies are included and up to date in your local environment.

Conclusion

Proficiency in Git commands is not just a technical requirement but a reflection of your ability to handle real-world version control challenges. In addition to knowing common commands, demonstrating your capacity to resolve errors like accidental commits, deleted branches, or erroneous file changes can set you apart during interviews. Use this guide to bolster your confidence and prepare for Git-related questions often asked in developer interviews.

By mastering these scenarios, you not only enhance your problem-solving skills but also showcase a deep understanding of Git's capabilities, vital for collaborative software development.

We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,

DigitalOcean - 100$ free credit & Linode - 100$ free credit

Check some Exclusive Deals, HERE.

Also, check out DevOps Book You should read section.