Git学习使用


Git的定义中,共有四个分区

image-20201226172111332

  1. 学习链接:

     B站:https://www.bilibili.com/video/BV1tf4y1e7yt?p=1
  2. 从远程仓库下载项目

    git clone https://github.com/wsdassssss/Github-test.git
  3. 将修改后文件加到缓存区中

    git add record.txt
  4. 将缓存区文件提交到本地仓库

    git commit -m "first change"
  5. 将本地仓库文件推送到远程仓库

    git push
  6. 从远程仓库拉取

    git pull
  7. 修改后,未提交到缓存区,恢复原文件

    git restore record.txt
  8. 查看工作区文件和缓存区文件内容差异

    git diff
    git diff --stat 显示摘要
  9. 查看文件变化

    git status 
  10. 取消上一次git add

   git reset HEAD
  1. 查看提交历史

    git log
  2. 删除

    git  rm 
    git rm --cached test3.txt #将test3.txt从缓存区删除
    git rm  -f test3.txt #将test3.txt从缓存区和工作区删除
  3. 新建仓库

    git init
  4. 提交第一个版本到版本库,第一代,v1.0

    git add first.txt
    git commit -m 'v1.0'
  5. 提交第二个版本到版本库,新增功能,v2.0

    git add second.txt
    git commit -m 'v2.0'
  6. 提交第三个版本到版本库,新增功能v2.2,但需要回退到v2.0,再回退到V2.2

    #提交版本v2.1
    git add .
    git commit -m 'v2.2' 
    #回退到V2.0
    git log #获取v2.0的版本标识  
    git reset --hard 9c7c82b0880e30a430bb506d9098d49362e8a6fa
    #执行成功,回退到v2.0,git log 无V2.2记录
    
    #再回到V2.2
    git reflog
    git reset --hard b2d80c3
  7. 简略图

    image-20201227222054748

  8. 修改文件后,放弃修改,在git add 之前

    git checkout -- first.txt
  9. git add之后想要回退文件

    git reset HEAD
  10. 分支

    #新建功能分支
    git branch func1 #新建func1分支
    git checkout fun1 #切换到func1分支
    nano first.txt #添加 3.house working
    git add 'first.txt'
    git commit -m 'new func1 branch'
    git checkout main
    #分支每次切换到main之前要先commit,否则文件内容会停在前面分支
    
    #修复BUG分支
    git branch bug1
    git checkout func1
    nano first.txt #修改 2.house--bug
    git add .
    git commit -m 'bug finish'
    git checkout main
    
    #main分支merge分支bug1
    git merge bug1 #first.txt line2: 2.drink--bug
    #合并bug分支完成
    
    #功能func1完成,合并分支func1 
    
    #main   first.txt 
    1.phoyo
    2.drink-bug
    #func1  first.txt
    1.phoyo
    2.drink
    3.house
    
    git merge func1
    #会引发冲突,因为main分支line2和func2分支line2不同,需要手动处理,再进行提交
    #示例如下图所示
    

    image-20201228002303518

  11. Github

    git remote add test[name] *************************.git #添加远程仓库
    git push -u test[name] main[branch]
    
    #本地仓库和远程仓库是独立仓库,关联需要先执行
    git pull test main --allow-unrelated-histories
    git push test main
    
    #推送func1分支
    git push -u github_test[name] func1[branch]
    
    #从远程仓库下载
    git clone ***********************.git
    #切换分支
    git checkout func1
    #修改first.txt
    ADD: 4.food
    
    git add first.txt
    git commit -m '4.fodd'
    git push
    #再去网页查看,func1分支已经更新
  12. Git rebase,让提交更加简洁

    git rebase -i HEAD~3 #合并最近三次的commit
  13. Git log

    git log --graph
    git log --graph --pretty=format:"%h %s"
  14.  

    git init
    git remote add qt_work **.git
    git pull qt_work
    git pull