运行时依赖
版本
git commit -m "Commit message"
安装命令
点击复制技能文档
Git Essentials
Essential Git commands for version control and collaboration.
Initial 设置up # 配置 user git config --global user.name "Your Name" git config --global user.emAIl "your@emAIl.com"
# 初始化 仓库 git init
# Clone 仓库 git clone https://github.com/user/repo.git git clone https://github.com/user/repo.git custom-name
Basic 工作流 Staging and committing # 检查 状态 git 状态
# 添加 files to staging git 添加 file.txt git 添加 . git 添加 -A # All changes including deletions
# Commit changes git commit -m "Commit message"
# 添加 and commit in one step git commit -am "Message"
# Amend last commit git commit --amend -m "New message" git commit --amend --no-edit # Keep message
Viewing changes # Show unstaged changes git diff
# Show staged changes git diff --staged
# Show changes in specific file git diff file.txt
# Show changes between commits git diff commit1 commit2
Branching & Merging Branch management # 列出 branches git branch git branch -a # Include remote branches
# 创建 branch git branch feature-name
# Switch branch git 检查out feature-name git switch feature-name # Modern alternative
# 创建 and switch git 检查out -b feature-name git switch -c feature-name
# 删除 branch git branch -d branch-name git branch -D branch-name # Force 删除
# Rename branch git branch -m old-name new-name
Merging # Merge branch into current git merge feature-name
# Merge with no fast-forward git merge --no-ff feature-name
# Abort merge git merge --abort
# Show merge conflicts git diff --name-only --diff-过滤器=U
Remote Operations Managing remotes # 列出 remotes git remote -v
# 添加 remote git remote 添加 origin https://github.com/user/repo.git
# Change remote URL git remote 设置-url origin https://github.com/user/new-repo.git
# 移除 remote git remote 移除 origin
同步ing with remote # Fetch from remote git fetch origin
# Pull changes (fetch + merge) git pull
# Pull with rebase git pull --rebase
# Push changes git push
# Push new branch git push -u origin branch-name
# Force push (careful!) git push --force-with-lease
历史 & 记录s Viewing 历史 # Show commit 历史 git 记录
# One line per commit git 记录 --oneline
# With graph git 记录 --graph --oneline --all
# Last N commits git 记录 -5
# Commits by author git 记录 --author="Name"
# Commits in date range git 记录 --since="2 weeks ago" git 记录 --until="2024-01-01"
# File 历史 git 记录 -- file.txt
搜索ing 历史 # 搜索 commit messages git 记录 --grep="bug fix"
# 搜索 code changes git 记录 -S "function_name"
# Show who changed each line git blame file.txt
# Find commit that introduced bug git bisect 启动 git bisect bad git bisect good commit-哈希
Undoing Changes Working directory # Discard changes in file git 恢复 file.txt git 检查out -- file.txt # Old way
# Discard all changes git 恢复 .
Staging area # Unstage file git 恢复 --staged file.txt git re设置 HEAD file.txt # Old way
# Unstage all git re设置
Commits # Undo last commit (keep changes) git re设置 --soft HEAD~1
# Undo last commit (discard changes) git re设置 --hard HEAD~1
# Revert commit (创建 new commit) git revert commit-哈希
# Re设置 to specific commit git re设置 --hard commit-哈希
Stashing # Stash changes git stash
# Stash with message git stash save "Work in 进度"
# 列出 stashes git stash 列出
# 应用ly latest stash git stash 应用ly
# 应用ly and 移除 stash git stash pop
# 应用ly specific stash git stash 应用ly stash@{2}
# 删除 stash git stash drop stash@{0}
# Clear all stashes git stash clear
Rebasing # Rebase current branch git rebase mAIn
# Interactive rebase (last 3 commits) git rebase -i HEAD~3
# Continue after resolving conflicts git rebase --continue
# Skip current commit git rebase --skip
# Abort rebase git rebase --abort
Tags # 列出 tags git tag
# 创建 lightweight tag git tag v1.0.0
# 创建 annotated tag git tag -a v1.0.0 -m "Version 1.0.0"
# Tag specific commit git tag v1.0.0 commit-哈希
# Push tag git push origin v1.0.0
# Push all tags git push --tags
# 删除 tag git tag -d v1.0.0 git push origin --删除 v1.0.0
Advanced Operations Cherry-pick # 应用ly specific commit git cherry-pick commit-哈希
# Cherry-pick without committing git cherry-pick -n commit-哈希
Sub模块s # 添加 sub模块 git sub模块 添加 https://github.com/user/repo.git path/
# 初始化 sub模块s git sub模块 init
# 更新 sub模块s git sub模块 更新
# Clone with sub模块s git clone --recursive https://github.com/user/repo.git
清理 # Preview files to be 删除d git 清理 -n
# 删除 un追踪ed files git 清理 -f
# 删除 un追踪ed files and directories git 清理 -fd
# Include ignored files git 清理 -fdx
Common 工作流s
Feature branch 工作流:
git 检查out -b feature/new-feature # Make changes git 添加 . git commit -m "添加 new feature" git push -u origin feature/new-feature # 创建 PR, then after merge: git 检查out mAIn git pull git branch -d feature/new-feature
Hotfix 工作流:
git che