Show List

Git Interview Questions on Commands

  • How do you create a new Git repository?
    Answer: To create a new Git repository, navigate to the directory where you want to create the repository and run the command git init. This will create an empty repository in the current directory.

  • How do you add files to a Git repository?
    Answer: To add files to a Git repository, use the command git add <filename> to add individual files or git add . to add all files in the current directory. Once the files are added, use the command git commit to save the changes.

  • How do you commit changes to a Git repository?
    Answer: To commit changes to a Git repository, use the command git commit -m "Commit message". This will save the changes you made and add a commit message to describe the changes.

  • How do you view the commit history of a Git repository?
    Answer: To view the commit history of a Git repository, use the command git log. This will show a list of all commits, with the most recent commit at the top.

  • How do you create a new branch in Git?
    Answer: To create a new branch in Git, use the command git branch <branchname>. This will create a new branch with the specified name.

  • How do you switch to a different branch in Git?
    Answer: To switch to a different branch in Git, use the command git checkout <branchname>. This will switch to the branch with the specified name.

  • How do you merge two branches in Git?
    Answer: To merge two branches in Git, first switch to the branch you want to merge into, and then use the command git merge <branchname>. This will merge the changes from the specified branch into the current branch.

  • How do you resolve merge conflicts in Git?
    Answer: To resolve merge conflicts in Git, open the files that have conflicts and manually edit them to resolve the conflicts. Once the conflicts are resolved, use the command git add <filename> to stage the changes, and then use the command git commit to save the changes.

  • How do you revert a commit in Git?
    Answer: To revert a commit in Git, use the command git revert <commitid>. This will create a new commit that undoes the changes made in the specified commit.

  • How do you discard changes in Git?
    Answer: To discard changes in Git, use the command git checkout -- <filename>. This will discard any changes made to the specified file since the last commit.

  • How do you rename a file in Git?
    Answer: To rename a file in Git, use the command git mv <oldfilename> <newfilename>. This will rename the file and stage the changes.

  • How do you delete a file in Git?
    Answer: To delete a file in Git, use the command git rm <filename>. This will remove the file from the repository and stage the changes.

  • How do you view the changes made to a file in Git?
    Answer: To view the changes made to a file in Git, use the command git diff <filename>. This will show the changes made to the file since the last commit.

  • How do you view the differences between two branches in Git?
    Answer: To view the differences between two branches in Git, use the command git diff <branchname1>..<branchname2>. This will show the differences between the two branches.

  • How do you apply a stash in Git?
    Answer: To apply a stash in Git, use the command git stash apply. This will apply the most recent stash. To apply a specific stash, use the command git stash apply <stashid>.

  • How do you create a new tag in Git?
    Answer: To create a new tag in Git, use the command git tag <tagname>. This will create a new tag with the specified name at the current commit.

  • How do you push changes to a remote repository in Git?
    Answer: To push changes to a remote repository in Git, use the command git push <remote> <branchname>. This will push the changes in the specified branch to the remote repository.

  • How do you pull changes from a remote repository in Git?
    Answer: To pull changes from a remote repository in Git, use the command git pull <remote> <branchname>. This will fetch the changes from the remote repository and merge them into the current branch.

  • How do you clone a Git repository?
    Answer: To clone a Git repository, use the command git clone <repositoryurl>. This will create a copy of the remote repository on your local machine.

  • How do you create a Git branch and switch to it in one command?
    Answer: To create a Git branch and switch to it in one command, use the command git checkout -b <branchname>. This will create a new branch with the specified name and switch to it.

  • How do you view the differences between two commits in Git?
    Answer: To view the differences between two commits in Git, use the command git diff <commitid1>..<commitid2>. This will show the differences between the two commits.

  • How do you view the files that have been modified in a Git repository?
    Answer: To view the files that have been modified in a Git repository, use the command git status. This will show a list of modified files.

  • How do you revert a file to a previous commit in Git?
    Answer: To revert a file to a previous commit in Git, use the command git checkout <commitid> <filename>. This will replace the contents of the file with the version from the specified commit.

  • How do you undo the most recent commit in Git?
    Answer: To undo the most recent commit in Git, use the command git reset --soft HEAD~1. This will remove the most recent commit, but leave the changes staged.

  • How do you undo the most recent commit and discard the changes in Git?
    Answer: To undo the most recent commit and discard the changes in Git, use the command git reset --hard HEAD~1. This will remove the most recent commit and all changes.

  • How do you rename a branch in Git?
    Answer: To rename a branch in Git, use the command git branch -m <oldbranchname> <newbranchname>. This will rename the branch.

  • How do you view the changes made to a file in a specific commit in Git?
    Answer: To view the changes made to a file in a specific commit in Git, use the command git show <commitid> <filename>. This will show the changes made to the file in the specified commit.

  • How do you list all the branches in a Git repository?
    Answer: To list all the branches in a Git repository, use the command git branch. This will show a list of all the branches.

  • How do you remove a file from a Git repository?
    Answer: To remove a file from a Git repository, use the command git rm <filename>. This will remove the file from the repository and stage the change.

  • How do you discard changes to a file in Git?
    Answer: To discard changes to a file in Git, use the command git checkout -- <filename>. This will discard all changes to the file and restore it to the last committed version.

  • How do you merge a branch into the current branch in Git?
    Answer: To merge a branch into the current branch in Git, use the command git merge <branchname>. This will merge the changes in the specified branch into the current branch.

  • How do you resolve merge conflicts in Git?
    Answer: To resolve merge conflicts in Git, you need to edit the conflicting file to resolve the conflicts, and then use the command git add <filename> to stage the changes. Once all conflicts are resolved and changes are staged, you can commit the changes using git commit.

  • How do you view the commit history in Git?
    Answer: To view the commit history in Git, use the command git log. This will show a list of all commits, with the most recent commits listed first.

  • How do you create an empty Git repository?
    Answer: To create an empty Git repository, use the command git init. This will create a new repository with no initial commit.

  • How do you change the remote URL of a Git repository?
    Answer: To change the remote URL of a Git repository, use the command git remote set-url <remote> <newurl>. This will update the URL of the remote repository.

  • How do you view the contents of a specific commit in Git?
    Answer: To view the contents of a specific commit in Git, use the command git show <commitid>. This will show the details of the commit, including the files that were changed.

  • How do you create a new branch that tracks a remote branch in Git?
    Answer: To create a new branch that tracks a remote branch in Git, use the command git checkout -b <newbranchname> <remotename>/<remotebranchname>. This will create a new branch with the specified name that tracks the specified remote branch.

  • How do you rename a file in Git?
    Answer: To rename a file in Git, use the command git mv <oldfilename> <newfilename>. This will rename the file and stage the change.

  • How do you view the contents of a file in a specific commit in Git?
    Answer: To view the contents of a file in a specific commit in Git, use the command git show <commitid>:<filename>. This will show the contents of the file in the specified commit.

  • How do you create a new Git repository from an existing project?
    Answer: To create a new Git repository from an existing project, use the command git init in the root directory of the project. This will create a new repository with the existing project files.

  • How do you view the differences between the working directory and the index in Git?
    Answer: To view the differences between the working directory and the index in Git, use the command git diff. This will show the differences between the current state of the files in the working directory and the staged changes in the index.


    Leave a Comment


  • captcha text