Git Command Quick Reference: Common Operations
3 min
| note
Basic Concepts
- Working Directory: The directory where you are editing files.
- Staging Area: The “buffer zone” where files are prepared for commit after using
git add. - Local Repository: The version repository where files are permanently stored after using
git commit(on your computer). - Remote Repository: Cloud repository on platforms like GitHub/Gitee (e.g.,
origin).
1. Core Three-Step Process (Daily Commit Cycle)
This is the command sequence you’ll use most often to save local changes to the cloud.
# 1. Add all changes from working directory to staging area
git add . # Add all changed files (new, modified, deleted)
# Or add specific files
git add file1 file2
# 2. Commit staging area content to local repository with description
git commit -m "Clear description of this commit"
# 3. Push local repository commits to remote repository
git push # Can be used directly if already associated (git push -u)
# Establish association when pushing a branch for the first time
git push -u origin branch-name # -u sets upstream, then use git push directlyKey Notes:
- The commit message in
git commit -m "xxx"is the soul of version history, please fill it carefully, e.g.: “Fix user login failure bug” or “Add product search functionality”.
2. Remote Repository Management
1. First-time association with remote repository (not needed after cloning)
# Give the remote repository an alias (usually called origin) and set its address
git remote add origin your-repository-url2. View remote repository information
git remote -v # View all remote repository aliases and their URLs3. Change remote repository address
# Direct modification (recommended)
git remote set-url origin new-repository-url
# Or remove first then add
git remote remove origin
git remote add origin new-repository-url4. Get latest status from remote repository
# Pull remote branch updates and merge with current local branch (most used)
git pull
# Equivalent to git fetch + git merge
# Only get remote update information without automatic merging (safer, easier to check)
git fetch
git merge origin/branch-name # Manually merge to current branch
# Or
git rebase origin/branch-name # Rebase merge, keep linear history3. Branch Renaming Guide
1. Rename local branch
# Rename the current branch
git branch -m new-branch-name
# Rename a specific local branch
git branch -m old-branch-name new-branch-name2. Push new branch and sync with remote (core step)
# Push the renamed local branch to remote and establish association
git push -u origin new-branch-name3. Delete old remote branch (cleanup)
# After confirming the new branch is pushed successfully and everything is normal, delete the old remote branch
git push origin --delete old-branch-nameSummary flow: Rename locally -> Push new branch -> (Optional) Delete old remote branch
4. Common Scenarios and Advanced Commands
Scenario 1: Clone an existing project
git clone repository-urlScenario 2: Create and switch to a new branch
git checkout -b new-branch-name # Classic command
# Or
git switch -c new-branch-name # More intuitive command in Git 2.23+Scenario 3: Check status and history
git status # Check status of working directory and staging area (must know!)
git log --oneline --graph # View concise, graphical commit historyScenario 4: Undo and rollback
# Undo changes in working directory (dangerous! Irreversible)
git checkout -- file-name
# Move file from staging area back to working directory (cancel add)
git restore --staged file-name
# Undo the last commit and keep changes in working directory
git reset --soft HEAD~1
# Force push to overwrite remote commits (use with caution!)
git push --force-with-leaseScenario 5: Resolve push conflicts
- First pull the latest code:
git pull - Resolve conflict markers (
<<<<<<<,=======,>>>>>>>) in the editor - Recommit and push:
git add .->git commit -m "Resolve merge conflict"->git push