Important Git Commands
1. What does git init do?
Creates a new local Git repository in the current folder.
git init
It creates the hidden .git folder that stores Git history and metadata.
Interview Answer: git init initializes an existing/new folder as a Git repository.
2. What does git clone do?
Creates a local copy of an existing remote repository, including its history.
git clone https://github.com/user/project.git
Interview Answer: git clone downloads a remote repository and creates a local working copy.
3. What does git status do?
Shows the current state of the working directory and staging area.
git status
It shows:
- Modified files
- Staged files
- Untracked files
Interview Answer: git status tells us which files are modified, staged, or untracked.
4. What does git add do?
Moves changes from the Working Directory → Staging Area.
git add Program.cs
Or:
git add .
Interview Answer: git add stages changes so they can be included in the next commit.
5. What does git commit do?
Saves the staged changes as a snapshot in the local repository.
git commit -m "Added login feature"
Staging Area → Local Repository
Interview Answer: git commit permanently records staged changes in the local Git history.
6. What does git push do?
Sends local commits to a remote repository.
git push origin main
Local Repository → Remote Repository
Interview Answer: git push uploads local commits to a remote repository such as GitHub or Azure Repos.
7. What does git pull do?
Gets changes from the remote repository and integrates them into the current branch.
git pull origin main
Conceptually:
git pull = fetch + integrate
Usually the integration is a merge, depending on configuration/options.
Interview Answer: git pull retrieves remote changes and integrates them into the current local branch.
8. What does git fetch do?
Downloads the latest remote commits and references without integrating them into your current branch.
git fetch origin
This allows you to inspect remote changes before integrating them.
fetch vs pull
fetch → Download only
pull → Download + Integrate
Interview Answer: git fetch retrieves remote changes without automatically changing my current branch.
9. What does git log do?
Displays the commit history.
git log
Short format:
git log --oneline
Example:
a12bc34 Added login
b45de67 Added authentication
c78fg90 Initial commit
Interview Answer: git log is used to view commit history, including commit hashes, authors, dates, and messages.
10. What does git diff do?
Shows the differences between versions of files.
git diff
By default, it shows unstaged changes compared with the index.
To see staged changes:
git diff --staged
Interview Answer: git diff helps us inspect code changes before committing them.
11. What does git remote do?
Manages connections to remote repositories.
git remote -v
Typical output:
origin https://github.com/user/project.git
Add a remote:
git remote add origin <repository-url>
Interview Answer: git remote is used to view and manage remote repository connections.
12. What does git checkout do?
git checkout is an older multi-purpose command commonly used to switch branches or restore files.
Switch branch:
git checkout main
Create and switch:
git checkout -b feature/login
Modern Git provides clearer commands such as git switch and git restore.
Interview Answer: git checkout can switch branches and restore files, although modern Git provides switch and restore for these purposes.
13. What does git switch do?
Used specifically to switch branches.
git switch main
Create and switch to a new branch:
git switch -c feature/login
Interview Answer: git switch is the modern command specifically designed for switching or creating branches.
14. What does git restore do?
Used to restore file content or discard certain changes.
For example, discard an unstaged change:
git restore Program.cs
Unstage a file:
git restore --staged Program.cs
Interview Answer: git restore is used to restore files or remove changes from the staging area.
15. git add . vs git add <file>
git add .
Stages applicable changes under the current directory:
git add .
Useful when you want to stage multiple changed files.
git add <file>
Stages only the specified file:
git add Program.cs
Difference
| Command | Purpose |
|---|---|
git add . |
Stage changes under current directory |
git add Program.cs |
Stage only Program.cs |
Interview Answer: git add . stages changes under the current directory, while git add <file> stages only a specific file. I prefer staging specific files when I want precise control over what goes into a commit.
Quick Revision
init → Create repository clone → Copy remote repository status → Check status add → Stage changes commit → Save locally push → Local → Remote pull → Fetch + Integrate fetch → Download remote changes log → View commit history diff → View differences remote → Manage remote repositories checkout → Older multi-purpose switch/restore switch → Switch branches restore → Restore files
git init vs git clone
Both commands create a local Git repository, but they are used in different situations.
git init |
git clone |
|---|---|
| Creates a new Git repository | Copies an existing repository |
| Used when starting Git for a local project | Used when a remote repository already exists |
Creates a .git folder |
Downloads files, history, branches/references and creates .git |
| No remote is automatically required | Usually configures the source repository as origin |
git init |
git clone <url> |
Example
Starting a new project:
cd MyProject
git init
git add .
git commit -m "Initial commit"
Joining an existing project:
git clone https://github.com/user/MyProject.git
cd MyProject
Simple Flow
New local project
↓
git init
Existing remote repository
↓
git clone
↓
Local copy of repository
Key Point
init= Start a new Git repositoryclone= Copy an existing Git repository
Interview Answer
git initinitializes a new Git repository in an existing local folder.git clonecreates a local copy of an existing repository, including its history, and normally configures the original repository as theoriginremote.