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 commandgit 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 commandgit add <filename>
to add individual files orgit add .
to add all files in the current directory. Once the files are added, use the commandgit commit
to save the changes.How do you commit changes to a Git repository?
Answer: To commit changes to a Git repository, use the commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit add <filename>
to stage the changes, and then use the commandgit commit
to save the changes.How do you revert a commit in Git?
Answer: To revert a commit in Git, use the commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit stash apply
. This will apply the most recent stash. To apply a specific stash, use the commandgit stash apply <stashid>
.How do you create a new tag in Git?
Answer: To create a new tag in Git, use the commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit add <filename>
to stage the changes. Once all conflicts are resolved and changes are staged, you can commit the changes usinggit commit
.How do you view the commit history in Git?
Answer: To view the commit history in Git, use the commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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 commandgit 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