git 简单命令

147 阅读1分钟

配置命令

// 设置用户名和email
git config --global user.name '你的名字'
git config --global user.email '你的邮箱'
// 配置ssh
创建密钥:$ ssh-keygen -t rsa -C “邮箱”
// 显示当前配置
git config --list

clone

git clone 项目地址

拉取分支

// 方式1
git fetch origin 远程分支:本地分支名称
git checkout 本地分支名称

// 方式2
git checkout -b 本地分支名称 origin/远程分支名称

代码提交

// 添加文件到暂存区
git add . //添加所有
git add [fileName] [fileName] ... //  添加指定文件

// 删除工作区文件,并且将这次删除放入暂存区
git rm [fileName] [fileName] ...

// 提交暂存区到仓库区
git commit -m "你的备注"

// 提交暂存区指定文件到仓库区
git commit [fileName] [fileName] ...  -m "你的备注"

代码信息

// 显示有变更的文件
$ git status

// 显示当前分支的版本历史
$ git log
// 显示过去5次提交
$ git log -5 --pretty --oneline
// 显示暂存区和工作区的代码差异
$ git diff
//  显示某次提交的元数据和内容变化
$ git show [commit]

代码撤销/回滚

// 回滚暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
// 回滚暂存区与工作区,与上一次commit保持一致
git reset --hard
// 回退到前3次提交之前,以此类推,回退到n次提交之前
git reset --hard HEAD~3
// 退到/进到 指定commit的sha码
git reset --hard commit_id  
// 回退到暂存区
git reset --soft ****
// 回退到工作区
git reset --mixed ****   

// 强推到远程
git push origin HEAD --force

代码同步

// 更新远程仓库
git remote update

// 同步远程仓库变动
git fetch
// 同步远程仓库变动,并与本地 分支合并
git pull [remote] [branch]

// 上传本地分支到远程仓库
git push
// 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force