Top 40 Git Interview Questions and Answers [Updated 2022]

Top 50 Git Interview Questions and Answers | Git Interview Preparation | DevOps Training | Edureka

You have accidentally deleted a branch. Is there any way to recover it and bring it back?

Using the git reflog command, you are able to inspect all of the recent HEAD pointer movements in this repository. If the deletion hasnt been too long ago, you should be able to find the last commit on this branch. Using that commits SHA-1 hash, you are then able to recreate the branch.

In case you are using the Tower Git client, you can undo most actions simply by hitting CMD+Z. Heres how to restore a deleted branch:

How can you restore a previous version of your project?

The git reset command is perfect for this task: you can provide the SHA-1 hash of the commit you want to return to and thereby restore the project to that revisions state.

43. Explain the difference between reverting and resetting.

  • Git reset is a powerful command that is used to undo local changes to the state of a Git repository. Git reset operates on “The Three Trees of Git” which are, Commit History ( HEAD ), the Staging Index, and the Working Directory.
  • Revert command in Git creates a new commit that undoes the changes from the previous commit. This command adds a new history to the project. It does not modify the existing history.
  • 31. Explain the different points when a merge can enter a conflicted stage.

    There are two stages when a merge can enter a conflicted stage.

    If there are changes in the working directory of the stage area in the current project, the merge will fail to start. In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands.

    The failure during the merge process indicates that there’s a conflict between the local branch and the branch being merged. In this case, Git resolves as much as possible, but some things have to be fixed manually in the conflicted files.

    How can you undo a commit that has already been pushed to a shared branch on a remote repository?

    First of all, using git push --force would not be a good solution in most cases. The reason for this is that force push has the potential to overwrite others work or at least change their commit history in an unexpected way.

    A better, unobtrusive approach would be to use git revert, because this will not rewrite / manipulate old commit history. Instead, a new commit will be created that corrects the effects of the unwanted one(s).

    24. What do you understand about the Staging area in Git?

    The Staging Area in Git is when it starts to track and save the changes that occur in files. These saved changes reflect in the .git directory. Staging is an intermediate area that helps to format and review commits before their completion.

    git interview questions

    16. Name a few Git commands and explain their usage.

    Below are some basic Git commands:

    Command Function
    git rm [file] deletes the file from your working directory and stages the deletion.
    git log list the version history for the current branch.

    git show [commit]

    shows the metadata and content changes of the specified commit.

    git tag [commitID]

    used to give tags to the specified commit.

    git checkout [branch name]

    git checkout -b [branch name]

    used to switch from one branch to another.

    creates a new branch and also switches to it.

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *