你怎么能删除,然后使用藏匿而无需在GIT中合并冲突恢复非指数的变化? [英] How can you remove and then restore non index changes using the stash without ever having merge conflicts in git?

查看:109
本文介绍了你怎么能删除,然后使用藏匿而无需在GIT中合并冲突恢复非指数的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够运行我的测试,我随着指数的当前状态的项目,忽视非承诺工作变动(后来我打算把它添加到pre-commit钩子)。不过,我有麻烦搞清楚如何删除,然后恢复从未导致合并冲突的方式非指数的变化。我需要这个,因为它是由一个脚本运行,因此在完成时应该不会改变库状态。

混帐藏匿--include-不露痕迹--keep指数 git的藏匿流行接近,但在很多情况下,它导致合并冲突,即使没​​有其中两个命令之间所做的更改。

例如:

 的mkdir项目; CD项目; git的初始化。#设置用的和file.rb初始test.rb项目
猫> file.rb<< EOF
高清FUNC()
  返回42
结束
EOF猫> test.rb<< EOF
#!的/ usr / bin中/ env的红宝石
负载'./file.rb
如果(FUNC()== 42)
  提出通过测试
  退出0
其他
  把测试失败
  1号出口
结束
EOF搭配chmod + X test.rb
git的添加。
git的承诺-m初始提交#改变现在file.rb并添加变化
猫> file.rb<< EOF
高清FUNC()
  回到10 + 32
结束
EOF
git的加file.rb#现在做一个重大更改,以FUNC,不加改变
猫> file.rb<< EOF
高清FUNC()
  回到20 + 32#42没有了......
结束
EOF

在这里,我想请对指数的当前状态的测试,并恢复提交的更改。预期的结果是该测​​试通过,作为开断变化不能添加到索引

下面的命令不起作用:

  git的承诺--include-不露痕迹--keep指数
./test.rb
混帐藏匿流行

git的藏匿处弹出出现问题 - 发生合并冲突。

唯一的其他解决方案,我能想到的是做一个临时的提交,然后藏匿剩下的更改,然后用回滚混帐提交重置--soft HEAD〜,然后弹出藏匿。然而,既烦琐,我不知道那是怎样安全地在$ P $运行P-commit钩子。

有没有更好的办法解决这个问题?


解决方案

  $混帐配置gc.auto 0#安全发挥
$指数='混帐写tree`
$ git的添加-f。
$ WORKTREE =`混帐写tree`
$的git读树$ INDEX
$ git的结帐指数-af
$ git的清洁-dfx
$#你的测试在这里
$的git读树$ WORKTREE
$ git的结帐指数-af
$ git的清洁-dfx
$的git读树$ INDEX
$混帐配置--unset gc.auto
$#你回来了。

git的清洁联机帮助页-x而椭圆表明,这种解决方案

I want to be able to run my tests for my project with the current state of the index, ignoring non-committed working changes (I later plan to add this to a pre-commit hook). However, I am having trouble figuring out how to remove and then restore the non index changes in a way that never causes merge conflicts. I need this because it is being run by a script, so it shouldn't alter the repository state when finished.

git stash --include-untracked --keep-index and git stash pop come close, but in many cases it results in merge conflicts, even if no changes where made between the two commands.

For example:

mkdir project; cd project; git init .;

# setup the initial project with file.rb and test.rb
cat > file.rb <<EOF
def func()
  return 42
end
EOF

cat > test.rb <<EOF
#!/usr/bin/env ruby
load './file.rb'
if (func() == 42)
  puts "Tests passed"
  exit 0
else
  puts "Tests failed"
  exit 1
end
EOF

chmod +x test.rb
git add .
git commit -m "Initial commit"

# now change file.rb and add the change
cat > file.rb <<EOF
def func()
  return 10 + 32
end
EOF
git add file.rb

# now make a breaking change to func, and don't add the change
cat > file.rb <<EOF
def func()
  return 20 + 32 # not 42 anymore...
end 
EOF

From here I want to run the tests against the current state of the index, and the restore the uncommitted changes. The expected result is for the tests to pass, as the breaking change wasn't added to the index.

The following commands do NOT work:

git commit --include-untracked --keep-index
./test.rb
git stash pop

The the problem occurs in the git stash pop - a merge conflict occurs.

The only other solution I could think of was to make a temporary commit, then stash the remaining changes, then rollback the commit with git reset --soft HEAD~, then pop the stash. However that is both cumbersome, and I'm not sure how safe that is to run in a pre-commit hook.

Is there a better solution to this problem?

解决方案

$ git config gc.auto 0   # safety play
$ INDEX=`git write-tree`
$ git add -f .
$ WORKTREE=`git write-tree`
$ git read-tree $INDEX
$ git checkout-index -af
$ git clean -dfx
$ # your tests here
$ git read-tree $WORKTREE
$ git checkout-index -af
$ git clean -dfx
$ git read-tree $INDEX
$ git config --unset gc.auto
$ # you're back.

The git clean manpage for -x rather elliptically suggests this solution

这篇关于你怎么能删除,然后使用藏匿而无需在GIT中合并冲突恢复非指数的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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