Git Command Cheat Sheet: Basics, Branching, Remotes & Workflows
This comprehensive guide covers essential Git commands for repository initialization, file staging, committing, branching, merging, remote synchronization, undo operations, tagging, submodules, and workflow patterns like GitHub Flow and Git Flow, with practical examples and common pitfalls.
This article provides a structured reference for daily Git operations, organized into twelve sections from basic setup to advanced workflows.
1. Basic Operations
git init– initialize a local repository. git clone <remote URL> – clone a remote repository locally. Example: git clone https://github.com/user/repo.git.
2. File Operations
git status– view file status. git add <file path> – stage a specific file. git add . – stage all changes. git reset <file path> – unstage a file. git checkout -- <file> – discard working‑directory changes (irreversible, use with caution).
3. Commits and History
git commit -m "message"– commit staged changes. git commit -am "message" – commit tracked files directly from working directory. git commit --amend – modify the last commit. git log – view commit history. git log --oneline – compact one‑line view. git log --graph – visualize branch merge graph. git diff – compare working directory with staging area. git diff HEAD – compare working directory with latest commit.
Best practice: write clear commit messages (e.g., "Fix login validation bug" instead of "Update").
4. Branch Management
Create and Switch Branches
git branch <branch name>– create branch. git checkout <branch name> – switch branch. git checkout -b <branch name> – create and switch. Example: git checkout -b feature/login. git branch -d <branch name> – delete merged branch. git branch -D <branch name> – force delete unmerged branch.
Merge and Rebase
git merge <source branch>– merge source into current branch. git rebase <target branch> – rebase current branch onto target.
Pitfall: during merge conflicts, manually edit conflicted files (markers
<<<<<<< ======= >>>>>>>), then git add and git commit.
5. Remote Repositories
Remote Management
git remote add <alias> <remote URL>– add remote. Example: git remote add origin https://github.com/user/repo.git. git remote -v – list remotes. git remote rename <old> <new> – rename remote.
Push, Pull, Fetch
git push <remote> <branch>– push branch to remote. git push -u <remote> <local branch> – push and set upstream. git pull <remote> <branch> – fetch and merge. git pull --rebase – fetch and rebase (avoids extra merge commits). git fetch <remote> – fetch only, no merge.
Pitfall: git pull can create unnecessary merges; prefer git fetch + git rebase.
6. Undo and Rollback
Undo Commits
git reset --hard <commit hash>– hard reset to commit (discards uncommitted changes, only for local unpushed commits). git reset <commit hash> – soft reset, keep changes in working directory. git revert <commit hash> – create a new commit that undoes the specified commit.
Stash Changes
git stash– stash current changes. git stash pop – restore latest stash. git stash list – list stashes. git stash drop – delete latest stash.
7. Tag Management
git tag <tag name>– create lightweight tag. git tag -a <tag name> -m "message" – create annotated tag. git tag – list tags. git push <remote> <tag name> – push tag to remote. Example: git tag v1.0.0.
8. Advanced Tips
Submodules
git submodule add <repo URL> <path>– add submodule. git submodule update --init – initialize submodules.
Interactive Staging
git add -p– interactively select hunks to stage.
.gitignore
Create .gitignore in project root with patterns:
node_modules/
*.log
dist/9. Common Error Handling
Merge Conflicts
git status– identify conflicted files.
Manually edit files, resolve
<<<<<<< ======= >>>>>>>markers. git add <conflicted file>. git commit.
Push Rejected
git pull --rebase– fetch remote changes and rebase. git push – push again.
Recover Lost Commits
git reflog– view operation log. git checkout <commit hash> – switch to the lost commit.
10. Workflow Recommendations
GitHub Flow
Create branch: git checkout -b feature/new.
Commit changes: git commit -am "Add new feature".
Push branch: git push origin feature/new.
Create Pull Request.
After review and merge, delete branch.
Git Flow
git flow init– initialize Git Flow. git flow feature start <feature name> – start feature branch. git flow release start v1.0.0 – start release branch. git flow hotfix start <hotfix name> – start hotfix branch.
11. Common Aliases
Add to .gitconfig:
[alias]
co = checkout
ci = commit
st = status
br = branch
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit12. Branch Naming Conventions
Feature: feature/xxx or feat/xxx.
Bug fix: fix/xxx or bugfix/xxx.
Hotfix: hotfix/xxx.
Documentation: docs/xxx.
Test: test/xxx.
Mastering these commands covers 90% of daily Git scenarios. Practice in real projects and use git help <command> for official documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lakehouse Research Base
Focused on technical sharing in the data field, covering a tech stack that includes Hadoop, Spark, Flink, Kafka, Fluss, Paimon, Iceberg, StarRocks, ClickHouse, ES, Milvus, and more. Welcome to follow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
