Git Beginner’s Guide: Quick Reference Cheat Sheet

Git is a powerful version control system that helps you track file changes, collaborate on development, and roll back code. For beginners, mastering core commands is key to getting started. This cheat sheet compiles the most common Git commands with simple explanations and examples for quick reference.

1. Basic Configuration & Repository Initialization

1. Configure User Information (Global/Local)

  • git config --global user.name "Your Name"
    Sets the global username (applies to all repositories). Subsequent commits will use this information.
    Example: git config --global user.name "Zhang San"

  • git config --global user.email "your@email.com"
    Sets the global email (required; otherwise, commits will fail).
    Example: git config --global user.email "zhangsan@example.com"

  • git config user.name "Local Username"
    Sets a local username (only for the current repository; overrides global settings). Optional (recommended to use global first for new users).

2. Initialize a Local Repository

  • git init
    Creates an empty Git repository in the current directory (or initializes an existing directory as a repository).
    Example: Create a folder on your desktop, open Terminal, and rungit init`*

2. Workspace & Staging Area Operations

1. Check File Status

  • git status
    Shows changes in the workspace and staging area (which files are new, modified, or untracked).
    Example: Output will show statuses like “changes to be committed”

2. Add Files to Staging Area

  • git add [filename]
    Adds a specific file from the workspace to the staging area (preparing for commit).
    Example: git add test.txt (adds only test.txt)

  • git add .
    Adds all modified/new files in the current directory to the staging area (. represents the current directory).
    Example: git add . (adds all untracked files)

3. Commit Staged Changes to Repository

  • git commit -m "Commit Message"
    Saves staged changes permanently to the local repository. A clear commit description (required) follows -m.
    Example: git commit -m "Added README documentation"

3. Branch Operations

1. Create & Switch Branches

  • git branch [branch-name]
    Creates a new branch (only creates; does not switch).
    Example: git branch dev (creates a dev development branch)

  • git checkout -b [branch-name]
    Creates and switches to a new branch (most common branch operation).
    Example: git checkout -b feature/login (creates and switches to a login feature branch)

2. Merge Branches

  • git merge [target-branch]
    Merges changes from the specified branch into the current branch (switch to the target branch first).
    Example: git checkout master (switch back to main branch) → git merge dev (merge dev into master)

3. List Branches

  • git branch
    Lists all local branches (current branch is marked with *).
    Example: Output shows master*, dev, etc.

  • git branch -r
    Lists remote repository branches (e.g., origin/master).

4. Remote Repository Operations

1. Connect to a Remote Repository

  • git remote add origin [remote-repo-url]
    Links a local repository to a remote repository (used for the first connection). origin is the default remote name.
    Example: git remote add origin https://github.com/yourname/yourrepo.git

  • git remote -v
    Checks the remote repository URL (verifies connection).

2. Pull & Push Code

  • git pull origin [branch-name]
    Pulls code from the remote repository and merges it into the local branch (equivalent to “pull + update”).
    Example: git pull origin master (pulls the remote master branch to local)

  • git push origin [branch-name]
    Pushes local branch changes to the remote repository (add -u for the first push to establish tracking).
    Example: git push -u origin dev (first push of the dev branch to remote)

5. Undo & Recovery

1. Undo Staged Files

  • git reset HEAD [filename]
    Removes a file from the staging area (undoes git add).
    Example: git reset HEAD test.txt (unstage test.txt)

2. Undo the Last Commit

  • git reset --soft HEAD~1
    Keeps workspace and staging area; only undoes the last commit (reverts to staging area).
    Example: git reset --soft HEAD~1 (undoes the last commit)

  • git reset --hard HEAD~1
    Completely reverts (workspace, staging area, and repository). Use with caution! Data may be lost.

3. Discard Workspace Changes

  • git checkout -- [filename]
    Discards local modifications to a file (restores it to the last commit state).
    Example: git checkout -- app.js (abandon local changes to app.js)

6. View Commit History

  • git log
    Shows all commit records, including commit ID, author, time, and description.
    Common flags:
  • git log --oneline (simplified output, one line per commit)
  • git log --graph (visualizes branch history)

7. Troubleshooting Common Issues

1. Branch Merge Conflicts

  • Scenario: Git throws “Automatic merge failed” when merging branches.
  • Solution: Open conflict files (marked with <<<<<<< HEAD (local code) and ======= (remote code), >>>>>>> branch-name (merged code)). Edit manually, then run git add [filename]git commit.

2. Stash Unfinished Work

  • git stash
    Stashes current workspace changes (e.g., when switching branches suddenly). Use git stash pop to restore stashed changes.

Summary

Git commands become familiar with practice. For issues, use git --help [command] or search error messages. Remember: git addgit commit is the foundation; branch and remote operations are core for collaboration.

(Bookmark this for quick reference!)

Xiaoye