git命令常用命令汇总

Git 常用命令汇总

配置相关

git config --global user.name "Your Name"          # 设置全局用户名
git config --global user.email "your@email.com"   # 设置全局邮箱
git config --list                                 # 查看当前配置

仓库操作

git init                                         # 初始化新仓库
git clone <repository_url>                       # 克隆远程仓库
git status                                       # 查看仓库状态
git remote -v                                    # 查看远程仓库信息
git remote add origin <repository_url>           # 添加远程仓库

基本工作流

git add <file>                                   # 添加文件到暂存区
git add .                                        # 添加所有修改到暂存区
git commit -m "commit message"                   # 提交更改
git commit -am "commit message"                  # 添加并提交所有已跟踪文件的修改
git diff                                         # 查看未暂存的修改
git diff --staged                                # 查看已暂存但未提交的修改
git restore <file>                               # 撤销工作区的修改(未add)
git restore --staged <file>                      # 撤销暂存区的修改(add后)

分支管理

git branch                                       # 查看本地分支
git branch <branch_name>                         # 创建新分支
git checkout <branch_name>                       # 切换到分支
git checkout -b <branch_name>                   # 创建并切换到新分支
git merge <branch_name>                          # 合并分支到当前分支
git branch -d <branch_name>                      # 删除分支
git branch -m <old_name> <new_name>              # 重命名分支

远程操作

git fetch                                        # 从远程获取最新版本到本地
git pull                                         # 拉取远程更改并合并
git pull origin <branch_name>                    # 拉取指定分支的更改
git push                                         # 推送本地提交到远程
git push origin <branch_name>                    # 推送分支到远程
git push -u origin <branch_name>                 # 推送并设置上游分支
git push origin --delete <branch_name>           # 删除远程分支

撤销与重置

git reset --soft HEAD~1                          # 撤销最后一次提交(保留更改)
git reset --mixed HEAD~1                         # 撤销提交和暂存(默认)
git reset --hard HEAD~1                          # 彻底撤销最后一次提交
git revert <commit_hash>                         # 创建一个撤销某次提交的新提交

日志与历史

git log                                          # 查看提交历史
git log --oneline                                # 简洁版提交历史
git log --graph                                  # 图形化显示分支历史
git log -p                                       # 显示每次提交的差异
git show <commit_hash>                           # 显示某次提交的详细信息

标签管理

git tag                                          # 列出所有标签
git tag <tag_name>                               # 创建轻量标签
git tag -a <tag_name> -m "message"               # 创建附注标签
git push origin <tag_name>                       # 推送标签到远程
git push origin --tags                           # 推送所有标签到远程

储藏与清理

git stash                                        # 储藏当前工作目录的修改
git stash list                                   # 查看储藏列表
git stash apply                                  # 恢复最近的储藏
git stash drop                                   # 删除最近的储藏
git clean -fd                                    # 删除未跟踪的文件和目录

其他实用命令

git cherry-pick <commit_hash>                    # 应用某次提交到当前分支
git rebase <branch_name>                         # 变基当前分支到目标分支
git reflog                                       # 查看所有操作记录
git blame <file>                                 # 查看文件的修改历史
git grep "pattern"                               # 在代码库中搜索内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值