0

I need to revert this repository to the commit e4ea7bf73124968e2f68012f77837df2046fd6e5. In other words I want to completely wipe all commits that come after that.

Based on one of the answers in this post here's what I'm doing:

git clone git@github.com:superflycss/utilities-layout.git
cd utilities-layout

git reset e4ea7bf73124968e2f68012f77837df2046fd6e5 
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to e4ea7bf73124968e2f68012f77837df2046fd6e5"

# Updates working copy to reflect the new commit
git reset --hard

At this point I'm thinking that I'm home free, but when I do an ls, all the files that were in the original clone are still there. It's as if no revert has happened at all. Thoughts?

Ole
  • 41,793
  • 59
  • 191
  • 359

2 Answers2

3

To achieve what you want with the least change to what you are doing, switch to this sequence of commands:

git clone git@github.com:superflycss/utilities-layout.git
cd utilities-layout

(same so far, but then:)

git reset --hard e4ea7bf73124968e2f68012f77837df2046fd6e5

(note the addition of --hard here), then back to what you had before:

git reset --soft HEAD@{1}
git commit -m "Revert to e4ea7bf73124968e2f68012f77837df2046fd6e5"

No additional reset is required after this, because the first git reset adjusted both your index and your work-tree (while also modifying the hash stored in your branch name). The second git reset only modifies the hash stored in your branch name, readying you for the git commit.

Your original command sequence uses a --mixed reset for the first step. This resets the index while writing a new hash, but leaves the work-tree untouched. The second, soft, reset fixes the stored hash ID. The third git reset --hard leaves untracked work-tree files untouched: these files became untracked at the first git reset and remained untracked since then. In other words, since that first reset was --mixed, those work-tree files were never removed, and now won't be.


(Side note: I would probably achieve this with one git read-tree followed by one git commit, rather than multiple git reset commands. But this is a different strategy, and does not explain what happened with the method you were using. Hence the answer above, which does explain it.)

torek
  • 448,244
  • 59
  • 642
  • 775
2

To revert to a specific commit you just need to

git reset --hard <COMMIT_HASH>

In case you want to update this change in remote

git push --force origin <BRANCH_NAME>

If you have untracked files have a look to

git clean
jesugmz
  • 2,320
  • 2
  • 18
  • 33
  • To remove all untracked files use `git clean -fdx` in the root directory of the repo. This will also delete untracked directories (d) and files named in .gitignore (x). – milbrandt May 04 '18 at 22:13