Git:
Git is a distributed version control system designed to track changes in source code during software development. It was created by Linus Torvalds in 2005 and has since become one of the most widely used version control systems
Git-Hub:
GitHub is a web-based platform that uses Git for version control. It adds a collaborative layer on top of Git, providing additional features for teams and individuals collaborating on software development projects.
Git Commands
Configuring Git
- git config --global user.name "Your Name" - Set your name globally.
- git config --global user.email "your.email@example.com" - Set your email globally.
- git config --list - List all Git configurations.
Creating Repositories
- git init - Initialize a new Git repository.
- git clone <repository-url> - Clone a remote repository to your local machine.
Making Changes
- git status - Show the status of changes as untracked, modified, or staged.
- git add <file> - Add a file or changes to the staging area.
- git commit -m "Commit message" - Commit changes with a descriptive message.
Branching
- git branch - List all branches in the repository.
- git branch <branch-name> - Create a new branch.
- git checkout <branch-name> - Switch to an existing branch.
- git merge <branch-name> - Merge changes from one branch into the current branch.
- git branch -d <branch-name> - Delete a branch.
Remote Repositories
- git remote - List remote repositories.
- git remote add <remote-name> <repository-url> - Add a remote repository.
- git push <remote-name> <branch-name> - Push changes to a remote repository.
- git pull <remote-name> <branch-name> - Fetch and merge changes from a remote repository.
Resolving Conflicts
- git fetch - Fetch changes from a remote repository.
- git merge origin/<branch-name> - Merge fetched changes into the current branch.
- git diff - Show the differences between working directory, staging area, and the last commit.
Undoing Changes
- git reset <file> - Unstage changes.
- git checkout -- <file> - Discard changes in the working directory.
- git revert <commit> - Create a new commit that undoes changes made in a previous commit.
Viewing History
- git log - Display commit history.
- git log --oneline - Display compact commit history.
- git blame <file> - Show who last modified each line of a file.
Git Interview Questions:
- What is Git?
- Answer: Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to collaborate on projects efficiently.
- What is the difference between Git and SVN?
- Answer: Git is a distributed version control system, whereas SVN (Subversion) is centralized. Git allows each developer to have a complete copy of the repository, enabling offline work and faster branching.
- Explain the basic Git workflow.
- Answer: The basic Git workflow involves cloning a repository, making changes in a local branch, committing those changes, pulling the latest changes from the remote repository, resolving conflicts if any, and pushing changes back to the remote repository.
- What is a Git repository?
- Answer: A Git repository is a storage location where a project's version-controlled files and history are kept. It can be local (on your machine) or remote (on a server).
- How do you create a new Git repository?
- Answer: To create a new Git repository, you can use the git init command in the project's root directory.
- Explain the difference between 'git pull' and 'git fetch.'
- Answer: git pull fetches changes from a remote repository and merges them into the current branch, while git fetch only fetches changes but does not automatically merge them. You need to use additional commands (e.g., git merge or git rebase) after git fetch.
- What is the purpose of the .gitignore file?
- Answer: The .gitignore file specifies files or patterns that Git should ignore when tracking changes. It helps avoid unnecessary files, such as compiled binaries or temporary files, from being included in the version control system.
- How do you create a new branch in Git?
- Answer: To create a new branch, you can use the git branch <branch_name> command followed by git checkout <branch_name> or git switch <branch_name> to switch to the new branch.
- What is Git commit?
- Answer: A Git commit represents a snapshot of changes made to the repository. It includes a unique identifier (SHA-1 hash), author information, timestamp, and the changes made.
- How do you undo the last Git commit?
- Answer: You can use the git reset command to undo the last commit. For example, git reset HEAD~1 will remove the last commit from the branch while keeping the changes locally.
- What is a Git merge?
- Answer: Git merge combines changes from different branches. It takes the contents of a source branch and integrates them into a target branch.
- Explain the difference between 'git merge' and 'git rebase.'
- Answer: Both git merge and git rebase integrate changes from one branch into another, but git rebase presents a cleaner history by placing the entire branch on top of the target branch, eliminating unnecessary merge commits.
- What is a Git conflict, and how do you resolve it?
- Answer: A Git conflict occurs when two branches have changes in the same part of a file. To resolve conflicts, you need to manually edit the conflicting files, mark them as resolved with git add, and then complete the merge or rebase with git merge or git rebase --continue.
- How do you delete a branch in Git?
- Answer: You can delete a branch using the git branch -d <branch_name> command. Use -D instead of -d to force deletion, even if changes are not merged.
- What is Git cherry-picking?
- Answer: Git cherry-picking is the process of selecting and applying specific commits from one branch to another. It allows you to choose individual changes without merging the entire branch.
- Explain Git submodule.
- Answer: A Git submodule is a reference to another Git repository inside a parent repository. It allows you to include external repositories as dependencies in your project.
- How do you view the Git commit history?
- Answer: You can view the Git commit history using the git log command. Adding --graph and --oneline options provides a more compact and graphical representation.
- What is Git stash, and when would you use it?
- Answer: Git stash allows you to temporarily save changes that are not ready to be committed. This is useful when you need to switch branches or perform other tasks without committing to your current changes.
- Explain the concept of Git remote.
- Answer: Git remote is a reference to a remote repository. It allows you to interact with repositories on servers, such as pushing or pulling changes.
- How do you tag a specific commit in Git?
- Answer: You can tag a specific commit using the git tag command. For example,
- git tag v1.0 <commit_hash> tags the specified commit with the name "v1.0."
- What is Git rebase interactive mode?
- Answer: Git rebase interactive mode (git rebase -i) allows you to interactively choose, edit, squash, or drop individual commits during the rebase process.
- How does Git handle branching in a distributed version control system?
- Answer: In Git, branches are lightweight and easy to create. Each developer can have their own local branches, and branches can be pushed and pulled between repositories.
- What is Git bisect, and how is it used?
- Answer: Git bisect is a command used for binary search to find a specific commit that introduced a bug. It helps in identifying the commit where the bug was introduced.
- How can you recover a deleted branch in Git?
- Answer: If a branch is deleted, you can recover it using the reflog with the command “git reflog <branch_name>”. Find the commit hash before deletion and recreate the branch using git branch <branch_name> <commit_hash>.
- Explain the concept of Git hooks.
- Answer: Git hooks are scripts that run automatically at key points in the Git workflow, such as pre-commit or post-receive. They allow you to customize and automate actions in response to specific events.
Git:
Git is a distributed version control system designed to track changes in source code during software development. It was created by Linus Torvalds in 2005 and has since become one of the most widely used version control systems
Git-Hub:
GitHub is a web-based platform that uses Git for version control. It adds a collaborative layer on top of Git, providing additional features for teams and individuals collaborating on software development projects.
Git Commands
Configuring Git
- git config --global user.name "Your Name" - Set your name globally.
- git config --global user.email "your.email@example.com" - Set your email globally.
- git config --list - List all Git configurations.
Creating Repositories
- git init - Initialize a new Git repository.
- git clone <repository-url> - Clone a remote repository to your local machine.
Making Changes
- git status - Show the status of changes as untracked, modified, or staged.
- git add <file> - Add a file or changes to the staging area.
- git commit -m "Commit message" - Commit changes with a descriptive message.
Branching
- git branch - List all branches in the repository.
- git branch <branch-name> - Create a new branch.
- git checkout <branch-name> - Switch to an existing branch.
- git merge <branch-name> - Merge changes from one branch into the current branch.
- git branch -d <branch-name> - Delete a branch.
Remote Repositories
- git remote - List remote repositories.
- git remote add <remote-name> <repository-url> - Add a remote repository.
- git push <remote-name> <branch-name> - Push changes to a remote repository.
- git pull <remote-name> <branch-name> - Fetch and merge changes from a remote repository.
Resolving Conflicts
- git fetch - Fetch changes from a remote repository.
- git merge origin/<branch-name> - Merge fetched changes into the current branch.
- git diff - Show the differences between working directory, staging area, and the last commit.
Undoing Changes
- git reset <file> - Unstage changes.
- git checkout -- <file> - Discard changes in the working directory.
- git revert <commit> - Create a new commit that undoes changes made in a previous commit.
Viewing History
- git log - Display commit history.
- git log --oneline - Display compact commit history.
- git blame <file> - Show who last modified each line of a file.
Git Interview Questions:
- What is Git?
- Answer: Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to collaborate on projects efficiently.
- What is the difference between Git and SVN?
- Answer: Git is a distributed version control system, whereas SVN (Subversion) is centralized. Git allows each developer to have a complete copy of the repository, enabling offline work and faster branching.
- Explain the basic Git workflow.
- Answer: The basic Git workflow involves cloning a repository, making changes in a local branch, committing those changes, pulling the latest changes from the remote repository, resolving conflicts if any, and pushing changes back to the remote repository.
- What is a Git repository?
- Answer: A Git repository is a storage location where a project's version-controlled files and history are kept. It can be local (on your machine) or remote (on a server).
- How do you create a new Git repository?
- Answer: To create a new Git repository, you can use the git init command in the project's root directory.
- Explain the difference between 'git pull' and 'git fetch.'
- Answer: git pull fetches changes from a remote repository and merges them into the current branch, while git fetch only fetches changes but does not automatically merge them. You need to use additional commands (e.g., git merge or git rebase) after git fetch.
- What is the purpose of the .gitignore file?
- Answer: The .gitignore file specifies files or patterns that Git should ignore when tracking changes. It helps avoid unnecessary files, such as compiled binaries or temporary files, from being included in the version control system.
- How do you create a new branch in Git?
- Answer: To create a new branch, you can use the git branch <branch_name> command followed by git checkout <branch_name> or git switch <branch_name> to switch to the new branch.
- What is Git commit?
- Answer: A Git commit represents a snapshot of changes made to the repository. It includes a unique identifier (SHA-1 hash), author information, timestamp, and the changes made.
- How do you undo the last Git commit?
- Answer: You can use the git reset command to undo the last commit. For example, git reset HEAD~1 will remove the last commit from the branch while keeping the changes locally.
- What is a Git merge?
- Answer: Git merge combines changes from different branches. It takes the contents of a source branch and integrates them into a target branch.
- Explain the difference between 'git merge' and 'git rebase.'
- Answer: Both git merge and git rebase integrate changes from one branch into another, but git rebase presents a cleaner history by placing the entire branch on top of the target branch, eliminating unnecessary merge commits.
- What is a Git conflict, and how do you resolve it?
- Answer: A Git conflict occurs when two branches have changes in the same part of a file. To resolve conflicts, you need to manually edit the conflicting files, mark them as resolved with git add, and then complete the merge or rebase with git merge or git rebase --continue.
- How do you delete a branch in Git?
- Answer: You can delete a branch using the git branch -d <branch_name> command. Use -D instead of -d to force deletion, even if changes are not merged.
- What is Git cherry-picking?
- Answer: Git cherry-picking is the process of selecting and applying specific commits from one branch to another. It allows you to choose individual changes without merging the entire branch.
- Explain Git submodule.
- Answer: A Git submodule is a reference to another Git repository inside a parent repository. It allows you to include external repositories as dependencies in your project.
- How do you view the Git commit history?
- Answer: You can view the Git commit history using the git log command. Adding --graph and --oneline options provides a more compact and graphical representation.
- What is Git stash, and when would you use it?
- Answer: Git stash allows you to temporarily save changes that are not ready to be committed. This is useful when you need to switch branches or perform other tasks without committing to your current changes.
- Explain the concept of Git remote.
- Answer: Git remote is a reference to a remote repository. It allows you to interact with repositories on servers, such as pushing or pulling changes.
- How do you tag a specific commit in Git?
- Answer: You can tag a specific commit using the git tag command. For example,
- git tag v1.0 <commit_hash> tags the specified commit with the name "v1.0."
- What is Git rebase interactive mode?
- Answer: Git rebase interactive mode (git rebase -i) allows you to interactively choose, edit, squash, or drop individual commits during the rebase process.
- How does Git handle branching in a distributed version control system?
- Answer: In Git, branches are lightweight and easy to create. Each developer can have their own local branches, and branches can be pushed and pulled between repositories.
- What is Git bisect, and how is it used?
- Answer: Git bisect is a command used for binary search to find a specific commit that introduced a bug. It helps in identifying the commit where the bug was introduced.
- How can you recover a deleted branch in Git?
- Answer: If a branch is deleted, you can recover it using the reflog with the command “git reflog <branch_name>”. Find the commit hash before deletion and recreate the branch using git branch <branch_name> <commit_hash>.
- Explain the concept of Git hooks.
- Answer: Git hooks are scripts that run automatically at key points in the Git workflow, such as pre-commit or post-receive. They allow you to customize and automate actions in response to specific events.
No comments:
Post a Comment