git mv与直接mv的区别

git mv

行为:

  1.创建一个和之前文件内容一样的文件,文件名为新的文件名

  2.将原来的文件删除

  3.将删除的文件添加到暂存区

  4.将新建的文件添加到暂存区

$ git mv a a1

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

  renamed: a -> a1

提交:

  直接 git commit -m ''

$ git commit -m 'rename a to a1'

[master 863356d] rename a to a1
  1 file changed, 0 insertions(+), 0 deletions(-)
  rename a => a1 (100%)

$ git status
  On branch master
  nothing to commit, working directory clean

恢复:

  1. 恢复暂存区(git reset HEAD oldName)

  2. 将新添加的文件从暂存区移除(git reset HEAD newName)

  3. 将原来的文件从暂存区恢复到工作区(git checout -- oldName)

  3. 从工作区删除新添加的这个文件(rm newName)

$ git reset HEAD a
Unstaged changes after reset:
D       a

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   a1

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    a


$ git reset HEAD a1
Unstaged changes after reset:
D       a

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    a

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a1

no changes added to commit (use "git add" and/or "git commit -a")

$ git checkout -- a

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a1

nothing added to commit but untracked files present (use "git add" to track)

$ rm a1

$ git status
On branch master
nothing to commit, working directory clean

 

直接调用系统的mv

行为:

  只是重命名了一个文件

$ mv a a1

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    a

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a1

no changes added to commit (use "git add" and/or "git commit -a")

提交:

  1.把新文件和旧文件加入暂存区

  2.提交暂存区的改动

$ git add a a1

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    a -> a1


$ git commit -m 'rename a to a1'
[master 8b02e6a] rename a to a1
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a => a1 (100%)

$ git status
On branch master
nothing to commit, working directory clean

恢复:

  1.将旧文件恢复到工作区,git checout -- oldName

  2.将新文件删除, rm newName

$ git checkout -- a
$ rm a1

$ git status
On branch master
nothing to commit, working directory clean

 

转载于:https://www.cnblogs.com/413xiaol/p/10555165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
git reset 和 git revert 都是用于取消 Git 提交的操作,但它们的实现方式和效果有所不同。 git reset 是通过将分支指针移动到指定的提交来撤销提交。它有三种模式:soft、mixed 和 hard。 - git reset --soft 只移动分支指针,不修改工作区和暂存区的内容。这意味着你可以撤销最近的提交,并将修改保留在暂存区中,以便重新提交。 - git reset --mixed 是默认模式,它会将分支指针移动到指定的提交,并且会取消暂存区的内容。这意味着你可以撤销最近的提交,并将修改保留在工作区中,以便重新提交。 - git reset --hard 是最彻底的模式,它会将分支指针移动到指定的提交,并且会取消所有修改,包括工作区和暂存区。这意味着你可以完全撤销最近的提交及其修改。 相比之下,git revert 是通过创建一个新的提交来撤销指定提交的更改。它会在 Git 历史记录中创建一个新的提交,该提交会反转指定提交的更改内容。这样做的好处是可以保留历史记录的完整性,因为每个撤销操作都会生成一次新提交。 总结一下: - git reset 是直接移动分支指针来撤销提交,可以修改工作区和暂存区的内容。 - git revert 是创建一个新提交来撤销指定提交的更改,保留历史记录完整性。 使用哪个命令取决于你的需求和意图。如果你只是想撤销最近的提交并重新提交修改,可以使用 git reset。如果你希望保留历史记录完整性,并且希望以撤销提交的方式来反转更改,可以使用 git revert。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值