Git使用操作学习笔记(源自周阳老师的教案)

记录了周阳老师的演示操作

  1. git init初始化本地的git仓库,并且touch hello.txt创建一个文件
    git status查看当前的状态,可以发现hello.txt文件还没被纳入管理
    在这里插入图片描述

  2. git add hello.txt之后再用git status查看状态,发现hello.txt已经放入cached缓存区
    在这里插入图片描述

  3. git commit -m "create hello.txt" 一定要加上一些描述信息,这个时候再git status发现缓存区和工作区都“干净”了(修改都已经提交同步到本地仓库)
    在这里插入图片描述

  1. vi hello.txt修改一下文件,git status发现git检测到了它被修改。
    在这里插入图片描述
  2. git add hello.txtgit commit -m "first update hello.txt"将这次的修改提交完成
    在这里插入图片描述
  3. vi hello.txt再修改一次文件,刚刚写了一行1,这次加一行2;
    同样 git add hello.txt放到缓存区,git commit -m "second update hello.txt"
111111
222222
  1. 然后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.txtgit commit -m "third update hello.txt"

  • 穿梭
    再增加4、5两行,修改两次提交,git log --pretty=oneline hello.txt查看提交记录
    在这里插入图片描述
  1. git reset --hard HEAD^返回到上一个版本cat hello.txt
    在这里插入图片描述
  2. git reset --hard HEAD^^^或者git reset --hard HEAD~3返回三次提交前
    此时git log也只能查看当前版本之前的提交
    在这里插入图片描述
  3. git reflog hello.txt来查看所有的版本变更信息
    在这里插入图片描述
  4. git reset --hard b97c71a回到五次更新后的版本
    在这里插入图片描述

reset有还有soft和mixed模式,详见这篇博客

  • 回撤
  1. vi hello.txt添加一行6
    在这里插入图片描述
  2. git checkout -- hello.txt可以撤回工作区的变更
    在这里插入图片描述
  • 回退
  1. vi hello.txt添加一行6,然后git add hello.txt,git status可以查看文件已经提交到了缓存区
    在这里插入图片描述

  2. git reset HEAD hello.txt,再次git status发现缓存区的修改被取消了
    在这里插入图片描述
    依旧git add hello.txtgit commit -m "sixth update hello.txt"

  • 删除
  1. 新建一个文件并提交到本地库
touch world.txt \
git add world.txt \
git commit -m "create file world.txt" world.txt
  1. 从本地库删除提交的文件 git rm -f world.tx

  1. git branch dev新建分支
  2. git checkout dev切换分支

git checkout -b可以将1和2一起操作

  1. git branch查看分支
    在这里插入图片描述
  2. vi hello.txt添加一行dev update 01,并git add hello.txtgit commit -m "dev update 01"

git checkout master切回master

  1. git merge dev合并分支
    在这里插入图片描述

如果内容冲突了的话,手动处理冲突的部分,然后git add hello.txt && git commit -m "conflict fixed"

  1. git branch -d dev可以将合并进来的dev分支删掉

猜你喜欢

转载自blog.csdn.net/weixin_44112790/article/details/110521461