586 words
3 minutes
The Complete Guide to Git Commands
Introduction
Git is an essential version control tool for developers, but its complex command system can be daunting. This article systematically organizes core Git commands, demonstrates real-world usage scenarios, and helps you master the Git workflow completely.
1. Environment Setup
1.1 Install Git
# Windows
<https://git-scm.com/downloads>
# macOS
brew install git
# Linux (Debian/Ubuntu)
sudo apt-get install git
1.2 Global Configuration
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "code --wait" # Use VSCode as the default editor
2. Basic Workflow
2.1 Initialize Repository
git init # Create a new repository
git clone <https://github.com/user/repo.git> # Clone a remote repository
2.2 Track Files
git add README.md # Add a single file to the staging area
git add src/ # Add an entire directory to the staging area
git add . # Add all changes (use with caution)
git reset HEAD file.txt # Unstage a file
2.3 Commit Changes
git commit -m "Initial commit" # Commit staged changes with a message
git commit -a -m "Update tracked files" # Skip staging area for tracked files
3. Branch Management
3.1 Branch Operations
git branch # View local branches
git branch feature/login # Create a new branch
git checkout develop # Switch branches
git checkout -b hotfix # Create and switch to a new branch
git branch -d old-branch # Delete a merged branch
git branch -D force-delete # Force delete an unmerged branch
3.2 Merging and Rebasing
git merge feature/login # Standard merge
git rebase main # Rebase current branch onto main
git merge --no-ff dev # Merge without fast-forward
4. Remote Collaboration
4.1 Remote Repository Management
git remote -v # View remote repositories
git remote add upstream https://...
git push -u origin main # Initial push and set upstream tracking
git fetch upstream # Fetch updates without merging
4.2 Pushing and Pulling
git push origin feature/login
git pull --rebase # Pull with rebase
git push --force-with-lease # Safely force push
5. Undo and Restore
5.1 File-Level Operations
git checkout -- file.txt # Discard changes in working directory
git restore --staged logo.png # Unstage file (Git 2.23+)
5.2 Commit-Level Operations
git reset --soft HEAD~1 # Undo last commit but keep changes
git reset --hard a1b2c3d # Reset to a specific commit
git revert HEAD # Create a new commit to reverse the last one
6. Advanced Techniques
6.1 Viewing History
git log --graph --oneline --all # Graphical log
git show commit_id # Show commit details
git blame index.html # Track code authorship
6.2 Stashing Changes
git stash # Temporarily save changes
git stash pop # Reapply last stashed changes
git stash list # List all stashes
6.3 Submodule Management
git submodule add https://...
git submodule update --init --recursive
7. Useful Configuration
.gitignore Example
# Ignore all .class files
*.class
# Ignore node_modules directory
node_modules/
# Exception rule to include a specific file
!important.config
Alias Configuration
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
8. Common Problem Solutions
8.1 Merge Conflict Resolution
- Use
git statusto view conflicted files - Manually edit files with conflict markers
<<<<<<< - Use
git addto mark conflicts as resolved - Complete the merge with a commit
8.2 Recover Lost Commits
git reflog # View operation history
git checkout HEAD@{5} # Restore to a specific state
Conclusion
Mastering these commands allows you to handle 90% of everyday Git usage. Git’s true power lies in its flexible workflows. Combine these skills with graphical tools like the VSCode Git plugin for even more efficiency. Remember: frequent commits, branching often, and using push -f with caution are the golden rules for safe Git practices.
Appendix: Git Command Quick Reference
| Operation Type | Common Commands |
|---|---|
| Repo Initialization | init, clone |
| File Operations | add, reset, restore |
| Commit Management | commit, amend |
| Branch Operations | branch, checkout, merge, rebase |
| Remote Collaboration | remote, push, fetch, pull |
| Undo Actions | checkout, reset, revert |
| View History | log, show, diff |
Bookmark this guide for quick reference during real-world development!
The Complete Guide to Git Commands
https://en.dymripper.com/posts/git/git/
