Skip to content

Commit 90d587e

Browse files
authored
Merge pull request #43 from kopalchakravarty/fix_undo_initial_commit
Fix: Undo Initial Commit
2 parents be9988d + dd6b723 commit 90d587e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ugit

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ undo_git_commit() {
5353
[ -z "$commit" ] && exit
5454

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

5758
if [[ "$last_commit" == "$commit" ]]; then
58-
if git reset HEAD~; then
59+
# 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).
60+
# This will not delete the files from your working directory
61+
if [[ "$last_commit" == "$initial_commit" ]]; then
62+
if git update-ref -d HEAD && git rm --cached -rf . > /dev/null 2>&1; then
63+
printf "%s\nInitial commit: $initial_commit successfully undone. Commit state is clean."
64+
fi
65+
elif git reset HEAD~; then
5966
printf "%s\n" "Commit: $last_commit successfully undone"
6067
fi
6168
elif git revert "$commit"; then

0 commit comments

Comments
 (0)