Git Common Commands Cheat Sheet
Git is currently the world's most advanced distributed version control system. Below is a checklist of commands most frequently used in daily development.
Version Control1. Initialization and Configuration
| Command | Description |
|---|---|
git init | Initialize a new Git repository in the current directory |
git clone <url> | Clone a remote repository to local |
git config --global user.name "Your Name" | Set username for commits |
git config --global user.email "email@example.com" | Set email for commits |
git config --list | View current configuration info |
2. Code Submission (Working Directory -> Staging Area -> Local Repository)
bash
# Check file status
git status
# Add specific file to staging area
git add <filename>
# Add all modifications to staging area
git add .
# Commit staging area to local repository
git commit -m "Commit message info"
# Modify last commit message (or append missed files)
git commit --amend3. Branch Management
Branching is Git's killer feature, used for parallel development.
| Command | Description |
|---|---|
git branch | List all local branches |
git branch <name> | Create new branch |
git checkout <name> | Switch to specified branch |
git switch <name> | (Newer) Switch branch |
git checkout -b <name> | Create and switch to new branch |
git merge <name> | Merge specified branch into current branch |
git branch -d <name> | Delete branch |
4. Remote Synchronization
bash
# View remote repository address
git remote -v
# Add remote repository
git remote add origin <url>
# Pull remote code and merge (pull = fetch + merge)
git pull origin <branch_name>
# Push local code to remote
git push origin <branch_name>
# Force push (Use with caution, especially in collaboration)
git push -f origin <branch_name>5. Undo and Rollback
Discard changes in working directory (not added):
bashgit checkout -- <filename> # Or newer command git restore <filename>Undo changes in staging area (added, not committed):
bashgit reset HEAD <filename> # Or newer command git restore --staged <filename>Version Rollback (committed):
bash# View commit log git log --oneline # Rollback to previous version git reset --hard HEAD^
