Mastering Git and GitHub: Essential Commands and Top Interview Questions
Table of contents
Git-GitHub Commands-
git init: Initialize a new Git repository in the current directory.
git clone <repository-url>: Clone a remote repository to your local machine.
git status: Show the current status of the repository (Untracked (Unstaged),Modified (Unstaged),Staged,Committed).
git add <file>: Add a file to the staging area to be included in the next commit.
git commit -m "commit message": Create a new commit with the staged changes and a descriptive message.
git diff: View the differences between the working directory and the staging area(scenario let you have 2 files fileA & fileB both are added and commited then you modified both but then you add fileA and not fileB ,then you get a difference between the staged and unstaged).
git log: Display the commit history of the repository.
git branch: List all branches in the repository.
git checkout <branch_name>: Switch to a different branch.
git merge <branch_name>: Merge changes from the specified branch into the current branch(Means the branch you want to merge into the current branch.).
git pull: Fetch and merge changes from a remote repository into your local machine.
git push: Push local commits to a remote repository.
git remote add origin <repository-url>: Add a remote repository as the origin (usually the main remote).
git remote -v: View a list of remote repositories and their URLs.
git reset <file>: Unstage changes for a file, removing them from the staging area.(let You add
example.txt
to the staging area,You decide not to includeexample.txt
in the next commit, so you unstage it.)git rm <file>: Remove a file from the repository and staging area.
git stash: Temporarily store changes not ready for a commit.
git fetch: Download objects and refs from another repository without merging.
git push origin <branch>: Push local commits to a remote branch on GitHub.
git pull origin <branch>: Fetch and merge changes from a remote branch on GitHub into the current local branch.
git clone <repository-url>: Clone a GitHub repository to your local machine.
git fork: Create a copy of a repository on your GitHub account (forking).
git pull-request: Create a pull request to propose changes from your fork to the original repository.
git pull upstream <branch>: Pull changes from the original repository after forking (upstream).
git remote add upstream <original-repo-url>: Add the original repository as the upstream remote for your fork.
git branch -d <branch>: Delete a local branch after it's merged and no longer needed.
git push origin --delete <branch>: Delete a remote branch on GitHub.
git revert <commit>: Create a new commit that undoes the changes from a specific commit.
git log: see all the previous commit with the commit id.
git reset --hard <commit_id>: It will return you to the previous commit.
Git-GitHub Interview Question-
Git Questions:
What is Git, and why is it used in software development?
Answer: Git is a version control system that tracks changes in code, allowing multiple developers to collaborate and manage code history.
Explain the difference between git fetch and git pull ?
Answer:
git fetch
updates your local copy with changes from the remote, whilegit pull
does the same and also merges them into your current branch.How does the git reset command differ from git revert? When would you use each?
Answer:
git reset
moves the branch pointer to a previous commit, effectively undoing commits. It can erase commits from history if used with--hard
.git revert
creates a new commit that undoes the changes from a previous commit, preserving the commit history.Can you explain what a branch is in Git, and how do you create a new branch?
Answer: A branch is a separate line of development. You create one with
git branch <branch-name>
and switch to it withgit checkout <branch-name>
or directly create and switch withgit checkout -b <branch-name>
.What is a merge conflict, and how do you resolve it?
Answer: A merge conflict happens when changes in different branches clash. You resolve it by manually editing the conflicting files and then committing the changes.
Describe the difference between git rebase and git merge? When would you prefer one over the other?
Answer:
git rebase
rewrites commit history to create a linear path, whilegit merge
combines changes from different branches, preserving history. Userebase
to keep history clean; usemerge
when you want to retain the branch history.What is the purpose of the .gitignore file?
Answer: It tells Git which files or directories to ignore and not track, like build files or sensitive data.
How would you remove a file from the Git repository but keep it locally?
Answer: Use
git rm --cached <file>
to remove the file from the repo but keep it on your local machine.What is the git stash command used for? Can you describe a scenario where it would be useful?
Answer:
git stash
temporarily saves your changes without committing them, useful when you need to switch branches without losing work.
GitHub Questions:
What is GitHub, and how does it relate to Git?
Answer: GitHub is a platform for hosting Git repositories online, enabling collaboration, code review, and project management.
Can you explain what a pull request is in GitHub? How does it work in a collaborative environment?
Answer: A pull request is a way to propose changes to a codebase, allowing others to review and discuss the changes before merging them into the main branch.
How do you fork a repository, and what is the purpose of forking?
Answer: Forking creates your own copy of someone else's repository, allowing you to experiment or contribute without affecting the original project.
What is the difference between GitHub and GitLab?
Answer: Both are platforms for hosting Git repositories, but GitLab offers more built-in CI/CD features(like it has runner which enables to create pipeline), while GitHub is more widely used and has GitHub Actions for CI/CD.
How do you handle collaboration on GitHub using issues and project boards?
Answer: Issues are used to track tasks or bugs, and project boards organize these issues into workflows (like to-do, in-progress, done).
Explain the difference between a public and a private repository on GitHub.
Answer: Public repositories are visible to everyone, while private repositories are only accessible to those you invite.
What is GitHub Actions, and how can it be used in CI/CD pipelines?
Answer: GitHub Actions automates workflows, such as testing and deploying code, making it easy to set up CI/CD directly within GitHub.
What is the purpose of a GitHub Gist, and when would you use it?
Answer: GitHub Gists are used for sharing small code snippets or notes. Use them when you need to quickly share code or ideas.
Can you describe how to use GitHub Pages to host a static website?
Answer: GitHub Pages allows you to host static websites directly from a GitHub repository by pushing your website files to a special branch (
gh-pages
ormain
/master
).
Final Thoughts:
Thank you for exploring this guide on essential Git and GitHub commands, along with some key interview questions! ๐ I hope this blog has helped you grasp these fundamental concepts and prepare confidently for your next interview. ๐โจ
If you have any questions, run into any challenges, or want to share your own tips and experiences, feel free to drop a comment below. ๐ฌ๐ Iโm always here to help and would love to hear how your learning journey is going! ๐
Happy coding! ๐จโ๐ป๐ฉโ๐ป Keep pushing, keep merging, and keep mastering Git and GitHub! ๐ก๐ง