如何git重置 - 哈哈一个子目录? [英] How to git reset --hard a subdirectory?

查看:1288
本文介绍了如何git重置 - 哈哈一个子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


更新:从Git 1.8.3开始,这将更加直观地工作,请参阅我自己的答案


想象一下下面的用例:我想摆脱特定的所有更改




  • 我可以做 git checkout。我的Git工作树的子目录保留了所有其他子目录。 ,但 git checkout。添加稀疏结帐排除的目录

  • 它不会让我这样做的一个子目录:

     > git reset --hard。 
    致命:无法用路径进行硬重置。

    同样:为什么git无法通过路径进行硬/软复位?


  • 我可以使用 git diff subdir |来修改当前状态patch -p1 -R ,但这是一个很奇怪的做法。

  • >这个操作的Git命令是什么?



    下面的脚本说明了这个问题。在下面插入正确的命令如何创建文件注释 - 当前命令将恢复文件 a / c / ac 这应该被稀疏结帐排除在外。请注意,我不希望显式地恢复 a / a a / b ,我只知道 a ,并且想要恢复下面的所有内容。 编辑:我也不知道 b ,或者其他目录与 a 。

     #!/ bin / sh 

    rm -rf repo ; git init回购; cd回购
    为b中的f;对b中的g做
    ; do
    mkdir -p $ f / $ g
    touch $ f / $ g / $ f $ g
    git add $ f / $ g
    git commit -madded $ f / $ g
    完成
    完成
    git config core.sparsecheckout true
    echo a / a> .git / info / sparse-checkout
    echo a / b>> .git / info / sparse-checkout
    echo b / a>> .git / info / sparse-checkout
    git read-tree -m -u HEAD
    echoread-tree:
    find * -type f

    rm a / a / aa
    rm a / b / ab
    echo>> b / a / ba
    echo修改后:
    find * -type f
    git status

    #如何让文件a / *在不更改的情况下重新出现b并没有重新创建一个/ C?
    git checkout - a

    echo结账后:
    git status
    find * -type f
    <根据Git开发者Duy Nguyen的说法,他善意地实现了

/ git / commit /?id = 08d595dc1cdf6f0d8e6022a69c4fcdd2fba628cf>该功能和兼容性开关,以下方式按预期工作自Git 1.8.3

  git checkout  -  a 

(其中 a 是您要硬重置的目录) 。原始行为可以通过

  git checkout --ignore-skip-worktree-bits  -  a 


UPDATE: This will work more intuitively as of Git 1.8.3, see my own answer.

Imagine the following use case: I want to get rid of all changes in a specific subdirectory of my Git working tree, leaving all other subdirectories intact.

What is the proper Git command for this operation?

The script below illustrates the problem. Insert the proper command below the How to make files comment -- the current command will restore the file a/c/ac which is supposed to be excluded by the sparse checkout. Note that I do not want to explicitly restore a/a and a/b, I only "know" a and want to restore everything below. EDIT: And I also don't "know" b, or which other directories reside on the same level as a.

#!/bin/sh

rm -rf repo; git init repo; cd repo
for f in a b; do
  for g in a b c; do
    mkdir -p $f/$g
    touch $f/$g/$f$g
    git add $f/$g
    git commit -m "added $f/$g"
  done
done
git config core.sparsecheckout true
echo a/a > .git/info/sparse-checkout
echo a/b >> .git/info/sparse-checkout
echo b/a >> .git/info/sparse-checkout
git read-tree -m -u HEAD
echo "After read-tree:"
find * -type f

rm a/a/aa
rm a/b/ab
echo >> b/a/ba
echo "After modifying:"
find * -type f
git status

# How to make files a/* reappear without changing b and without recreating a/c?
git checkout -- a

echo "After checkout:"
git status
find * -type f

解决方案

According to Git developer Duy Nguyen who kindly implemented the feature and a compatibility switch, the following works as expected as of Git 1.8.3:

git checkout -- a

(where a is the directory you want to hard-reset). The original behavior can be accessed via

git checkout --ignore-skip-worktree-bits -- a

这篇关于如何git重置 - 哈哈一个子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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