File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments