Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Sunday, 27 August 2023

Git Commands

Question: Configure Git

  Answer:  git config --global user.name "Your Name"

   git config --global user.email "your.email@example.com"


Question: Create a new Git repository

  Answer:  git init


Question: Clone a remote repository to your local machine

  Answer:  git clone <repository_url>


Question: Check the status of your working directory and staging area

Answer:  git status



Question: Add changes to the staging area

Answer:  git add <file_name>  # To stage a specific file

   git add .            # To stage all changes



Question: Commit changes to the repository with a descriptive message

Answer: git commit -m "Your commit message here"



Question: Push changes to a remote repository

Answer: 

   git push origin <branch_name>



Question: Pull changes from a remote repository

Answer: 

   git pull origin <branch_name>


Question: Create a new branch

Answer: 

   git branch <branch_name>


Question: Switch to an existing branch

 Answer: 

    git checkout <branch_name>

 

Question: Create a new branch and switch to it in one command

 Answer: 

    git checkout -b <branch_name>

 

Question: Merge changes from one branch to another

 Answer: 

    git merge <source_branch>

 

Question: View the commit history

 Answer: 

    git log

 

Question: View the difference between the working directory and the last commit

 Answer: 

    git diff

 


Question: View the difference between two branches

 Answer: 

    git diff <branch_name> <another_branch_name>

 


Question: Discard changes in the working directory

Answer:  

    git checkout -- <file_name>

 


Question: Discard changes in the staging area and working directory

 Answer: 

    git reset --hard HEAD

 


Question: Undo the last commit and move changes to the staging area

 Answer: 

    git reset --soft HEAD~1

 


Question: Create and apply a stash to save and temporarily remove changes

 Answer: 

    git stash save "Your stash message"

    git stash apply

 


Question: Fetch changes from a remote repository without merging

 Answer: 

    git fetch

 


Question: Rename a file and stage the change

 Answer: 

    git mv <old_file_name> <new_file_name>

 


Question: Delete a branch

Answer:  

    git branch -d <branch_name>

 


Question: Show the remote repositories

 Answer: 

    git remote -v

 


Question: Add a remote repository

 Answer: 

    git remote add <remote_name> <repository_url>

 


Question: Change the remote repository URL

 Answer: 

    git remote set-url <remote_name> <new_repository_url>

 


Question: Push to a specific branch on the remote repository

 Answer: 

    git push <remote_name> <local_branch_name>:<remote_branch_name>

 


Question: Pull changes from a remote repository and automatically merge

 Answer: 

    git pull

 


Question: Create an annotated tag for a specific commit

  Answer: 

    git tag -a <tag_name> -m "Tag message" <commit_sha>

 


Question: List all tags in the repository

  Answer: 

    git tag

 


Question: Show the details of a specific tag

 Answer:  

    git show <tag_name>

 


Question: Revert changes from a specific commit

 Answer:  

    git revert <commit_sha>

 


Question: Change the last commit message

 Answer:  

    git commit --amend -m "New commit message"

 


Question: Move the HEAD to a specific commit

 Answer:  

    git reset <commit_sha>

 


Question: Create a new branch from a specific commit

 Answer:  

    git branch <new_branch_name> <commit_sha>

 


Question: List all branches (local and remote)

 Answer:  

    git branch -a

 


Question: Show the difference between two commits

 Answer:  

    git diff <commit_sha_1> <commit_sha_2>

 


Question: Show a summary of the changes introduced by a commit

 Answer:  

    git show <commit_sha>

 


Question: Move changes from the working directory to the staging area

 Answer:  

    git add -u

 


Question: Delete a file from the working directory and stage the change

 Answer:  

    git rm <file_name>

 


Question: Create an empty commit (useful for triggering hooks)

 Answer:  

    git commit --allow-empty -m "Empty commit"

 


Question: Show the configuration settings

 Answer:  

    git config --list

 


Question: Edit the Git configuration file

 Answer: 

    git config --global --edit

 


Question: Show the Git version

  Answer: 

    git --version

 


