`git branch -f< branch_name>之间的区别< hash>`和`git checkout< branch_name>; git reset --hard< hash>`在干净的工作树下? [英] Difference between `git branch -f <branch_name> <hash>` and `git checkout <branch_name>; git reset --hard <hash>` under a clean working tree?

查看:1378
本文介绍了`git branch -f< branch_name>之间的区别< hash>`和`git checkout< branch_name>; git reset --hard< hash>`在干净的工作树下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我一直使用 git checkout< branch_name>; git reset --hard< hash> 将分支移回到先前的提交。

然后我碰到这个问题,但答案和评论并没有详细解释它们之间的差异。



假设我有一个干净的工作树,那么它们之间有什么内在的区别?

  git branch -f< branch_name> <散列> 

  git checkout< branch_name> 
git reset --hard< hash>

并且做这样的区别,如果有的话,对于高级用法有任何微妙的含义?

解决方案

主要区别在于: git branch -f< branchname> < commitref> 移动< branchname> 指向指定的提交而不触及 HEAD 索引工作副本,而 git checkout< branchname> &安培;&安培; git reset --hard< commitref> 修改了所有这三项。



如果要快速重新排列分支而不移动 HEAD 或者修改你当前的工作树,那么 git branch -f 是一个很好的方法。如果您使用 git checkout ,那么它也可以工作,如果您有未提交的更改



另一个区别与性能有关,但它只与 大型项目相关。

在这些情况下,使用<$ c $修改您的工作树c> git checkout 和 git reset --hard 可能会是一个昂贵的操作
,带有大量磁盘I / O 。另一方面,使用 git branch -f 时,只有一个单个文件将写在磁盘上,即包含< commithash> < branchname> 引用。


Up until now, I have always used git checkout <branch_name>; git reset --hard <hash> to move a branch back to an earlier commit.

Then I came across this question, but the answers and comments do not explain in great detail the differences between them.

Assuming I have a clean working tree, what internal differences are there between

git branch -f <branch_name> <hash>

and

git checkout <branch_name>
git reset --hard <hash>

and do such differences, if any, have any subtle implications for advanced usage?

解决方案

The main difference is that git branch -f <branchname> <commitref> moves <branchname> to point the specified commit without touching HEAD, the index or the working copy, while git checkout <branchname> && git reset --hard <commitref> modifies all three.

If you want to quickly rearrange branches without moving HEAD or modifying your current working tree, then git branch -f is a good way to do it. It will also work if you have uncommitted changes, which isn't always possible if you use git checkout.

Another difference is related to performance, but it's only relevant for very large projects.
In those cases, modifying your working tree with git checkout and git reset --hard could potentially be an expensive operation with lots of disk I/O. On the other hand, with git branch -f only a single file will be written on disk, i.e. the one that contains the <commithash> referenced by <branchname>.

这篇关于`git branch -f&lt; branch_name&gt;之间的区别&lt; hash&gt;`和`git checkout&lt; branch_name&gt;; git reset --hard&lt; hash&gt;`在干净的工作树下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