[TOC] #### 1. 用户配置 ---- 用户名和邮箱(只是用在提交记录中的显示,和是否有权限提交代码无关,这是很多人的误区) ``` # 全局配置用户名和邮箱 git config --global user.name "辰风沐阳" git config --global user.email "23426945@qq.com" ``` 查看配置 ``` # 查看全局配置的用户名和邮箱 git config --global user.name git config --global user.email # 也可以使用 cat ~/.gitconfig ``` #### 2. 命令帮助 ---- ``` # 查看命令全面手册 git help <command> git <command> --help # 只查看命令的参数选项 git <command> -h ``` #### 3. 流水线操作 ---- ``` # 初始化仓库 git init # 将工作区文件添加到暂存区 git add . # 将暂存区文件提交到版本库 git commit -m 'first commit' # 本地库关联远程仓库 git remote add origin https://gitee.com/holyking/test-4.git # 将代码推送到远程仓库 git push -u origin master ``` #### 4. 添加到暂存区 ---- ``` # 将工作区所有文件添加到暂存区 git add . git add -A # 将工作区已被追踪的文件添加到暂存区 git add -u # 删除工作区文件,并将这次删除放入暂存区 git rm [file1] [file2] ... # 停止追踪指定文件,但该文件会保留在工作区 git rm --cached [file] ``` #### 5. 提交到版本库 ---- ``` # 将暂存区文件提交到版本库 git commit -m <message> # 修正上次提交操作,同时也会将暂存区文件提交到版本库 git commit --amend -m <message> ``` #### 6. 远程仓库配置 ---- **语法格式** ``` # 添加远程仓库配置 # url 是 git 远程库地址,name 是给 url 起的别名 git remote add <name> <url> # 修改远程仓库地址 git remote set-url <name> <newurl> # 删除远程仓库配置 git remote remove <name> ``` **使用示例** ``` # 添加远程库 # 这是平时使用最多的方式,习惯上大家都将远程库的别名设置为 origin git remote add origin https://gitee.com/holyking/test-4.git ``` #### 7. 拉取远程仓库 ---- **将远程库拉取到本地** ``` # 拉取远程库的默认分支 git clone <url> # 拉取远程库的指定分支 git clone -b <branch> <url> # 拉取远程库到指定目录 git clone <url> <directory> ``` **拉取方式: https 方式** ``` # 永久记住密码 git config --global credential.helper store # 拉取远程库时会让输入代码托管平台的账号和密码 git clone https://gitee.com/holyking/test.git # git 会将输入的账号密码存储在 /.git-credentials 文件中 $ cat ~/.git-credentials https://23426945%40qq.com:liang666@gitee.com # 删除密码 git config --global --unset credential.helper ``` **拉取方式: ssh 方式** ```bash # 生成 ssh 公钥 ssh-keygen -t rsa # 生成的公钥保存在 ~/.ssh/id_rsa.pub 文件中 cat ~/.ssh/id_rsa.pub # 将生成的公钥配置到代码托管平台,然后使用 ssh 方式拉取仓库即可 git clone git@gitee.com:holyking/test.git ``` #### 8. 分支管理命令 ---- `git branch` 分支管理 ``` # 查看本地和远程分支 git branch -avv # 创建分支 git branch <branch> # 删除分支 git branch -d <branch> # 强制删除分支 git branch -D <branch> # 远程仓库删除了某个分支,本地仓库还存在该分支,使用以下两个命令任意一个都可以解决 git fetch --prune git remote prune origin ``` `git checkout` 分支管理 ``` # 切换分支 git checkout <branch> # 创建并切换分支 git checkout -b <branch> # 强制创建分支,然后切换分支 git checkout -B <branch> # 基于标签创建分支 git checkout -b <branch> <tagname> ``` `git push` 分支管理 ``` # 推送到远程分支 git push <remote> <branch> # 删除远程仓库分支 git push <remote> :<branch> ``` #### 9. 标签管理命令 ---- ``` # 查看标签 git tag # 创建标签 git tag v1.0.0 # 创建标签时添加描述 git tag v1.0.0 -m '正式上线' # 删除本地标签 git tag -d v1.0.0 # 删除远程仓库标签 git push origin :v1.0.0 # 将标签推送到远程仓库 git push origin v1.0.0 # 将本地所有标签推送到远程仓库 git push origin --tags # 查看远程仓库标签 git ls-remote --tags ```