| 命令 | 说明 | 范例 |
|---|---|---|
| clone | 从远程仓库克隆 | git clone git@github.com:xxx.git |
| init | 创建空仓库或重新初始化现有仓库 | git init |
| add | 添加文件到暂存区 | git add <file> |
| commit | 保存暂存区的变更记录到工作区 | git commit -m "日志说明" |
| status | 查看文件变更状态 | git status |
| log | 查看提交日志记录 | git log |
| branch | 创建、删除分支,查看分支列表 | 查看:git branch -l 删除: git branch -D <branch_name>删除: git branch -D xxx |
| merge | 合并其他分支到当前分支 | git merge <branch_name> |
| switch | 切换分支,等价于checkout | git switch <branch_name> |
| pull | 拉取代码 | git pull origin <branch_name> |
| push | 推送代码 | git push origin <branch_name> |
| stash | 备份当前工作区内容 | git stash |
创建并切换分支
创建并切换分支,如下:
git checkout -b <branch_name>
//等价于
git switch -c <branch_name>
仅仅切换分支,如下:
git checkout <branch_name>
//等价于
git switch <branch_name>
本地仓库关联远程仓库
git init
git remote add origin git@github.com:xxx.git
git push -u origin master
撤销工作区的修改
git checkout -- <file>
撤销暂存区的修改记录
执行git add后,可以通过如下命令,撤销暂存区中的文件,保留修改的内容。
git rm --cached <file>
撤销修改记录
执行git commit后,可以通过如下命令,撤销提交记录,保留修改的内容。
git reset <hash>
回滚远程仓库的修改
执行git commit后,可以通过如下命令,撤销提交记录,回滚到上一个版本。
git reset --hard <hash>
修改提交的日志内容
当执行git commit后发现提交信息写错了,可以增加--amend参数修改内容
git commit --amend -m "新的日志信息"
.gitignore不生效问题
$ git rm -r --cached .
$ git add .
$ git commit -m 'update .gitignore'
本文提供了Git的常用命令总结,包括从远程仓库克隆、创建仓库、文件管理、分支操作、代码拉取与推送等核心功能。同时,还介绍了如何解决.gitignore不生效的问题,以及如何撤销修改、回滚版本和修改提交信息。
9493

被折叠的 条评论
为什么被折叠?



