Git 操作命令

环境配置

  • git config user.name your_name : 设置你的用户名, 提交会显示
  • git config user.email your_email : 设置你的邮箱
  • git config core.quotepath false : 解决中文文件名显示为数字问题

操作

基本操作

  • git init : 初始化一个 git 仓库
  • git add <filename> : 添加一个文件到 git 仓库中
    • git add  . : stages new and modified, without deleted
    • git add -u : stages modified and deleted, without new
    • git add -u : stages All
  • git rm
    • rm <filename> : 本地删除,暂存区未删除
    • git rm <filename> : 本地删除, 暂存区删除
    • git rm --cached <filename> : 本地未删除,暂存区删除
  • git checkout -- <file> : 还原未暂存(staged)的文件
  • git reset 
    • git reset HEAD <filename> : 取消暂存,那么还原一个暂存文件,应该是先 reset 后 checkout
    • git reset --hard <commit>: 替换引用的指向,替换暂存区,替换工作区
    • git reset --soft <commit>: 只更改引用的指向,不改变暂存区和工作区
  • git stash : 隐藏本地提交记录, 恢复的时候 git stash pop。这样可以在本地和远程有冲突的情况下,更新其他文件
  • git status : 查看你的代码在缓存与当前工作目录的状态
  • git log : 显示分支中提交更改的记录

  • git commit -m "commit message": 提交到本地
  • git push [remote-name] [branch-name] : 把本地的提交记录推送到远端分支
  • git pull: 更新仓库 git pull = git fetch + git merge

分支

  • git branch <branch-name> : 基于当前 commit 新建一个分支,但是不切换到新分支
  • git checkout -b <branch-name> : 新建并切换分支
  • git checkout <branch-name> : 切换分支
  • git branch -d <branch-name> : 删除分支
  • git push origin <branch-name> : 推送本地分支
  • git checkout -b <local-branch-name> origin/<origin-branch-name> : 基于某个远程分支新建一个分支开发
  • git checkout --track origin/<origin-branch-name> : 跟踪远程分支(创建跟踪远程分支,Git 在 git push 的时候不需要指定 origin 和 branch-name,其实当我们 clone 一个 repo 到本地的时候,master 分支就是 origin/master 的跟踪分支,所以提交的时候直接 git push)。
  • git push origin :<origin-branch-name> : 删除远程分支

比较不同

  • 查看变更还未载入(changed but unstaged)的文件比对: git diff
  • 查看载入(stage)而并未提交(commit)的变更: git diff --stage
  • 查看在最后一次提交之后的所有变更: git diff HEAD
  • 从一个特定点开始文件的修改情况: git diff v1.6.0 -- README
  • 两次提交的差异比对: git diff v1.0 v1.1
    • git diff v1.6.1.1 v1.6.1.2 --stat 两个版本之间差异的统计
    • git diff v1.6.1.1 v1.6.1.2 -- sha1_file.c 某个具体文件的变更比对

  • git commit -m "commit message": 提交到本地
  • git push [remote-name] [branch-name] : 把本地的提交记录推送到远端分支
  • git pull: 更新仓库 git pull = git fetch + git merge
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值