backend / git / 01_git_commands.md

Useful Git Commands

3 interview angles 3 min read source

Useful Git Commands


1. Basic Setup

  • git init: Initializes a new Git repository in the current directory.
  • git clone <repository-url>: Creates a local copy of a remote repository.

2. Configuring User Information

  • git config --global user.name "<Your Name>": Sets the user name for all repositories on your machine.
  • git config --global user.email "<Your Email>": Sets the user email for all repositories on your machine.

3. Tracking Changes

  • git status: Shows the status of the working directory and staging area (e.g., untracked files, changes staged for commit).
  • git add <file>: Stages a file to be committed.
    • git add .: Stages all changes in the current directory.
  • git commit -m "Commit message": Saves staged changes to the repository with a descriptive message.

4. Viewing History

  • git log: Displays the commit history.
    • git log --oneline: Shows a simplified, one-line history.
  • git diff: Shows the differences between various states.
    • git diff HEAD: Compares the working directory with the last commit.

5. Branching and Merging

  • git branch: Lists all branches in the repository.
    • git branch <branch-name>: Creates a new branch.
  • git checkout <branch-name>: Switches to another branch.
    • git switch <branch-name>: An alternative, modern way to switch branches.
  • git merge <branch-name>: Merges changes from another branch into the current branch.

6. Syncing with Remote Repositories

  • git remote add origin <repository-url>: Links the local repository to a remote repository.
  • git pull: Fetches changes from the remote repository and merges them into the current branch.
  • git push: Uploads local changes to the remote repository.

7. Undoing Changes

  • git reset: Undoes changes by resetting the HEAD.
    • git reset --soft <commit>: Moves HEAD but keeps changes staged.
    • git reset --hard <commit>: Moves HEAD and discards all changes.
  • git checkout -- <file>: Discards changes in the working directory for a specific file.
  • git revert <commit>: Creates a new commit that reverses a specific commit.

8. Stashing Changes

  • git stash: Temporarily saves changes that aren’t ready to be committed.
  • git stash pop: Applies stashed changes and removes them from the stash.
  • git stash list: Displays a list of stashed changes.

9. Tagging

  • git tag <tag-name>: Creates a lightweight tag for a commit.
  • git tag -a <tag-name> -m "Tag message": Creates an annotated tag with a message.

10. Collaboration Commands

  • git fetch: Downloads changes from the remote repository but doesn’t merge them.
  • git pull origin <branch>: Fetches and merges changes from a specific branch on the remote.
  • git push origin <branch>: Pushes changes from the current branch to the remote.

11. Inspecting and Debugging

  • git show <commit>: Shows details about a specific commit.
  • git blame <file>: Shows who made the last change to each line of a file.
  • git log --graph --all --decorate: Displays a graphical view of the commit history.

12. Cleaning Up

  • git clean -n: Displays untracked files and directories to be removed.
  • git clean -f: Deletes untracked files from the working directory.

13. Advanced Commands

  • git cherry-pick <commit>: Applies the changes from a specific commit onto the current branch.
  • git rebase <branch>: Reapplies commits on top of another base branch.

Most Used Daily Workflow

  1. Clone or Initialize: git clone <repo-url> or git init
  2. Check Status: git status
  3. Stage Changes: git add <file> or git add .
  4. Commit Changes: git commit -m "Your message"
  5. Push to Remote: git push origin <branch>
  6. Pull Updates: git pull origin <branch>


Interview angle

  • “Merge or rebase?” - rebase for your own local feature branch to keep history linear before merging; merge for shared branches. Never rebase a branch others have pulled, because rewriting shared history forces everyone to recover manually.
  • “How do you undo a commit that’s already pushed?” - git revert, which adds a new commit undoing the change and preserves shared history. reset --hard rewrites history and is only safe on branches nobody else has.
  • “How do you recover something you think you destroyed?” - git reflog. It records where HEAD has been, so a botched reset or a deleted branch is usually recoverable for a couple of weeks.