Saturday 9 November 2019

Git interview questions and answers - Problem and Solutions


Git interview questions and answers - Problem and Solutions

Question: How to delete a Git branch both locally and remotely?
To remove a local branch from your local system.
git branch -d the_local_branch

To remove a remote branch from the server.
git push origin :the_remote_branch



Question: How do you undo the last commit?
git revert commit-id



Question: How to Edit an incorrect commit message in Git?
git commit --amend -m "This is your new git message"



Question: What are the differences between 'git pull' and 'git fetch'?
Git pull automatically merges the commits without letting you review them first.
Git fetch stores them in your local repository but it not merge them with your current branch.
git fetch similar to guit pull but it does not merge the changes.


Question: How do you rename the local branch?
git branch -m oldBranchName newBranchName



Question: How do I remove local files (Not in Repo) from my current Git branch?
git clean -f -n


Question: How to Checkout remote Git branch?
git checkout test



Question: How do I remove a Git submodule?
git rm the_submodule
rm -rf .git/modules/the_submodule



Question: How do you create a remote Git branch?
git checkout -b your_branch_name
git push -u origin your_branch_name



Question: How to Change the URL for a remote Git repository?
git remote set-url origin git://this.is.new.url



Question: How to Change the author of a commit in Git?
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='NewAuthorName'; GIT_AUTHOR_EMAIL='authoremail@gmail.com'; GIT_COMMITTER_NAME='CommiterName'; GIT_COMMITTER_EMAIL='committergmail@gmail.com';" HEAD



Question: What is .gitignore?
.gitignore tells git which files/folder should be ignore.
Create a file with name of .gitignore in root.
temporay files, system files, hidden files or local downloaded modules we can add in .gitignore file, so that git will not added this.


Question: How do I delete a Git branch locally?
git branch -d {the_local_branch}


Question: How do I delete a Git branch remotely?
git push origin --delete {the_remote_branch}
(use -D instead to force deleting the branch without checking merged status)


Question: What is the difference between git pull and git fetch?
git pull does a git fetch followed by a git merge.



Question: How do I undo git add before commit?
git reset file.php



Question: How do I rename a local Git branch?
git branch -m oldname newname




Question: How do I discard unstaged changes in Git?
git stash save --keep-index --include-untracked

You don't need to include --include-untracked if you don't want to be thorough about it.
After that, you can drop that stash with a git stash drop command if you like.