Git-threepart

1.如何修改commit的msg 信息 
    (1)修改最新的commit的msg
        git commit --amend

    (2)修改之前某一个的commit的msg
        git rebase   -- 变基
        git rebase -i <after-this-commit> : 
                -i 表示交互式
        修改之前某一个的commit = 基于前一个commit 进行修改当前的commit 
p, pick <commit> = use commit  =》 选择某个commit 
r, reword <commit> = use commit, but edit the commit message 重写某个commit msg 
s, squash <commit> = use commit, but meld into previous commit 合并多个commit msg

    (3)合并多个commit的msg 合并成一个commit msg
        git rebase -i <after-this-commit> 

2.工作区 vs 暂存区 vs 本地仓库 

    (1)查看文件差异 
    git status  => 文件的个数 名字

    (2)查看文件内容差异 
        所有文件内容差异:
         比较 不同工作区 
     git diff     工作区 vs 暂存区 文件内容差异
     git diff --cached  暂存区 vs 本地仓库 文件内容差异
         比较不同的commit 之间差异
             git diff 9cbef63 0aece3e
             git diff HEAD 0aece3e
             git diff HEAD HEAD~1
             git diff HEAD HEAD~2

             git diff HEAD HEAD~2 -- 2.log 1.log

             git diff a分支 b分支

    3.恢复操作 
        工作区 vs 暂存区 vs 本地仓库 
        (1)工作区 => 暂存区 
            git add 
                可以进行撤回
                git restore --staged <file>...
                git restore --staged 1.log 2.log 3.log
        (2)工作区:git add 之前 
                编写代码 也可以撤回
                git restore <file>...
                git restore 3.log

        (3)暂存区 vs 本地仓库 可以撤回
            git commit 之前: 
            git reset --hard HEAD  =>回退版本

3.删除文件 文件重命名
    git : 建议   相关命令 进行操作
    (1)删除/重命名文件 
        git rm/mv 
         git commit -m "Rm 11.log"

    (2)补充: 【了解】
        rm: git rm 
            rm -rf xxx 
            git add xxx 
            git commit -m "Rm xxx"

        mv : git mv 


4.紧急任务

 开发代码 => 线上bug =>commit =>开发代码 存起来 =>恢复出来 进行开发

    git stash 

    (1)开发代码
    $ vim 2.log

    (2)突然线上bug +解决bug
    git stash
    commit 

    (3)恢复 解决bug之前的开发代码
    git stash list
    git stash apply  补充 : --index <stash 下标>

5. .gitignore 
    通过这个文件 可以指定 哪些文件 不需要被git管控
    -- java 
        => xxx.java  => xxx.class
        => .idea 文件里面的文件 也是不需要被管控
    
    eg: 
        *.class  =》以.class文件结尾的 不需要被 git管控 
        log/
        

6.仓库备份:克隆
    git clone: 
        git clone  <repository> [<directory>]

        repository: 仓库
            本地仓库: path
            远程仓库:url 
    
    (1)本地仓库 备份到 本地 【不会用的】
    git clone /e/test/music/.git music_blk


    (2)远程仓库 备份到 本地 【常用的】
        远程仓库: 
            本地项目
            代码托管平台:github、gitlab、gitee

        git clone http://192.168.41.110/dl2262/dl2262.git

7.本地仓库 与 远程仓库 交互 
    本地仓库 连接到 远程仓库
        git remote: 
        git remote add <name> <URL>

        (1)连接远程仓库: 
        git remote add gitlab http://192.168.41.110/dl2262/test01.git
        (2)检查远程仓库的连接 
            git remote -v
        
        (3)本地仓库 push 远程仓库 
            git push  ==> 更新远程仓库的分支

            git push [--all ] => 本地所有分支 更新到远程仓库 分支
            git push [<repository> [<refspec>…​]]
                        =》更新 可以选择 哪个远程仓库 哪个分支


            eg: 
                git push --all 

$ git push --all

 * [new branch]      dev -> dev   =》 本地dev =》 远程 自动给远程 dev 分支
 ! [rejected]        master -> master (fetch first) 
error: failed to push some refs to 'http://192.168.41.110/dl2262/test01.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决:
    先拉下来:
        git fetch / git pull : 
            git fetch  [<repository> [<refspec>…​]] =》可以指定拉取哪个仓库哪个分支
            git fetch --all  => 拉取远程仓库 所有分支
        
        git fetch gitlab master  =>默认先把远程 分支拉下来 但是文件没有和 本地分支 进行合并 
        git merge 远程分支: 
            :
        git merge gitlab/master
            fatal: refusing to merge unrelated histories

        git merge --allow-unrelated-histories gitlab/master    
        git push gitlab master

场景: 
    1.多人协同开发 常用操作

        1.多人共同维护同一个分支 操作不同的文件 【不会有代码冲突问题】
            1.远程仓库创建一个分支 
                bigdata 
            2.准备两个 本地仓库 
            git clone http://192.168.41.110/dl2262/test01.git test01_blk

            3.zhangsan 开发项目 提交到 远程仓库 
            4.lisi开发项目 提交到 远程仓库
                1.git fetch 
                2.git merge 
                3.git push 

            注意: 
                1.拉取远程仓库代码 =》 本地仓库 和 远程仓库保持一致

                git pull  <=> git fetch  + git merge

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值