magit一键add, commit, push

组合了一个很好用的功能,使用magit一键提交 emacs_magit

对于不重要的提交,可以跳过填写commit信息步骤,直接按 Enter,使用默认commit message 整个流程不到1s, 如果还想更快,可以去掉交互代码。 emacs_magit1

(defun aborn/simple-git-commit-push ()
  "Simple commit current git project and push to its upstream."
  ;; (interactive "sCommit Message: ")
  (interactive)
  (when (and buffer-file-name
             (buffer-modified-p))
    (save-buffer))                   ;; save it first if modified.
  (magit-diff-unstaged)
  (when (yes-or-no-p "Do you really want to commit everything?")
    (magit-stage-modified)
    ;; (magit-mode-bury-buffer)
    (magit-diff-staged)
    (setq msg (read-string "Commit Message: "))
    (when (= 0 (length msg))
      (setq msg (format-time-string "commit by magit in emacs@%Y-%m-%d %H:%M:%S"
                                    (current-time))))
    (message "commit message is %s" msg)
    ;;(magit-commit (list "-m" msg))
    (magit-call-git "commit" "-m" msg)
    (magit-push-current-to-upstream nil)
    (message "now do async push to %s" (magit-get "remote" "origin" "url")))
  (magit-mode-bury-buffer))
13 个赞

感谢楼主分享!

2 个赞

个人项目很有用 多人维护项目最好是不要出现无意义或者说重复的 commit

嗯嗯,是的,默认提交主要用于个人项目,多人项目还是需要按规范写commit信息

1 个赞

:joy: 只有我这样的菜鸡手动写 commit 吗?

不过也不是全手动,每次 commit 的时间标记(ISO 8601 格式)是用工具自动生成的。

1 个赞

应该还是需要手写的 :joy: 自己的 repo 可以随便弄,多人项目我们遵循类似 angular commit 规范,还没找到能自动生成类似 commit 的工具。

可能都是内部使用的自动化工具吧。

这里是不是没意义啊?毕竟下一步的结果直接就覆盖掉了,也看不到这个回显。

抛砖引玉,小做改动:

(defun git-add-commit-push ()
  "Simple commit current git project and push to its upstream."
  (interactive)
  (when (and buffer-file-name
             (buffer-modified-p))
    (save-buffer))                   ;; save it first if modified.
  (magit-diff-unstaged)
  (when (yes-or-no-p "Do you really want to commit everything?")
    (magit-stage-modified)
    (magit-diff-staged)
    (let ((msg (read-string "Commit Message: ")))
      (when (= 0 (length msg))
        (setq msg (format-time-string "commit by magit in emacs@%Y-%m-%d %H:%M:%S"
                                      (current-time))))
      (magit-call-git "commit" "-m" msg)
      (magit-push-current-to-upstream nil)
      (message "now do async push to %s" (magit-get "remote" "origin" "url"))))
  (magit-mode-bury-buffer))
1 个赞

:+1: 我也是在 aborn 基础上改的,加了一点功能,源代码位置在这 https://github.com/aborn/.spacemacs.d/blob/687750f41a67ef3e8829b36095074f05d75f5b0d/parts/aborn-swift.el#L71

个人项目也最好写下 msg,如果实在不想写,用同步网盘如坚果云是更好的选择。

对 rust 项目来说不合适,target目录太大了

用git习惯了 :joy: 写点东西或者有些配置想交到github上,但又不想写commit,写了感觉永远也不会看,这样用默认commit还是挺有用的

1 个赞

无 message 不提交。

1 个赞

又发现一个小的点,(magit-stage-modified t)可以把untracked files也暂存起来。我一般是对自己的org-roam用的,所以有了新的node当然要一并提交,试了试(magit-stage-untracked)不知为什么无法生效,反倒是这个方法能搞定。

我用一个sh脚本一键push

#!/bin/bash
#datime=$(date "+%Y-%m-%d %H:%M")
datime=$(date "+%Y年%m月%d日 %H:%M")
cd /path/to/current/git/repo
git add .
git commit -m "[脚本]自动上传于 $datime"
echo "git commit: 上传于 $datime"
git push
echo "finished..."