Blog / Others/ Git Command Reference Manual

Git Command Reference Manual

【转载】Git命令参考手册

Git is a powerful distributed version control system. Mastering its common commands is essential for efficient collaboration. This reference manual covers core operations from configuration and commits to branch management and remote collaboration.

Configuration & Initialization

Set up your Git environment before starting.

# Initialize a new local repository
git init

# Configure global user name and email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Enable colored output for better readability
git config --global color.ui auto

Cloning & Status

# Clone a remote repository
git clone <repository-url>

# Check the status of the working directory and staging area
git status

Committing Changes

Record file modifications into the repository history.

# Stage a specific file
git add <file>
# Stage all changed files in the current directory
git add .

# Commit staged changes with a message
git commit -m "Commit message"

# Stage and commit tracked files in one step
git commit -am "Commit message"

# Amend the last commit message (no new commit)
git commit --amend -m "New commit message"

Viewing History & Diffs

# Show commit history
git log
# Show last 5 commits
git log -5
# Show concise, graphical history
git log --oneline --graph

# Show diff between working directory and staging area
git diff
# Show diff between staging area and last commit
git diff --cached
# Compare working directory with a specific commit (e.g., HEAD^)
git diff HEAD^

Branch Operations

Branches enable parallel development.

# List local branches (* indicates current)
git branch
# List remote branches
git branch -r
# List all branches
git branch -a

# Create and switch to a new branch
git checkout -b <new-branch-name>
# Switch to an existing branch
git checkout <branch-name>

# Delete a merged branch
git branch -d <branch-name>
# Force delete a branch (even if unmerged)
git branch -D <branch-name>

# Merge a branch into the current branch
git merge <branch-name>

# Rebase current branch onto target branch
git rebase <target-branch>

Tag Management

Tags are often used to mark release versions.

# List all tags
git tag
# Create an annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tags to remote repository
git push origin --tags

Remote Collaboration

# View remote repository information
git remote -v
# Add a remote alias
git remote add origin <url>

# Fetch updates from remote (no auto-merge)
git fetch origin
# Fetch and merge a remote branch into current branch
git pull origin <branch-name>

# Push a local branch to remote
git push origin <branch-name>
# Delete a remote branch
git push origin --delete <branch-name>

Undo & Reset

# Discard changes in working directory for a file (restore to last commit)
git checkout -- <file>

# Unstage a file (undo git add)
git reset HEAD <file>

# Hard reset current branch to a commit (discards all subsequent changes, use with caution)
git reset --hard <commit-hash>

# Create a new commit that undoes a specific commit (safe undo)
git revert <commit-hash>

Stash & Cleanup

# Temporarily stash changes in working directory and staging area
git stash
# List all stashes
git stash list
# Apply and remove the most recent stash
git stash pop
# Delete a stash
git stash drop

# Run garbage collection to optimize local repository
git gc

This manual covers most high-frequency Git commands for daily use. For advanced scenarios (interactive rebase, submodules, hooks), refer to the official Git documentation. The key is understanding the concepts of the working directory, staging area, and repository, as well as branch and merge workflows.

Post a Comment

Your email will not be published. Required fields are marked with *.