整理git常用操作

一、安装之后设置名字跟email

git config --global user.name "leocgl" git config --global user.email "leocgl@email.com"

二、常用操作命令

git config --list #检查已有配置信息 git init [file] #把所在目录变成git仓库 git add [file] #把文件提交到暂存区 git commit -m "txt" #把暂存区内容提交到仓库 git diff [file] #查看上次修改内容 git log #查看历史版本历史记录;加上参数--pretty=oneline精简显示 git reflog #查看每次命令记录 git reset --hard HEAD^ [版本号] #回退到上一个版本 git checkout -- file #丢弃工作区修改 本地删除>git rm file> git commit -m "rm file" #删除文件

三、远程仓库

(1)添加远程仓库 git remote -v #查看远程仓库信息 git remote add origin git@github.com:leocgl/test.git #关联github远程仓库 git push -u origin master #将本地库当前分支master的内容推送到远程;[-u参数:把本地的master分支与远程的master分支相关联] (2)克隆远程仓库 git clone git@github.com:leocgl/test.git

四、分支管理

git branch #列出所有分支,当前分支前会标注一个*号 git checkout -b test #创建test分支并切换到test分支上;加上-b选项相当于以下两条命令;等同于git switch -c test git branch test #创建test分支 git checkout test #切换到test分支;等同于git switch master git merge test #将test分支合并到当前分支 git branch -d test #删除test分支 git log --graph --pretty=oneline --abbrev-commit #查看分支的合并情况