add support for restoring file to previous commit

This commit is contained in:
Bhupesh-V 2021-04-24 12:42:04 +05:30
parent d52a73108a
commit 216b70382d
2 changed files with 28 additions and 9 deletions

View file

@ -1,6 +1,6 @@
<h1 align="center">ugit</h1>
<p align="center"><img align="center" alt="ugit logo" height="100px" src="https://user-images.githubusercontent.com/34342551/115037937-a608d800-9eec-11eb-88a9-252da7d6f507.png"></p>
<h3 align="center"><code>Undo your last oopsie 🙈️ in git without much effort</code></h3>
<h4 align="center"><code>Undo your last oopsie 🙈️ in git without much effort</code></h4>
<p align="center">
<a href="https://github.com/Bhupesh-V/ugit/blob/master/LICENSE">
<img alt="License: MIT" src="https://img.shields.io/github/license/Bhupesh-V/ugit" target="_blank" />
@ -29,14 +29,15 @@
- [x] Undo `git branch -D` (branch delete)
- [x] Undo `git pull`
- [x] Undo `git reset`
- [ ] Undo `git merge`
- [x] Undo `git tag -d` (tag delete)
- [x] Undo `git stash apply`
- [x] Undo `git stash pop/drop/clear`
- [x] Undo accidental file delete (Restore a deleted file after a commit)
- [x] Undo (Restore) a file to a previous version
- [ ] Undo `git merge`
- [ ] Undo `git tag` (rename a tag)
- [ ] Undo `git rebase`
- [ ] Undo `git cherry-pick`
- [x] Undo accidental file delete (Restore a deleted file after a commit)
- [ ] Undo `git worktree remove` (recover deleted work-tree)
## Installation
@ -98,6 +99,9 @@ You can read my in-process guide on [**How to undo anything in Git**](https://bh
- Twitter : [@bhupeshimself](https://twitter.com/bhupeshimself)
- DEV : [bhupesh](https://dev.to/bhupesh)
## Credit & Thanks
To all the SO threads that I will probably never visit again ;)
## ☺️ Show your support
Support me by giving a ⭐️ if this project helped you! or just [![Twitter URL](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FBhupesh-V%2Fugit%2F)](https://twitter.com/intent/tweet?url=https://github.com/Bhupesh-V/ugit&text=ugit%20via%20@bhupeshimself)

27
ugit
View file

@ -45,6 +45,7 @@ display_menu() {
printf "%s\n" "Undo [git rebase]"
printf "%s\n" "Undo/Recover commited file delete"
printf "%s\n" "Undo/Recover uncommited file delete"
printf "%s\n" "Undo/Restore file to a previous commit/version"
}
option=$(display_menu | nl -n ln | fzf --header="Don't worry we all mess up sometimes" --height 30% --reverse --pointer='ᐅ' --cycle | awk '{print $1}')
@ -73,10 +74,10 @@ undo_git_add() {
}
change_commit_message() {
echo -e "Enter New Commit Message (Ctrl+d to save):"
printf "%s" "Enter New Commit Message (Ctrl+d to save):"
msg=$(</dev/stdin)
echo
[[ "$msg" ]] && git commit --amend -m "$msg" || echo "bruh enter something!!"
[[ "$msg" ]] && git commit --amend -m "$msg" || printf "%s" "Empty Commit string!"
}
undo_git_push() {
@ -89,9 +90,9 @@ undo_branch_delete() {
# undo local branch delete
last_branch_commit=$(git reflog | fzf --ansi --height 20% --reverse --multi --header="Choose last good branch commit" | awk '{print $1}')
# echo "$last_branch_commit"
read -p "Enter Branch Name: " branch_name
if git checkout -b "$branch_name" "$last_branch_commit"; then
printf "%s\n" "Branch $branch_name successfully recovered"
read -p "Enter Branch Name: " -r BRANCH_NAME
if git checkout -b "$BRANCH_NAME" "$last_branch_commit"; then
printf "%s\n" "Branch $BRANCH_NAME successfully recovered"
fi
}
@ -170,13 +171,26 @@ undo_file_delete() {
exit 0
fi
fi
commit_hash=$(git log --diff-filter=D --oneline| fzf --ansi --height 20% --reverse --header="Choose commit that deleted the file" | awk '{print $1}')
commit_hash=$(git log --diff-filter=D --oneline | fzf --ansi --height 20% --reverse --header="Choose commit that deleted the file" | awk '{print $1}')
read -p "Enter complete filename: " -r FILENAME
if git checkout "$commit_hash"~1 -- "$FILENAME"; then
printf "%s\n" "$FILENAME Successfully Recovered"
fi
}
restore_file() {
FILE=$(git ls-files | fzf --ansi --height 20% --reverse --header="Choose file to restore" | awk '{print $1}')
COMMIT=$(git log --oneline "$FILE" | fzf --ansi --height 80% --reverse --prompt="Choose commit to restore file to" --header="Use ctrl-j/ctrl-k to navigate file preview" --preview "echo {} | cut -d' ' -f1 | xargs -I{} git show {}:$FILE" --bind 'j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up' --preview-window down:60%)
if ! git diff -s --exit-code "$FILE"; then
# check if any local changes
printf "%s\n" "$FILE seems to be modified. Please either commit or discard those changes."
exit 0
elif git reset "$COMMIT" "$FILE"; then
printf "%s\n" "$FILE restored to version $COMMIT"
fi
}
case $option in
1) undo_git_commit;;
2) undo_git_push;;
@ -194,4 +208,5 @@ case $option in
14) do_git_reset "Choose commit just before rebase started";;
15) undo_file_delete;;
16) undo_file_delete "uncommited";;
17) restore_file;;
esac