zoukankan      html  css  js  c++  java
  • Git使用及原理

    Git使用及原理

    Git基础

    working directory

    stage/index

    local repository

    image-20210626205846287

    git init

    image-20210626133106138

    git add

    image-20210626141614149

    如果git add,文件会被tracked,此时修改文件,会出现Changes not staged for commit

    可见file1.txt虽然更新了,但是并没有被更新到暂存区。

    image-20210626142936898

    image-20210626143045116

    这时候的再次执行git add操作,会在暂存区生成一个新的git object,并且新的修改的记录也提交到了暂存区

    image-20210626143244157

    但是如果已经git add过,git commit会把修改直接commit到本地仓库

    git commit实际上是一次快照,记录一个版本的信息。

    image-20210626144348743

    此时对文件进行了修改,commit的还是上个版本的,stage里已经是最新的文件,需要commit才能把当前的文件作为快照保存起来。

    image-20210626144532859

    再次提交,发现多了个parent的信息,指向上一个commit

    image-20210626144726634

    git commit之后,文件还是会在暂存区,还是之前的ecd2文件,所以如果对文件进行了修改需要进行git add才能把修改提交到暂存区,git是无法追踪到文件的新修改的。

    image-20210626144935895

    可见object对象是被压缩过的,如果直接打开就是乱码,解压才能看到文本信息。

    image-20210626145309921

    git commit的同时会把文件更新到暂存区,git commit相当于要先add修改到暂存区,再commit

    image-20210626151903878

    如果没有add过,就不属于git管理,就无法进行commit

    image-20210626152116202

    如果一个文件没有被git add过,他就不受git的管理,所以file5在任何一个branch都是有的。

    而file1-4则会受到checkout branch的影响。

    image-20210626152945309

    image-20210626152958882

    命令
    git init 初始化git仓库,但是不会做任何操作,
    与git clone出来的不同
    git cat-file -p hash 查看git object的内容
    git ls-files --stage 查看暂存区的文件
    git add filename/* 添加文件/修改后的文件 到暂存区
    git commit filename -m "message" commit 生成版本的快照
    git status 查看git状态
    git rm --cache filename: 历史版本里还有,但是当前工作区没有,提交的时候也不会有
    git rm -f filename: 删除暂存区和工作区的文件

    image-20210626213104319
    撤销已提交的版本库,不会修改暂存区和工作区
    git reset --soft hash
    撤销已提交的版本库和暂存区,不会修改工作区
    git reset --mixed hash
    git reset --hard hash 工作区也会修改
    git rebase -i HEAD~3 合并多个版本
    pick => s
    在弹出来的vim界面,把下面两行改成s,就会把后两次压缩到第一行
    git rebase --abort
    https://segmentfault.com/a/1190000007748862
    image-20210627003413729image-20210627003608231
    git rebase -i HEAD~3 修改多个版本
    pick => s
    git commit --amend 分别修改对应的commit message 就可以实现批量修改commit message
    image-20210627002426214
    git commit --amend
    git commit --amend --no-edit
    修改message或者提交内容
    首先作出你想要补上的修改, 暂存它们,然后用 git commit --amend 以新的改进后的提交来 替换 掉旧有的最后一次提交
    git push -f origin feature:feature [本地] :[远程] 强制push到远程库
    git ls-tree -r HEAD image-20210627004247421

    image-20210627002616648

    image-20210627004320113

    image-20210627004424338

    本地分支

    image-20210626145839175

    在本地创建分支 git checkout -b branchname 创建并切换到分支。此时两个分支的文件内容是完全一致的。

    创建一个分支可以理解为新建了一个feature指针。指针指向了当前最后的一次commit,

    image-20210626150719009

    当在当前的feature分支进行的新的commit之后,feature的指针向后进行了移动,指向了新的commit。

    image-20210626151104269

    命令
    git log git操作的log git log参数
    image-20210626151503820
    git reflog
    branch 指针变化的logimage-20210626154032911
    git checkout -b branchname 创建并切换到分支
    git log --graph --oneline --all 查看所有分支image-20210626153920547

    如果此时checkout到master分支,就会切换到master分支所指向的那一个版本。

    image-20210626152350705

    image-20210626154711304

    image-20210626160013795

    image-20210626160029905

    再在master 快进merge到feature 此时都指向一个commit了

    image-20210626160407470

    远程分支

    git clone

    image-20210626161715871

    image-20210626161758713

    在自己本地的master分支工作,远程的master分支也会有更新,git fetch会获取新的数据

    git fetch

    image-20210626161902118

    git remote add origin https://github.com/paranoiddemon/test-git.git 添加远程仓库
    git push -u origin master:master
    git push -u origin master:main 后面是远程分支名,如果省略,就会创建一样名字的分支
    image-20210626175859322
    git stash
    git stash push
    git stash save "test-cmd-stash"
    git stash apply/pop
    git stash drop
    git stash list
    git stash show -p 显示差异
    git stash branch branchname
    git stash命令提供了参数用于缓存工作区和暂存区的文件。使用-u或者--include-untracked可以stash untracked文件。使用-a或者--all命令可以stash当前目录下的所有修改。
    git stash drop
    git stash clear
    https://www.cnblogs.com/tocy/p/git-stash-reference.html
    git checkout branchname
    image-20210626180655920
    image-20210626180607915
    git branch -vv 显示跟踪的分支 image-20210626185859572
    image-20210626190035518
    git push origin --delete feature 删除远程的feature分支
    git fetch + git merge 会比较好
    git fetch --all
    image-20210626190220966
    git branch branchname 创建分支
    git checkout branchname 切换到指定分支
    => git checkout -b branchname
    image-20210626190529040
    git fetch
    git merge origin/master fetch只是创建远程分支在本地的拷贝origin/master,再执行合并操作
    => git pull origin master:feature 把远程的master分支合并到本地的feature分支,如果是合并到本地的master分支 则可以省略 :master
    git checkout server +
    git rebase master
    =>
    git rebase master server :git rebase [basebranch] [topicbranch]

    切回到master 快进合并
    git checkout master
    git merge server
    image-20210626191654927
    git push --force origin feature:feature [localbranch]:[remotebranch] 强制覆盖服务器上的记录
    fast forward:
    git checkout master
    git merge hotfix
    git branch -d hotfix 删除分支

    git merge branchname :当前所在分支合并指定的分支
    如果产生冲突处理完要git add表示已经处理完冲突
    1
    image-20210626210425296
    git branch --merged: 如果已经合并了,不用的分支可以删掉 git branch -d testing
    git branch --no-merged
    image-20210626211148566
    git ls-remote image-20210626211335854
    git remote show image-20210713142933381
    image-20210626211436676
    git branch -r
    git checkout -b 本地分支名x origin/远程分支名x 使用该方式会在本地新建分支x,并自动切换到该本地分支x。
    git fetch origin 远程分支名x:本地分支名x 使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。
    git branch -vv
    git branch -u origin/branchname
    git branch --set-upstream-to origin/branchname
    建立本地分支和远程分支的映射 git push fetch的时候就知道对应的分支
    如果不建立映射的话,就需要手动指明
    image-20210713145040034

    git merge vs git rebase

    总的原则是,只对尚未推送或分享给别人的本地修改执行变基操作清理历史, 从不对已推送至别处的提交执行 变基操作,这样,你才能享受到两种方式带来的便利。

    工作目录下的每一个文件都不外乎这两种状态:已跟踪未跟踪。 已跟踪的文件是指那些被纳入了版本控制的文件,在上一次快照中有它们的记录,在工作一段时间后, 它们的状态可能是未修改,已修改或已放入暂存区。简而言之,已跟踪的文件就是 Git 已经知道的文件。

    工作目录中除已跟踪文件外的其它所有文件都属于未跟踪文件,它们既不存在于上次快照的记录中,也没有被放入暂存区。 初次克隆某个仓库的时候,工作目录中的所有文件都属于已跟踪文件,并处于未修改状态,因为 Git 刚刚检出了它们, 而你尚未编辑过它们。

    编辑过某些文件之后,由于自上次提交后你对它们做了修改,Git 将它们标记为已修改文件。 在工作时,你可以选择性地将这些修改过的文件放入暂存区,然后提交所有已暂存的修改,如此反复。

    image-20210626141354961

  • 相关阅读:
    【概念】using 三种使用方式
    2019-7-2 作业1 2 3
    异常
    java.lang.NullPointerException
    课外作业(建立double类型的小数,按照四舍五入保留2位小数)
    作业1.2.3.4
    左自增与右自增的区别
    深入了解JVM(Java虚拟机)
    Eclipse报错Could not resolve archetype
    ThinkPad E550 连蓝牙鼠标logitech M557
  • 原文地址:https://www.cnblogs.com/land-fill/p/14939652.html
Copyright © 2011-2022 走看看