Skip to content

Instantly share code, notes, and snippets.

@grafov
Last active July 21, 2025 22:26
Show Gist options
  • Save grafov/1fa5ce74fe5f4d4f536d78c971bf5638 to your computer and use it in GitHub Desktop.
Save grafov/1fa5ce74fe5f4d4f536d78c971bf5638 to your computer and use it in GitHub Desktop.
Create a temporary commit from the staged changes and merge it with the previous commit. Comments in Russian and Esperanto but I think translation is not a problem nowadays )
(defun my-magit-commit-and-rebase-fixup ()
"Создать временный коммит из staged изменений и объединить с предыдущим коммитом."
(interactive)
;; Проверяем наличие staged изменений
(when (not (magit-anything-staged-p))
(user-error "Nenio ŝanĝoj estas en la 'staged'"))
;; Проверяем наличие коммитов в истории
(when (not (magit-rev-verify "HEAD"))
(user-error "Nenio ŝanĝoj estas en la historio"))
(let* ((parent-commit (magit-rev-parse "HEAD")) ; Получаем текущий коммит (HEAD)
(parent-msg (magit-rev-format "%s" parent-commit)) ; Сообщение предыдущего коммита
(temp-msg (concat "fixup! " parent-msg)) ; Формируем сообщение для временного коммита
(base (if (magit-rev-verify "HEAD~2") "HEAD~2" "--root"))) ; Базовый коммит для rebase
;; Создаем временный коммит из staged изменений
(magit-call-git "commit" "--no-verify" "-m" temp-msg)
;; Выполняем интерактивный rebase с автодополнением
(let ((process-environment (cons "GIT_SEQUENCE_EDITOR=true" process-environment)))
(condition-case err
(magit-rebase-interactive base (list "--autosquash" "--autostash"))
(error
(message "Rebase failed: %s" err)
(magit-rebase-abort)
(user-error "Rebase failed, aborted"))))
;; Обновляем интерфейс Magit
(magit-refresh-all)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment