Top Git interview Questions for Experienced Developer

1. How do you create a new repository in GitHub?

A GitHub repository is a centralized location to store, manage, and track changes to your project files. It supports collaboration and version control.

  1. Log in to your GitHub account.
  2. Click the “+” icon in the top-right corner and select “New repository”.
  3. Enter a repository name and optional description.
  4. Choose visibility: Public or Private.
  5. Click “Create repository”.

2. How do you clone a repository to your local machine?

Cloning downloads a repository from GitHub to your local system, enabling offline development.

git clone https://github.com/username/repository.git

3. How do you create a new branch in Git?

Branches let you isolate development work without affecting the main codebase.


git branch feature-branch
git checkout feature-branch
  

4. How do you merge a branch into the main branch?

Merging integrates changes from one branch into another, typically from a feature branch into main.


git checkout main
git merge feature-branch
  

5. How do you resolve merge conflicts in Git?

Conflicts occur when changes in different branches overlap. Git marks the conflicting sections in affected files.

  1. Edit the conflicting files manually.
  2. Mark resolved sections.
  3. Stage the changes: git add filename
  4. Commit the resolution: git commit

6. How do you push changes to a remote repository?

Pushing sends your local commits to the remote repository on GitHub.

git push origin branch-name

7. How do you pull the latest changes from a remote repository?

Pulling fetches and merges changes from the remote repository into your current branch.

git pull origin branch-name

8. How do you create a pull request in GitHub?

Pull requests propose changes and allow for code review before merging.

  1. Push your branch to GitHub.
  2. Navigate to the repository.
  3. Click “Pull Requests” > “New Pull Request”.
  4. Select the base and compare branches.
  5. Review changes and click “Create Pull Request”.

9. How do you revert a commit in Git?

Reverting creates a new commit that undoes the changes of a previous commit without altering