Git从入门到精通

Git

常用命令

  • 初始化仓库,查询仓库状态
git init
git status
  • git配置
git config --global alias
git config --global user.name "usertest"
git config --global user.email "test@test.com"
git config --global push.default simple
git config --global core.quotepath false
ssh -T git@git.coding.net
#查看git配置信息
git config --system -l
# 清除git认证信息,linux与mac os系统需要使用sudo
git config --system --unset credential.helper
  • 远程下载
git clone http://github.com/changelit/doc.git
git clone project_name new_project
  • 管理修改
git add .            //将所有文件及自目录文件添加到本地仓库     
git rm abc.html  //删除文件
git mv abc.html cc.html     //重命名文件
git commit -m ""  //提交注释
git push             //推送到git服务器
git diff
  • 版本回滚
git log -oneline
git reset -hard
  • 分支管理
git checkout
git checkout -b
git branch
git branch -D
git merge

进阶常用命令

  • 强行覆盖
#强行覆盖本地的文件
git checkout .
#强行推送
git push --force

大师之路

###对象库

  • git 中将数据分为4中对象类型:blob,tree,commit,tag

  • git 使用160位SHA1哈希对象,将哈希值存储在.git/objects目录中,保持数据的唯一性,相同的哈希值将会指向同一个blob块

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hILv2AP8-1611652161672)(object.jpg)]

索引

  • 索引三种状态:untracked,change to be commited, changes not staged

在这里插入图片描述
在这里插入图片描述

git ls-files -s      //查询文件对应的BLOB
git ls-files --stage             //查询在暂存区中的文件
git cat-files -p <object_id>     //查询对象的内容 
git rev-parse <6bit-id>        //将6位id解析成20位完整哈希值
#git commit 组成
git write-tree                 //根据索引更改来生成新树对象
git commit-tree  <object-id>    //id使用上文生成的树对象ID
#git diff 
git diff  [<blob-id> <blod-id>]                   //查看未追踪的文件差异
git diff --cached                                    //查看暂存区的文件差异
git rm                                          //删除文件
git rm --cached                             //从暂存区中删除,但不删除真实文件,索引区状态变更为untracked
git commit --all                             //not staged 状态的也会被提交

提交

#修改上一次提交的备注信息,进入交互模式
git commit --amend
#查询提交的记录
git show-branch --more=35
  • 父提交与祖父提交的标示

~1 代表父提交,~2代表祖父提交
^ 代表第一个父提交,^2代表第二个父提交

# 查询master提交日志,缩略为一行
git log master --pretty=oneline

分支

#show branche. * is current branche 
git branch
git checkout  
git branche <newbeanche> [starting-commit] 
 #force to switch 
git checkout-f
#show other branch file 
git show dev:newfile 
#show branch commit log
git show-branch --more=<n>
#create new branch and checkout 
git checkout -b newfeature
#从master中检出一个路径到当前分支
git checkout master -- foo.c

###diff

# work dir and index area
git diff
# index and Indicated commit
git diff –cached
# difference between two commit
git diff <commit> <commit>
# show statestics data
git diff –stat
# show difference between two branch
git diff <branch>  <branch>
#search from latest 50 commit
git diff -S”” master~50

###修改提交

rebase

#rebase current branch to master HEAD
#如果分支上有子分支进行了rebase,则子分支将的父级将变成分支父级的分支
#对已有合并的分支进行rebase则需要增加--preserve-merges 参数
git rebase master
#migrate newbranch to master branch
git rebase --onto master branch^ newbranch

reset

#回退到前一个提交,并且保持索引不变(changes to be commit),工作目录文件保持不变,用于修改提交备注信息
git reset --soft HEAD^
#默认状态,更改索引区(not staged),工作目录文件不变
git reset --mixed HEAD^
#直接回退到前一个提交的状态,工作目录文件也随之改变
git reset --hard HEAD^

cherry-pick & revert

#将一次提交应用到当前分支上
git cherry-pick dev^
#将一连串的提交应用到当前,如果解决所有冲突则使用--continue 或者使用--abort 放弃应用提交
git cherry-pick dev~3..dev
#撤销历史中的一个错误提交,实际上是将历史的一个提交在当前状态逆转
git revert master~3

###日志查询

#查看提交日志,并简化输出为一行
git log --oneline 
#查看所有的日志,包含回退后的提交日志
git reflog 
#查看指定日期的日志
git log 'dev@{2 days ago}'  

疑难

  • 使用用户名登陆后,无法忘记用户名,解决如下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值