1. What are some basic Git commands and their purposes?
Answer:
Git provides core commands to manage source code and collaborate on development projects. Here are the most important ones:
- git init: Initializes a new Git repository in your current folder.
- Creates a .git directory.
- Starts version control for your project.
- Creates a .git directory.
- git clone <repo_url>: Clones a remote repository to your local system.
- Useful when starting work on an existing repository.
- Brings full version history.
- Useful when starting work on an existing repository.
- git status: Shows the current state of the working directory.
- Highlights modified, staged, and untracked files.
- Highlights modified, staged, and untracked files.
- git add <filename>: Stages changes for commit.
- Use git add . to stage all modified files.
- Prepares files to be saved in version history.
- Use git add . to stage all modified files.
- git commit -m “message”: Saves staged changes with a commit message.
- A checkpoint in the development process.
- A checkpoint in the development process.
- git push: Uploads local commits to a remote repository.
- Use git push origin <branch> to push a specific branch.
- Use git push origin <branch> to push a specific branch.
- git pull: Fetches and merges remote changes into your local branch.
- git fetch: Only downloads changes from the remote (no merge).
- git merge <branch>: Combines another branch into the current branch.
- git log: Displays the commit history.
- git diff: Shows changes not yet staged or committed.
2. What are Git branching commands and how are they used?
Answer:
Branching allows developers to work in parallel without affecting the main code.
- git branch: Lists all branches in the repository.
- git branch <name>: Creates a new branch.
- git checkout <branch>: Switches to an existing branch.
- git checkout -b <branch>: Creates and switches to a new branch.
- git merge <branch>: Merges another branch into the current one.
- git rebase <branch>: Reapplies commits on top of another base tip.
- git branch -d <branch>: Deletes a branch (safe delete).
- git branch -D <branch>: Force deletes a branch.
- git remote set-head origin <branch>: Sets the default branch for the remote.
3. How do you manage remote repositories in Git?
Answer:
Remotes connect your local project with cloud-hosted repositories like GitHub or GitLab.
- git remote -v: View remote repository URLs.
- git remote add origin <url>: Add a remote repository.
- git remote remove origin: Remove a remote.
- git push origin <branch>: Push changes to a remote branch.
- git push -u origin <branch>: Push and set upstream tracking.
- git pull origin <branch>: Pull changes from a specific remote branch.
- git fetch origin: Fetch changes without merging.
- git remote show origin: Shows detailed info about the remote.
4. How do you stage and commit changes in Git?
Answer:
This process captures changes in your working directory and saves them in version history.
- git add .: Stages all changes.
- git add -A: Stages all including deletions.
- git reset <file>: Unstages a file.
- git commit –amend: Modify the previous commit.
- git commit -a or –all: Stages and commits all modified files.
- git commit –no-verify: Skips hooks during commit.
- git reset –soft HEAD~1: Undo the last commit but keep changes staged.
- git reset –hard HEAD~1: Undo last commit and discard all changes.
5. How can you view and compare changes in Git?
Answer:
Inspect changes using diff and log commands:
- git diff: Shows unstaged changes.
- git diff –staged: Shows staged vs last commit.
- git diff <commit_id>: Compares working directory with a specific commit.
- git log: Full commit history.
- –oneline: Condensed view.
- –graph: Tree-like view.
- –author=”name”: Filter by author.
- –since=”1 week ago”: Filter by time.
- –oneline: Condensed view.
- git blame <file>: Shows who changed each line.
- git show <commit_id>: Shows details of a specific commit.
- git show <commit>:<file>: View file content at a specific commit.
6. How do you undo changes in Git (reset, revert)?
Answer:
Undo actions depending on whether changes are committed, staged, or just in the working directory.
- git reset <commit>: Moves HEAD to a previous commit.
- –soft: Keep changes staged.
- –mixed: Keep changes in working directory.
- –hard: Discards all changes.
- –soft: Keep changes staged.
- git revert <commit>: Creates a new commit that reverses a past commit.
- git restore <file>: Restores file from index.
- git restore –staged <file>: Removes file from staging area.
- git clean -f: Deletes untracked files.
- git clean -fd: Deletes untracked files and folders.
7. What is Git stash and how is it used?
Answer:
Stashing is used to temporarily save your changes without committing.
- git stash: Stashes changes.
- git stash list: Shows stash history.
- git stash pop: Applies and removes latest stash.
- git stash apply: Applies stash without removing it.
- git stash drop: Deletes a specific stash.
- git stash clear: Deletes all stashes.
- git stash save “msg”: Adds a message to stash.
- git stash branch <name>: Creates a new branch from stash.
8. How do you work with Git tags?
Answer:
Tags mark specific points in history, often used for version releases.
- git tag: Lists all tags.
- git tag <name>: Create a lightweight tag.
- git tag -a <name> -m “msg”: Annotated tag with metadata.
- git tag -d <name>: Delete local tag.
- git push origin <tag>: Push a specific tag.
- git push origin –tags: Push all tags.
- git fetch –tags: Fetch all remote tags.
9. How do you configure Git?
Answer:
Git can be configured globally or per repository.
- git config –global user.name “Your Name”
- git config –global user.email “you@example.com”
- git config –list: Shows current config.
- git config <key> <value>: Set a specific config.
- git config –global core.editor “code”: Change default editor.
- git config –global color.ui true: Enable color output.
- git config –global alias.st status: Create shortcut command (alias).
10. What are Git aliases and how are they useful?
Answer:
Aliases simplify long commands.
Example:
bash
git config –global alias.co checkout
- Now git co = git checkout.
11. What are Git hooks and why are they used?
Answer:
Git hooks are scripts that run automatically during Git events.
- Stored in .git/hooks/
- pre-commit: Run before commit (e.g., linting).
- commit-msg: Validate commit messages.
- post-commit: Actions after commit.
12. What are Git submodules?
Answer:
Submodules help include other Git repositories inside your own.
- git submodule add <repo>: Add a new submodule.
- git submodule init: Initialize submodules.
- git submodule update: Get latest defined commit.
- git submodule status: See current commit for submodule.
- git submodule deinit: Remove a submodule.
- git submodule update –remote: Pull latest from submodule.
- git submodule foreach: Run a command in all submodules.
13. What are Git workflow commands for collaboration?
Answer:
These are useful in team environments to manage changes:
- git pull –rebase: Apply local commits on top of fetched changes.
- git rebase -i <commit>: Interactive rebase to squash, edit commits.
- git rebase –continue: Continue after fixing conflicts.
- git rebase –abort: Abort the rebase.
- git merge –no-ff: Force a merge commit even if fast-forward is possible.
- git pull –no-commit: Pull changes without auto-committing.
- git push –force-with-lease: Force push with safety checks.
- git push –force: Force push (dangerous if not used carefully).
- git cherry-pick <commit>: Apply a specific commit from another branch.
👉The Next 13 Questions-2: GIT COMMANDS