Top 21 GIT Interview Questions and Answers for SDET - DevOps - Automation QA


How to Switch to previous branch:

Ans: git switch @{-1}

How to create repository in bitbucket and push code for automation project?
Ans: Below are the sequence of commands to use:

First go to your bitbucket path and get access to create repository.
Create a repository with a self defined name, let name is "Automaters"
Then open cmd in the laptop where you have kept your automation project
cd existing-project
git init
git add --all
git commit -m "InitialCommit"
git remote add origin "https://path....Automaters.git"
git push -u origin master

If you get SSL certificate error to push then use below:

git config --global http.sslVerify false

How to Push a local branch for the first time:
Ans: git push --set-upstream origin <branch>

How to Push a local branch to a different remote branch:
Ans: git push origin <local_branch>:<remote_branch>

How to undo commits?
AnsThe following command will undo your most recent commit and put those changes back into staging, so you don't lose any work:
$ git reset --soft HEAD~1
The next one will completely delete the commit and throw away any changes. Be absolutely sure this is what you want:
$ git reset --hard HEAD~1

Fetch changes from both origin and upstream in the same shot:
Ans: git fetch --multiple origin upstream

How to Delete a remote branch on origin:
Ans: git push origin : <remote_branch>

Difference between rebase and merge?
Ansgit merge apply all unique commits from branch A into branch B in one commit 
 git rebase gets all unique commits from both branches and applies them one by one.
 git rebase rewrites commit history but doesn't create extra commit for merging.

Unstash those changes and bring them back into your working directory
Ans: git stash pop

Difference between soft, mixed, and hard resets?
Ans: --soft: Uncommit changes but leave those changes staged
--mixed (the default): Uncommit and unstage changes, but changes are left in the working directory
--hard: Uncommit, unstage, and delete changes

Listing the existing tags in Git is straightforward
Ans: $ git tag

How to create tag?
Ans: git tag <tagname>

Command to delete the file from your working directory and stages the deletion?
Ans: git rm [fileName]

Unstash your changes without popping them off the stack.
Ans: git stash apply

How to know about the history?
git log
To know more about git log : https://git-scm.com/docs/git-log

How to know all the details of changes to a commit?
git show <commitID>
To know more about git show : https://git-scm.com/docs/git-show
 
How to know the last commit details and see a log where each commit is one line?
To know the last commit details : git log -1
see log each commit in one line: git log --pretty=oneline

What git add and git commit command does
Git add--> add the changes to index area
Git commit--> add changes to local repo

How to compare two branch in GIT?
git diff branch1...branch2

How to view current state and any merge conflits?
git status
    The git status command displays the state of the working directory and the staging area.

How to undo last commit and rewrite history?
git reset --hard HEAD~1
 
Difference between soft, mixed, and hard resets?
--soft: Uncommit changes but leave those changes staged
--mixed (the default): Uncommit and unstage changes, but changes are left in the working directory
--hard: Uncommit, unstage, and delete changes

How to reset a file back to how it was before changes?
git restore <filename>     # new syntax (as of Git 2.23)
git checkout -- <filename> # old syntax

How to unstage changes or restore files?
Maybe you accidentally staged some files which we don't want to commit.
git restore test.js
git restore .
Git Official Docs