• Git常用命令diff和mv


    Git常用命令diff和mv

    1、diff

    # 查看工作区和暂存区所有文件的对比
    # 该命令可以显示尚未添加到stage的文件的变更
    $ git diff
    
    • 1
    • 2
    • 3
    # 查看工作区和暂存区单个文件的对比
    $ git diff file
    
    • 1
    • 2
    # 显示暂存区和上一个commit的差异
    # 查看暂存区与指定提交版本的不同,版本可缺省为HEAD
    $ git diff --cached commit
    
    • 1
    • 2
    • 3
    # 显示工作区与当前分支最新commit之间的差异
    $ git diff HEAD
    # 比较与上一个版本的差异
    $ git diff HEAD^
    
    • 1
    • 2
    • 3
    • 4
    # 显示今天你写了多少行代码
    $ git diff --shortstat "@{0 day ago}"
    
    • 1
    • 2
    # 显示添加到stage的文件与当前最新版本之间的差异
    # 该命令可以显示添加到stage的文件与当前最新版本之间的差异
    $ git diff --staged
    
    • 1
    • 2
    • 3
    # 查看工作区和暂存区所有文件的对比,并显示出所有有差异的文件列表
    # 注意
    # 1.你修改了某个文件,但是没有提交到暂存区,这时候会有对比的内容,一旦提交到暂存区,就不会有对比的内容(因为暂存区已经更新)
    # 2.如果你新建了一个文件,但是没有提交到暂存区,这时候diff是没有结果的
    $ git diff --stat
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 查看暂存区与上次提交到本地仓库的快照(即最新提交到本地仓库的快照)的对比
    $ git diff --cached/--staged
    
    • 1
    • 2
    # 查看工作区与上次提交到本地仓库的快照(即最新提交到本地仓库的快照)的对比
    $ git diff branchname
    
    • 1
    • 2
    # 查看两个本地分支中某一个文件的对比
    $ git diff branchname..branchname filename
    
    • 1
    • 2
    # 查看两个本地分支所有的对比
    # 该命令可以显示两个分支之间的差异
    $ git diff branchname..branchname
    
    • 1
    • 2
    • 3
    # 查看远程分支和远程分支的对比
    $ git diff origin/branchname..origin/branchname
    
    • 1
    • 2
    # 查看远程分支和本地分支的对比
    $ git diff origin/branchname..branchname
    # 比较远程分支master上有本地分支master上没有的
    $ git diff origin/master..master                            
    
    • 1
    • 2
    • 3
    • 4
    # 只显示差异的文件,不显示具体内容
    $ git diff origin/master..master --stat
    
    • 1
    • 2
    # 查看工作区与指定提交版本的不同
    $ git diff commit
    
    # 查看两个commit的对比,其中任一可缺省为HEAD
    $ git diff commit1..commit2 
    
    # 查看2个不同分支指定提交版本的不同,其中任一可缺省为HEAD
    $ git diff commit...commit
    # 该命令相当于
    $ git diff $(git-merge-base A B) B
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    # 比较与HEAD版本lib目录的差异
    git diff HEAD -- ./lib
    
    • 1
    • 2

    2、mv

    修改暂存区文件。

    # 修改文件名字,并且将这个改名放入暂存区
    $ git mv old_file_name new_file_name
    
    • 1
    • 2
    # 文件必须纳入版本控制才可以执行git mv
    $ touch old_file.txt
    
    $ git mv old_file.txt new_file.txt
    fatal: not under version control, source=old_file.txt, destination=new_file.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # git add之后修改
    $ ls
    a.txt  b.txt  c.txt  d.txt  f.txt  new.txt  test.txt
    
    $ touch old_file.txt
    
    $ git add old_file.txt
    
    $ git mv old_file.txt new_file.txt
    
    $ git status
    On branch branch_a
    Your branch is up-to-date with 'origin/branch_a'.
    
    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
            new file:   new_file.txt
    
    
    $ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.txt  new_file.txt
    
    $ git commit -m "rename file"
    [branch_a b54a03a] rename file
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 new_file.txt
    
    $ git status
    On branch branch_a
    Your branch is ahead of 'origin/branch_a' by 1 commit.
      (use "git push" to publish your local commits)
    
    nothing to commit, working tree clean
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    # git commit之后修改
    $ ls
    a.txt  b.txt  c.txt  d.txt  f.txt  new.txt  test.txt
    
    $ touch old_file.txt
    
    $ git add old_file.txt
    
    $ git commit -m "add old_file.txt"
    [develop 360e9c5] add old_file.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 old_file.txt
    
    $ git mv old_file.txt new_file.txt
    
    # 是执行完git add命令之后的结果
    $ git status
    On branch develop
    Your branch is ahead of 'origin/develop' by 1 commit.
      (use "git push" to publish your local commits)
    
    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
            renamed:    old_file.txt -> new_file.txt
            
    $ ls
    a.txt  b.txt  c.txt  d.txt  f.txt  new.txt  new_file.txt  test.txt
    
    $ git commit -m "rename file"
    [develop a0ab796] rename file
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename old_file.txt => new_file.txt (100%)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    算法竞赛入门【码蹄集新手村600题】(MT1260-1280)C语言
    Java基础进阶List-ArrayList集合
    GitLab CI/CD关键词(十三):创建下游流水线 trigger,引入流水线模板 include
    【并发编程五】c++进程通信——信号量(semaphore)
    SecretFlow学习指南(2)学习路径
    LeetCode每日一题(1383. Maximum Performance of a Team)
    【论文笔记之 YIN】YIN, a fundamental frequency estimator for speech and music
    怎么在idea中搭建一个maven项目?
    Vue2:网易云播放音乐并实现同步一次显示一行歌词
    链表算法题
  • 原文地址:https://blog.csdn.net/qq_30614345/article/details/130876008