git 常用指令
以下是 Git 最常用的指令分类整理,适合日常开发使用:
基础操作
命令 | 说明 |
---|---|
git init | 初始化新仓库 |
git clone <repo-url> | 克隆远程仓库 |
git add <file> | 添加文件到暂存区 |
git commit -m "消息" | 提交更改 |
git status | 查看工作区状态 |
git diff | 查看未暂存的修改 |
分支管理
命令 | 说明 |
---|---|
git branch | 查看本地分支 |
git branch <name> | 创建新分支 |
git checkout <branch> | 切换分支 |
git checkout -b <new-branch> | 创建并切换分支 |
git merge <branch> | 合并分支到当前分支 |
git branch -d <branch> | 删除分支 |
git branch -d -f <branch> | 强制删除本地分支 |
git push origin --delete <branch> | 删除远程分支 |
远程操作
命令 | 说明 |
---|---|
git remote -v | 查看远程仓库 |
git fetch <remote> | 下载远程更新(不合并) |
git pull | 拉取并合并远程更改(= fetch + merge) |
git push | 推送本地提交到远程 |
git push -u origin <branch> | 首次推送并设置上游分支 |
历史与日志
命令 | 说明 |
---|---|
git log | 查看提交历史 |
git log --oneline --graph | 简洁图形化历史 |
git blame <file> | 查看文件修改历史 |
git show <commit-id> | 查看某次提交详情 |
撤销与回退
命令 | 说明 |
---|---|
git restore <file> | 撤销工作区修改(Git 2.23+) |
git restore --staged <file> | 取消暂存(保留修改) |
git reset --soft HEAD~1 | 撤销提交但保留修改 |
git reset --hard HEAD~1 | 强制回退到前一次提交(慎用!) |
git revert <commit-id> | 创建新提交来撤销某次更改 |
标签管理
命令 | 说明 |
---|---|
git tag | 查看标签 |
git tag v1.0 | 创建轻量标签 |
git tag -a v1.0 -m "消息" | 创建含注释的标签 |
git push --tags | 推送所有标签到远程 |
高级工具
命令 | 说明 |
---|---|
git stash | 临时保存未提交的修改 |
git stash pop | 恢复暂存的修改 |
git rebase <branch> | 变基(重写提交历史) |
git cherry-pick <commit> | 复制特定提交到当前分支 |
git bisect | 二分查找定位问题提交 |
配置相关
命令 | 说明 |
---|---|
git config --list | 查看所有配置 |
git config user.name "姓名" | 设置用户名(当前仓库) |
git config --global user.email "邮箱" | 全局设置邮箱 |
典型工作流示例
# 克隆仓库
git clone https://github.com/user/project.git
# 创建功能分支
git checkout -b feature/login
# 开发并提交
git add .
git commit -m "实现登录功能"
# 推送到远程
git push -u origin feature/login
# 合并到主分支
git checkout main
git pull origin main
git merge feature/login
git push origin main
💡 提示:
- 使用
git <command> -h
查看命令帮助(如git add -h
)- 重要操作前先
git status
确认状态- 慎用
--force
/--hard
等破坏性操作
git设置用户名和邮箱地址
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
// 检查设置是否成功
git config --global user.name
git config --global user.email
gitee初始化
git init #初始化
git add . #将当前目录加入到git
git commit -m "first commit(提交的描述信息)" #git提交到本地版本库
git remote add origin https://gitee.com/xxxx.git #git本地库连接远程版本库,这一步会有对应输入账号和密码的操作
git push -u origin master #将文件上传到远程版本库master分支
git clone https://gitee.com/xxxx.git #获取git项目
合并分支
git checkout main // 换到目标分支
git merge my-branch // 合并分支
合并某一次特定的代码提交
git cherry-pick <commit-hash>
版本回退到某一次提交
git reset --hard <commit-hash>
git push -f // 强制推送
禁用HTTPS操作时的SSL验证
git config --global http.sslVerify false
提交规范
feat: 新功能(feature)
fix: 修补bug
docs: 文档(documentation)
style: 格式(不影响代码运行的变动)
refactor: 重构(即不是新增功能,也不是修改bug的代码变动)
chore: 构建过程或辅助工具的变动
revert: 撤销,版本回退
perf: 性能优化
test:测试
improvement: 改进
build: 打包
ci: 持续集成
相关报错
-
interactive rebase in progress; onto
执行:git rebase --continue
-
error: remote origin already exists.
执行:git remote rm origin
删除关联的origin的远程库
然后再:git remote add origin https://gitee.com/xxxxxx.git
关联自己的仓库
最后git push origin master
-
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://gitee.com/xiaoefen/vue3-web-worker.git’
执行:git pull --rebase origin master
然后再:git push origin master
GitLab上创建了新的分支,但在TortoiseGit上切换分支的时候发现找不到
进入自己的项目中点击右键->打开Git Bash Here
在命令框中输入git remote update origin --prune
更新远程分支列表
重新使用TortoiseGit就可以看见最新的分支了
Node
node v14.21.3(npm v6.14.18)装不上cnpm
指定装低版本的cnpm
npm install -g cnpm@7.1.0 --registry=https://registry.npmmirror.com/