Skip to content

Commit

Permalink
Merge pull request #43 from kopalchakravarty/fix_undo_initial_commit
Browse files Browse the repository at this point in the history
Fix: Undo Initial Commit
  • Loading branch information
Bhupesh-V authored Aug 15, 2022
2 parents be9988d + dd6b723 commit 90d587e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ugit
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ undo_git_commit() {
[ -z "$commit" ] && exit

last_commit=$(git log --format="%h" -n 1)
initial_commit=$(git rev-list --max-parents=0 --abbrev-commit HEAD)

if [[ "$last_commit" == "$commit" ]]; then
if git reset HEAD~; then
# If the last commit is the same as the initial commit, delete reference to the branch(update-ref) and unstage changes of the initial commit(git rm).
# This will not delete the files from your working directory
if [[ "$last_commit" == "$initial_commit" ]]; then
if git update-ref -d HEAD && git rm --cached -rf . > /dev/null 2>&1; then
printf "%s\nInitial commit: $initial_commit successfully undone. Commit state is clean."
fi
elif git reset HEAD~; then
printf "%s\n" "Commit: $last_commit successfully undone"
fi
elif git revert "$commit"; then
Expand Down

0 comments on commit 90d587e

Please sign in to comment.