git常用命令

1、配置用户名和邮箱

git config --global user.name "your name"
git config --global user.email "your email@email.com"
git config --list // 查看全局配置

--global 参数表示当前机器都采用这个配置

2、初始化仓库并提交到版本库

git init // 初始化仓库
git add <file> // 添加到暂存区
git commit  -m '描述信息' // 提交到本地版本库
git status // 查看本地仓库的状态
git diff <file> // 查看修改不同的地方
git rm --cached <file> // 从暂存区移除
git rm -f <file> // 连同文件也移除

3、查看历史记录

git log // 查看完整信息
git log --pretty=oneline // 单行
git log --graph --pretty=oneline // 图形化、单行
git reflog // 查看命令记录

4、重置版本及撤销

git reset --hard head^ // 回退到上一个版本
git reset --hard <commit id> // 回退到某个版本,不保留已经修改的内容
git reset <commit id> // 回退到某个版本,保留已经修改的内容
git reset head <file> // 将暂存区的提交撤销回工作区
git checkout --file // 撤销工作区内容,保持和暂存区或版本库一致
git push -f // 强推代码到远程

5、远程操作

git remote add origin 远程仓库地址 // 添加远程地址
git clone 远程地址 // 克隆
git push -u origin <branch name>// 第一次推送master分支
git push origin <branch name> // 往后每次推送master分支
git pull origin <branch name> // 拉取

6、分支

git branch <branch name> // 创建分支
git checkout <branch name> // 切换分支
git checkout -b <branch name> // 创建并切换分支
git branch // 查看分支
git merge <branch name> // 将制定分支合并到当前分支,merge可能导出当前分支时间线混乱,使用时候注意
git branch -d <branch name> // 删除分支
git branch -a // 查看远程分支
git push origin --delete <branch name> // 删除远程分支
git branch -m <old branch name> <new branch name> // 重命名分支

7、进度管理

git stash save 'message' // 保存进度
git stash list // 查看保存
git stash pop <stash id> // 恢复进度,并删除保存
git stash apply <stash id> // 恢复进度,不删除保存
git stash drop <stash id> // 删除进度
git stash clear // 清除所有进度

8、整理本地分支

git rebase // 未push的当前本地分支可以被整理成一条直线
git rebase <branch name> // 将某个分支合并到当前分支,并整理成一条直线,如果分支之间存在冲突,那么可以手动解决冲突,并继续rebase操作
git rebase --continue // 继续rebase操作
git rebase --abort // 放弃rebase操作,该操作会还原之前的冲突处理内容
git cherry-pick <commit id> // 将指定的提交提交到当前分支上

9、标签

git tag <tagName> // 创建一个标签
git tag -a <tagName> -m "备注"  // 创建一个有备注的标签
git push origin <tagName> // 将标签推送到远程
git tag -d <tagName> // 删除本地标签
git push origin :refs/tags/<tagName> // 删除远程标签
git tag // 查看所有标签

10、配置

git config --global credential.helper store // 记住账号密码,不用每次重新输入
原文地址:https://www.cnblogs.com/lay2017/p/9738134.html