Git常用命令

我的GitHub

创建版本库

//初始化本地版本库
git init

//克隆远程版本库到本地
git clone <url>

修改和提交

//跟踪文件
git add .    //跟踪所有改动过的文件
git add <file>    //跟踪指定的文件
 //提交
git commit -m "commit message"    //提交跟踪过的文件并提示
git commit --amend //修改最后一次提交
 //查看变更内容
git diff
 //查看状态
git status
 //删除
git rm <file>    //删除工作区的文件
git rm --cached <file> //停止跟踪文件但不删除
 //文件改名
git mv <old> <new>

查看提交历史

//查看提交历史
git log    //查看所有提交历史
git log -p <file>    //查看指定文件的提交历史
git log --pretty=oneline    //一行显示历史记录

//查看所有提交记录(含版本号)
git reflog

//以列表方式查看指定文件的提交历史
git blame <file>

撤销

//撤销工作目录中所有未提交文件的修改内容
git reset --hard HEAD^    //有多少个^返回多少版本
git reset --hard HEAD~<number>   //返回前第<number>个版本 
git reset --hard <commit_id>    //返回<commit_id>这个版本
git reset HEAD <file>    //把暂存区的修改撤销掉,重新放回工作区

//撤销未`git add`的文件
git checkout -- <file>    //用版本库里的版本替换工作区的版本
git checkout HEAD <file>    //撤销指定的未提交文件的修改内容

//还原
git revert <commit_id>    //撤销指定的提交

分支与标签

//分支
git branch   //显示所有本地分支
git branch <branch_name>   //创建<branch_name>分支
git branch -d <branch_name>   //删除<branch_name>分支

//切换
git checkout <branch_name>   //切换到<branch_name>分支
git checkout <tag_name>   //切换到<tag_name>标签

//标签
git tag   //显示所有本地标签
git tag <tag_name>   //基于最新提交创建标签
git tag -d <tag_name>   //删除<tag_name>标签

合并与衍合

//合并
git merge <branch_name>   //"暴力"合并指定分支到当前分支,不管有无差别

//衍合
git rebase <branch_name>   //一件件衍合指定分支到当前分支,有差别时提示

远程操作

//远程库
git remote add <remote> <url>   //添加远程库
git remote -V   //查看远程版本库信息
git remote show <remote>   //查看指定远程版本库信息

//从远程库获取代码
git fetch <remote>

//下载代码及快速合并
git pull <remote> <branch>

//上传代码及快速合并
git push -u <remote> <branch>   //使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push
git push <remote> <branch>   //将本地master分支推送到<remote><branch>分支
git push <remote>:<branch/tag>   //删除远程分支或标签
git push --tags   //上传所有标签

删除全局配置

git config --unset --global <config>

你可能感兴趣的:(git,常用命令)