Get success in GIT interview!

What you will learn

Understand GIT concepts

Learn GIT Interview Questions

Best practices of GIT

Tricky questions on GIT

Gain deep GIT knowledge

Demand higher salary or promotion based on the knowledge gained!

Why take this course?


Get Instant Notification of New Courses on our Telegram channel.


  1. What is GIT?
    GIT is a distributed version control system created by Linus Torvalds, originally designed to handle kernel development for the Linux operating system and other software projects with an emphasis on speed, data integrity, and support for distributed and non-linear workflows.
  2. What is a repository in GIT?
    A repository (often shortened to “repo”) is a storage location under version control that holds an entire directory tree along with its complete history. In Git, every repository can function as both a server and a client, making it a distributed system.
  3. What are the main benefits of GIT?
    • Distributed nature: Each developer has their own copy of the codebase, leading to robustness and reliability because the history of changes is local (and thus backed up) in each repository.
    • Data integrity: Git uses a data model that ensures the cryptographic integrity of historical data. It’s impossible to change past commits without breaking the repository.
    • Branching and merging are very simple in Git, so it’s easier to adopt and explore different approaches or features simultaneously.
    • Speed: Git is designed from the ground up to be fast. The performance of a slow disk is often the limiting factor rather than CPU power.
    • Offline usage: You can work on your code locally and synchronize with a server when you’re ready.
  4. What are the disadvantages of GIT?
    • Learning curve: New users might find Git’s terminology and branching model more complex than other version control systems like Subversion (SVN).
    • Repository size: Because each commit is stored as a complete snapshot, repositories can become quite large over time.
    • Lack of coarse-grained access control mechanisms compared to SVN or centralized systems.
    • Merge conflicts can be challenging to resolve for those new to Git.
  5. What are the main differences between GIT and SVN?
    • Git is distributed, while SVN is centralized.
    • Git allows you to work offline, whereas SVN traditionally does not.
    • Git’s branching and merging are more flexible and powerful, allowing for complex workflows and parallel development without the need for “locking” files.
    • Git has a shallower learning curve once understood, as it is based on content addressing and allows you to see the history of changes in a more straightforward manner.
    • SVN typically uses a single central repository, whereas each clone in Git is a full-fledged repository with a complete copy of the project’s history.
  6. How do you initialize a new GIT repository?
    To create a new Git repository, navigate to your desired directory and run git init. This command initializes a new Git repository in the current working directory.
  7. How to track files with Git?
    To start tracking a file with Git, add it to the staging area using git add filename or git add . to add all new and changed files. After adding, commit the changes using git commit -m "commit message".
  8. How to clone an existing GIT repository?
    Use the git clone command followed by the URL of the remote repository, e.g., git clone https://github.com/username/repository.git.
  9. How to push changes to a remote repository in GIT?
    After committing local changes with git commit, you can use git push origin main (assuming the branch is named ‘main’) to send your commits to the remote repository named ‘origin’.
  10. How to resolve merge conflicts in GIT?
    Resolving merge conflicts involves manually editing the files where conflicts occurred, choosing which changes to keep, and then staging and committing those resolutions using git add and git commit.
  11. How do you revert to a previous commit in GIT?
    Use git checkout <commit-sha> or reset your branch to the previous state with git reset --soft <commit-sha>.
  12. How to view the status of changes in GIT?
    The command git status will show you which files are modified, staged, or untracked.
  13. How to create a new branch in GIT and switch to it?
    Create a new branch with git branch <branchname> and then switch to that branch with git checkout <branchname> or git switch <branchname>. The latter command is an alias introduced in Git version 2.23.
  14. How to delete a branch in GIT after merging it into the main branch?
    After ensuring all changes have been merged into your main branch, you can delete a branch with git branch -d <branchname> or use git delete for remote branches.
  15. What are hooks in GIT and how do you run a script on commit?
    Git hooks are scripts that run before or after events such as commit (pre-commit, post-commit) and push (pre-push, post-push). To run a script on commit, place it in the hooks directory of your repository.
  16. How to determine if changes are staged, committed, or neither in GIT?
    Use git diff to compare staged changes with the last commit, and use git log --follow <file> to trace the history of a file including renames.
  17. What are some common Git commands and their uses?
  • git clone: Creates a copy of a repository.
  • git add: Stages changes for the next commit.
  • git commit: Records changes to the repository.
  • git push: Updates remote repository with new commits.
  • git pull: Fetches and merges changes from a remote repository.
  • git fetch: Downloads all new commits from the remote repository but doesn’t merge them into your current branch.
  • git branch: Lists all local branches in the currently active repository.
  • git checkout -b: Creates a new branch and switches to it.
  • git merge: Joins two lines of development by recording changes from one branch into another.
  • git rebase: Reapply commits on top of another base tip. Useful for cleaning up history.
  1. How do you handle large files in GIT?
    Large files can be handled by either excluding them from version control using .gitignore, or by filtering them out with git attribute and rewriting the history with git filter-branch.
  2. What is the difference between git merge and git rebase?
    git merge combines a series of commits from one branch into another, creating a new commit representing their difference. git rebase takes existing commits and applies them on top of another base commit, resulting in a cleaner project history without merge commits.
  3. How do you handle binary files or large blobs in GIT efficiently?
    You can use Git Large File Storage (LFS) to handle binary files that are too big to store in the repository proper. This is done using git lfs commands.
  4. What is a detached HEAD state in GIT and how do you avoid it?
    A detached HEAD occurs when you checkout an commit that’s not the beginning of a branch. To avoid this, either create or switch to an existing branch before checking out. You can return to a branch with git checkout <branch-name>.
  5. How do you handle conflicts during a git pull or rebase in GIT?
    During a rebase (git rebase) or pulling from a remote (git pull), if conflicts occur, Git will pause and allow you to resolve the conflicts manually by editing the conflicting files, then staging the resolved changes with git add, and finally continuing the rebase (or merging in the case of a pull) with git rebase --continue or git merge --continue.
  6. What is the difference between --track and --no-track options in git clone?
    The --track option creates remote-tracking references corresponding to each local branch, while --no-track disables this behavior, making the cloned branches non-fast-forward only if they have different tips.
  7. How do you view the commit history in GIT?
    Use git log to see the commit history, and git log --oneline for a concise list of commits. Use --graph to include a diffusion graph (dot-graphviz format) before the commit lists and --decorate or --oneline to show branch and tag information.
  8. How to check if your local repository is up-to-date with the remote repository in GIT?
    Use git fetch --dash (or git status -uall) to see all branches that have changes pushed to the remote repository but not yet pulled into your local repository.
  9. How to create a new commit on top of the latest commit in a branch in GIT?
    After staging your changes with git add, create a new commit with git commit --amend if you want to modify the last commit, or use git commit followed by git rebase -i HEAD~n where n is the number of commits you want to interactively rebase.
  10. How to cherry-pick a commit from one branch and apply it to another in GIT?
    Use git cherry-pick <commit-sha> after navigating to the target branch where you want to apply the commit.
  11. What is a Git rebase vs. Bash diff approach for cleaning up history?
    Rebasing rewrites the project history by moving commits to a new base commit. A Bash diff approach involves manually editing the patch files produced by git diff and then resolving them with git apply. Rebasing is generally more convenient for most use cases.
  12. How do you manage different development environments (e.g., develop, release, hotfix) in GIT?
    Use branching strategies like Gitflow or feature branch workflows to manage different development environments. Each type of work has its own branch prefix and lifecycle defined within the strategy.
  13. How do you undo a commit in GIT?
    You can use git reset to undo commits: git reset HEAD~n (where n is the number of commits to undo) will unstage those commits, and git reset --soft HEAD~n will uncommit them without altering the staged changes. A --hard option will both uncommit and unstage the changes, reverting your files as well.
  14. How do you resolve a merge conflict that occurred during a git pull or rebase in GIT?
    After pulling or rebasing, if conflicts occur, manually edit the conflicting files to resolve the issues. Then stage the resolved changes with git add, and continue the rebase (or finalize the pull) using the appropriate command (git rebase --continue or git merge --continue).
  15. What is a Git stash and how do you use it?
    A stash is a way to save all modifications on your current branch and check them out in a detached HEAD state. You can stash your changes with git stash, apply them back with git stash pop, and reapply them if you’ve since made additional changes with git stash apply.
  16. How to work on a feature branch and then merge it into the main branch in GIT?
    After completing work on your feature branch, push your changes to the remote repository, and then merge your branch into the main branch using git checkout main, git pull, then git merge feature-branch (assuming “feature-branch” is the name of your feature branch).
  17. How do you rename a branch in GIT?
    You can’t directly rename a remote branch, but you can create and push a new branch with the desired name and then delete the old branch. Use git branch -m old-name new-name for a local branch and git push origin -u new-name to set the upstream for the newly named branch, then push your changes and delete the old branch.
  18. What are Git hooks and how do you use them?
    Git hooks are scripts that run before or after events, such as commit (pre-commit and post-commit) or push (pre-push and post-push), in a Git repository. They are used to enforce certain conditions before an operation occurs or to perform additional tasks afterward.
  19. How do you handle submodules in GIT?
    Submodules allow you to include other Git repositories within your project and track changes in those submodules. Initialize a submodule with git submodule add [url] path, update a submodule with git submodule update --init [url] path, and commit the submodule reference with a git commit (the submodule’s state will be captured by its HEAD commit).
  20. How to handle large binaries or files in GIT effectively?
    Use Git LFS (Large File Storage) to handle binary files that are too big to store in the repository proper. Configure LFS with git lfs init and track files with git lfs track "path/to/file" or git lfs track "glob pattern", then commit the file as a placeholder commit. Push your changes, and LFS will handle the actual file data on Git Hub or other Git hosting services.
  21. What is the difference between git diff and git log -p?
    git diff shows the differences between two sets of files or commits in the working directory, staging area (index), or commit history, while git log -p shows an incremental diff between each committed version of a file.
  22. How do you view the differences between branches in GIT?
    Use git diff <branch1> <branch2> to view the differences between two branches in your local repository. To compare with a remote branch, use git diff origin/<branch2> <branch1>.
  23. How do you handle renaming files or directories in GIT?
    Rename files or directories locally and commit the change as usual. If you want to rename a file remotely (e.g., after a Git LFS migration), you’ll need to add a renaming mapping in the .gitattributes file, then use git lfs migrate --renames and commit the new mappings.
  24. How to keep a GIT repository in sync with a remote repository?
    Regularly pull changes from the remote repository (git pull origin main) and push your local commits (git push origin main). Ensure that you handle merges or rebases as necessary to integrate changes smoothly.
  25. What are some best practices for maintaining a GIT repository?
    Keep your repository clean by regularly pulling from and pushing to the remote repository, resolving conflicts promptly, using meaningful commit messages, keeping an organized branching strategy (e.g., Gitflow), enforcing code reviews before merging, and backing up your repository regularly.
  26. How do you reset your local repository to match a remote branch?
    You can reset your local branch to match the state of the remote branch by first fetching the latest changes (git fetch origin), then resetting your branch (git reset --hard origin/<branch>) or re-setting and merging changes (git reset --keep origin/<branch> followed by git merge origin/<branch>).
  27. What are Git reflogs and how do you use them?
    Git maintains a log of every object it accesses, which can be used to recover lost commits or file a bug with a reproducible example. Use git reflog to list the reflogs, and git reset --hard [reflog] to restore a specific state from the reflog.
  28. How do you handle a dead branch in GIT that has been integrated into another branch?
    If a branch has been fully merged into another and is no longer needed, you can delete it using git push origin --delete <dead-branch>. This will remove the branch from both your local repository and the remote repository.
  29. How do you cherry-pick a commit from one branch to another in GIT?
    Cherry-pick a specific commit from one branch to another using git cherry-pick <commit>. First, make sure you’re on the branch where you want to apply the commit, then run the command.
  30. What is the difference between git fetch and git pull in GIT?
    git fetch retrieves new data from a remote repository but does not modify your current working branch. git pull fetches data and immediately updates your current branch with the changes fetched.
  31. How do you manage and release tags in GIT?
    Create and manage tags locally using git tag [tag-name] to create a new tag and git push origin --tags to push all tags to the remote repository. To release a tag, simply push it to the remote repository as described above.
  32. What is the difference between shallow and deep clones in GIT?
    A shallow clone contains only the recent history of a repository, which can be useful for large projects or when you only need recent commits. A deep clone has the full history of the repository, as it would have been if cloned directly from the remote source without any filtering.
  33. How do you handle merge conflicts that occur during a pull request in GIT?
    When a pull request (PR) is opened and a conflict arises upon trying to merge, address the conflicts on the PR page by editing the files, staging the resolved changes, and creating a commit to resolve the conflict. Once resolved, mark the PR as ready for review.
English
language