Git命令仅重置索引和工作树而不重置HEAD [英] Git command to reset only index and working tree not the HEAD

查看:142
本文介绍了Git命令仅重置索引和工作树而不重置HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是对的后续操作>这个问题.它试图找到一种在交互式变基过程中编辑提交的更简单方法.

This question is a follow up on this question. It's trying to find a simpler way to edit commits during an interactively rebase.

这是一个完整的示例,可以看到问题:

Here is a full example where the problem can be seen:

mkdir git_example
cd git_example
git init
echo first > first
git add first 
git commit -m "initial"
git tag initial

echo "How do I see this change while editing in interacive rebase" > second
git add second 
git commit -m "second"

echo third > third
git add third 
git commit -m "third"

git rebase -i initial
    e 66127f1 second
    pick 70c0b50 third

git reset HEAD~
git add .
git commit  
# NOT commit --amend
# The commit message will be empty, that's ok
git rebase --continue

问题出在git reset HEAD~上,并且它改变了HEAD.这会破坏该提交中的提交消息,并留下# The commit message will be empty, that's ok.

The problem lays in git reset HEAD~ and the fact that it changes the HEAD. This destroys the commit message from this commit and leaves us with # The commit message will be empty, that's ok.

是否有一种方法可以重置索引和工作树,但保持HEAD完好无损?

Is there a way to reset the index and working tree but keep the HEAD intact?

git reset -h的帮助列出了所有组合,而不是该组合.

The help for git reset -h list all combinations instead of that one.

推荐答案

使用git-restore

是否有一种方法可以重置索引和工作树,但保持HEAD完好无损?

Is there a way to reset the index and working tree but keep the HEAD intact?

这正是新的 git-restore 命令用于:

That's exactly what the new git-restore command is for:

使用还原源中的一些内容将工作路径还原到工作树中.

Restore specified paths in the working tree with some contents from a restore source.

默认情况下, git-restore 仅修改工作树,但您也可以告诉它更新索引:

By default, git-restore will only modify the working tree, but you can tell it to update the index as well:

该命令还可以用于使用--staged还原索引中的内容,或者使用--staged --worktree还原工作树和索引.

The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree.

您可能会注意到,此行为与git-reset直接重叠;两者之间的主要区别在于git-restore不会碰到HEAD引用,而这正是您想要的.

You may notice that this behavior overlaps directly with git-reset; the key difference between the two is that git-restore won't touch the HEAD reference, which is exactly what you want.

该命令将是:

git restore --staged --worktree --source HEAD~

请记住,git restore仍在积极地工作中(文档中将其列为试验性项目),但这绝对是前进的方向.

Keep in mind that git restore is still being actively worked on (the documentation lists it as experimental) but this is definitely the way forward.

还有另一种方法.如果您想在交互式基础上查看与当前提交相关联的补丁,则不必将索引重置为HEAD~即可;相反,您可以简单地使用 git-show

There's also another way. If you want to see the patch associated to the current commit during an interactive rebase, you don't have to reset the index to HEAD~ to do it; instead, you can simply use git-show or git-diff.

您的工作流程将是:

git rebase -i initial
    e 66127f1 second
    pick 70c0b50 third
git show
# or
git diff HEAD~..HEAD
# Look at the patch generated by 66127f1
# Modify the files in your working directory as needed 
git add .
git commit --amend
git rebase --continue

这篇关于Git命令仅重置索引和工作树而不重置HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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