记录了周阳老师的演示操作
起
-
git init
初始化本地的git仓库,并且touch hello.txt
创建一个文件
git status
查看当前的状态,可以发现hello.txt文件还没被纳入管理
-
git add hello.txt
之后再用git status
查看状态,发现hello.txt已经放入cached缓存区
-
git commit -m "create hello.txt"
一定要加上一些描述信息,这个时候再git status
发现缓存区和工作区都“干净”了(修改都已经提交同步到本地仓库)
承
vi hello.txt
修改一下文件,git status
发现git检测到了它被修改。
git add hello.txt
再git commit -m "first update hello.txt"
将这次的修改提交完成
vi hello.txt
再修改一次文件,刚刚写了一行1,这次加一行2;
同样git add hello.txt
放到缓存区,git commit -m "second update hello.txt"
111111
222222
- 然后
git log
从日志可以看到最近几次的提交。
git log --pretty=oneline hello.txt
可以按行显示
vi hello.txt
再添加一行3
5.git diff hello.txt
可以查看工作空间和本地库有什么区别
注意
git diff
使用
当工作区有改动,临时区为空,diff的对比是“工作区与最后一次commit提交的仓库的共同文件”;
当工作区有改动,临时区不为空,diff对比的是“工作区与暂存区的共同文件”。
git add hello.txt
并git commit -m "third update hello.txt"
转
- 穿梭
再增加4、5两行,修改两次提交,git log --pretty=oneline hello.txt
查看提交记录
git reset --hard HEAD^
返回到上一个版本cat hello.txt
git reset --hard HEAD^^^
或者git reset --hard HEAD~3
返回三次提交前
此时git log
也只能查看当前版本之前的提交
git reflog hello.txt
来查看所有的版本变更信息
git reset --hard b97c71a
回到五次更新后的版本
reset有还有soft和mixed模式,详见这篇博客
- 回撤
vi hello.txt
添加一行6
git checkout -- hello.txt
可以撤回工作区的变更
- 回退
-
vi hello.txt
添加一行6,然后git add hello.txt
,git status
可以查看文件已经提交到了缓存区
-
git reset HEAD hello.txt
,再次git status
发现缓存区的修改被取消了
依旧git add hello.txt
并git commit -m "sixth update hello.txt"
- 删除
- 新建一个文件并提交到本地库
touch world.txt \
git add world.txt \
git commit -m "create file world.txt" world.txt
- 从本地库删除提交的文件
git rm -f world.tx
合
git branch dev
新建分支git checkout dev
切换分支
git checkout -b
可以将1和2一起操作
git branch
查看分支
vi hello.txt
添加一行dev update 01
,并git add hello.txt
和git commit -m "dev update 01"
git checkout master
切回master
git merge dev
合并分支
如果内容冲突了的话,手动处理冲突的部分,然后
git add hello.txt && git commit -m "conflict fixed"
git branch -d dev
可以将合并进来的dev分支删掉