Question: Show the working tree status in short format

  Answer: 

    git status -s

 


Question: Show the branch name you are currently on

  Answer: 

    git rev-parse --abbrev-ref HEAD

 


Question: Show the changes made in the last commit

  Answer: 

    git show HEAD

 


Question: Rename a branch

  Answer: 

    git branch -m <old_branch_name> <new_branch_name>

 


Question: Remove files from the staging area but keep the changes in the working directory

  Answer: 

    git reset <file_name>

 


Question: Remove files from the staging area and the working directory

  Answer: 

    git rm --cached <file_name>

 


Question: Undo the last commit and keep the changes in the working directory

  Answer: 

    git reset HEAD~1


Question: How to Resolve Merge Conflicts in Git

 Answer: 


Step 1: The easiest way to resolve a conflicted file is to open it and make any necessary changes.


Step 2: After editing the file, we can use the git add a command to stage the new merged content.


Step 3: The final step is to create a new commit with the help of the git commit command.


Step 4: Git will create a new merge commit to finalize the merge.


Git Commands to Resolve Conflicts

1. git log --merge 

The git log --merge command helps to produce the list of commits that are causing the conflict.


2. git diff 

The git diff command helps to identify the differences between the states repositories or files.


3. git checkout 

The git checkout command is used to undo the changes made to the file, or for changing branches.


4. git reset --mixed 

The git reset --mixed command is used to undo changes to the working directory and staging area.


5. git merge --abort

The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began.


6. git reset

The git reset command is used at the time of merge conflict to reset the conflicted files to their original state.

 


Git MCQ

Question : What is Git?

A version control system

A programming language

An operating system

A web browser

Correct Answer :  A version control system 

 

Question : How do you create a new Git repository?

`git init`

`git create`

`git new`

`git repo init`

Correct Answer :  `git init` 

 

Question : How do you clone an existing Git repository?

`git clone <repository_url>`

`git pull <repository_url>`

`git checkout <repository_url>`

`git fetch <repository_url>`

Correct Answer :  `git clone <repository_url>` 

 

Question : How do you stage changes in Git?

`git add .`

`git commit -m 'message'`

`git stage .`

`git push`

Correct Answer :  `git add .` 

 

Question : How do you commit changes in Git?

`git commit -m 'message'`

`git add .`

`git push`

`git commit -a -m 'message'`

Correct Answer :  `git commit -m 'message'` 

 

Question : How do you check the status of your Git repository?

`git status`

`git info`

`git check`

`git show`

Correct Answer :  `git status` 

 

Question : How do you create a new branch in Git?

`git new-branch <branch_name>`

`git create-branch <branch_name>`

`git branch <branch_name>`

`git checkout -b <branch_name>`

Correct Answer :  `git checkout -b <branch_name>` 

 

Question : How do you switch to a different branch in Git?

`git switch <branch_name>`

`git checkout <branch_name>`

`git change-branch <branch_name>`

`git move-to <branch_name>`

Correct Answer :  `git checkout <branch_name>` 

 

Question : How do you merge branches in Git?

`git merge <branch_name>`

`git branch <branch_name>`

`git merge-branch <branch_name>`

`git combine <branch_name>`

Correct Answer :  `git merge <branch_name>` 

 

Question : How do you resolve conflicts in Git?

Manually edit the conflicted files

`git resolve`

`git conflict`

`git fix`

Correct Answer :  Manually edit the conflicted files 

 

Question : How do you view the commit history in Git?

`git log`

`git history`

`git show`

`git commits`

Correct Answer :  `git log` 

 

Question : How do you discard changes in a file and revert to the last commit version in Git?

`git discard <file>`

`git revert <file>`

`git reset <file>`

`git remove <file>`

Correct Answer :  `git reset <file>` 

 

Question : How do you remove a file from a Git repository?

`git remove <file>`

`git delete <file>`

`git rm <file>`

`git erase <file>`

Correct Answer :  `git rm <file>` 

 

Question : How do you rename a file in Git?

`git rename <old_name> <new_name>`

