0

Case:

The commit history is:

A -> B -> C -> D

I hope to reset the current work dir to B on current branch with history:

A -> B -> C -> D -> E

The result of the tree is same to B, which means

git diff B..E --name-only

should returns empty list.

A stupid way is:

# we assume current work directory is `tree`, and branch is `master`
# and work directory HEAD at `D`

# reset HEAD to D
cd tree
git checkout -f D

# create a copy work directory
cd ..
rm -rf copy
cp -r tree copy

# clean old work directory
rm -rf tree
mkdir tree
cp -r copy/.git tree/.git

# reset copy directory to revision `B`
cd copy
git checkout -f B

# copy tree from copy directory to tree
rm -rf .git
cp -r ./.* ./* ../tree

# commit changes, current revision is we wanted `E`
git add .
git ci -m 'revert to commit B'

Is there a simplified way to do this?

acrazing
  • 1,955
  • 16
  • 24
  • This is just an all-files variant of https://stackoverflow.com/q/215718/1256452, not sure if I should close as duplicate though. – torek Nov 22 '18 at 23:21

2 Answers2

0

Just revert the intermediate commits:

git revert D C

You might need to squash the resulting commits then.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • This leads to two problems: the first is it will create multiple commits, the result will be `A -> B -> C -> D -> D' -> C'`, and the second is that may be meat a merge commit or conflicts, it could not be handled automatically. – acrazing Nov 22 '18 at 08:19
0

checkout files in commit B

git checkout B -- .

you can see the changes B and D

git diff HEAD

then commit it