How to undo all working changes in local repository for two month(new branches, commit, etc.)? Reflog history also need to clear.
-
Hi and welcome to SO. Do you have some commit (hash) that you want to revert to or do you want to start completely fresh? There are many similar questions but I don't know if they are what you are looking for. Please provide a little bit more information so that we can help you :) EDIT: possible duplicate of: https://stackoverflow.com/questions/49067898/git-remove-old-reflog-entries – nemanja Nov 15 '18 at 14:17
-
Remove commits with [`git reset --hard`](https://stackoverflow.com/q/5097456/7976758) and clear reflog with [`git reflog expire`](https://stackoverflow.com/a/12883611/7976758). – phd Nov 15 '18 at 15:03
-
Hello. I want to clear all local changes in git by date. For example I set 01.09.2018 and all work (new branches, commits) and history after this date will be remove. – Игорь Жукович Nov 15 '18 at 15:57
2 Answers
This question will help you find the commit you want: How do I view all commits for a specific day? With your example, the first commit listed by this command is the one you want:
git log --until="2018-09-01"
Then you can use @phd's suggestion and reset your sandbox to that commit's sha1:
git reset --hard <sha1>
Warning: this reset command will also move the HEAD of your current branch to that commit. This is probably what you want but you need to be aware of it.
Unfortunately, you will have to do this for each branch one at a time, so I realize this is not the global solution you're looking for.
As for the reflog, I've looked at what expire
does, and it cleans older references. I don't see how to remove the newest lines in the reflog, since that's contrary to what the reflog is intended to do. However, the reflog is stored in .git/logs/HEAD
. Manually deleting lines from that file works, but I don't see an easy way to find which of those lines are after a specific date. If you edit this file, keep in mind the most recent entries are at the end, not at the beginning like git reflog
shows.

- 10,635
- 14
- 30
- 40
Reset commit or index or worktree
Choose one from the below:
git reset --hard <commit> # reset commit, index and worktree git reset [--mix] <commit> # reset commit and index git reset --soft <commit> # reset commit only
Remove the reflogs
Choose one from the below:
# removes all references of unreachable commits in reflog. git reflog expire --expire-unreachable=now --all # expire time: 2.month.ago or 2011-05-17.09:00:00 git reflog expire --expire=<time> --expire-unreachable=now --all # clear reflog git reflog expire --expire=all --all
Cleanup unnecessary files and optimize the local repository
git gc --prune=now

- 56
- 5