`git mv <old_name> <new_name>`

`git move <old_name> <new_name>`

`git rename-file <old_name> <new_name>`

Correct Answer :  `git mv <old_name> <new_name>` 

 

Question : How do you view the changes made in a specific commit in Git?

`git changes <commit_id>`

`git show <commit_id>`

`git diff <commit_id>`

`git view <commit_id>`

Correct Answer :  `git show <commit_id>` 

 

Question : How do you remove untracked files in Git?

`git remove-untracked`

`git clean`

`git clear`

`git remove --untracked`

Correct Answer :  `git clean` 

 

Question : How do you undo the last commit in Git?

`git undo`

`git revert HEAD`

`git reset --hard HEAD`

`git commit --undo`

Correct Answer :  `git reset --hard HEAD` 

 

Question : How do you create and apply a Git patch?

`git create-patch` and `git apply-patch`

`git patch-create` and `git patch-apply`

`git diff > patch` and `git apply patch`

`git create-diff` and `git apply-diff`

Correct Answer :  `git diff > patch` and `git apply patch` 

 

Question : How do you discard changes in all files and revert to the last commit version in Git?

`git discard-all`

`git reset --hard HEAD`

`git revert-all`

`git remove-all`

Correct Answer :  `git reset --hard HEAD` 

 

Question : How do you remove a branch in Git?

`git remove-branch <branch_name>`

`git branch -d <branch_name>`

`git delete <branch_name>`

`git rm <branch_name>`

Correct Answer :  `git branch -d <branch_name>` 

 

Question : How do you rename a branch in Git?

`git rename-branch <old_name> <new_name>`

`git mv-branch <old_name> <new_name>`

`git branch -m <old_name> <new_name>`

`git move-branch <old_name> <new_name>`

Correct Answer :  `git branch -m <old_name> <new_name>` 

 

Question : How do you view the changes made between two commits in Git?

`git diff <commit1> <commit2>`

`git changes <commit1>..<commit2>`

`git show <commit1>..<commit2>`

`git diff <commit1>..<commit2>`

Correct Answer :  `git diff <commit1> <commit2>` 

 

Question : How do you create an empty commit in Git?

`git commit --empty`

`git commit -e`

`git commit -a`

`git commit --allow-empty`

Correct Answer :  `git commit --allow-empty` 

 

Question : How do you remove a remote branch in Git?

`git remote remove <branch_name>`

`git remove remote <branch_name>`

`git remote delete <branch_name>`

`git push --delete origin <branch_name>`

Correct Answer :  `git push --delete origin <branch_name>` 

 

Question : How do you update your local repository with changes from the remote repository in Git?

`git fetch` followed by `git merge`

`git pull`

`git sync`

`git update`

Correct Answer :  `git pull` 

 

Question : How do you view the list of remote repositories in Git?

`git list-remotes`

`git remote show`

`git remote list`

`git remote -v`

Correct Answer :  `git remote -v` 

 

Question : How do you push a new branch to a remote repository in Git?

`git push <remote> <branch>`

`git push --branch <branch>`

`git push origin <branch>`

`git push -b <branch>`

Correct Answer :  `git push <remote> <branch>` 

 

Question : How do you configure your name and email in Git?

`git set-name <name>` and `git set-email <email>`

`git config user.name <name>` and `git config user.email <email>`

`git name <name>` and `git email <email>`

`git config --name <name>` and `git config --email <email>`

Correct Answer :  `git config user.name <name>` and `git config user.email <email>` 

 

Question : How do you create an annotated tag in Git?

`git tag <tag_name>`

`git create-tag <tag_name>`

`git annotate-tag <tag_name>`

`git tag -a <tag_name>`

Correct Answer :  `git tag -a <tag_name>` 

 

Question : How do you view the list of tags in Git?

`git list-tags`

`git show-tags`

`git tag`

`git tag list`

Correct Answer :  `git tag`

Featured

Software Testing

Question: What is software testing, and why is it important?  Answer: Software testing is the process of evaluating a software application ...

popular