gpt4 book ai didi

Emacs -- 如何将一个 Git 存储库推送到多个远程服务器

转载 作者:行者123 更新时间:2023-12-04 15:22:35 25 4
gpt4 key购买 nike

我正在寻找一些帮助,请使用 Emacs/Magit 将本地存储库更改一举推送到远程网站和 Github。

我找到了一个非 Emacs/非 Magit 相关线程 (https://stackoverflow.com/a/3195446/2112489),其中评论指出这是推送到远程和 Github 的最终答案,并且有几百个赞。我认为(可能不正确)这是我计算机上 .gitconfig 目录中本地 $HOME 文件的一个很好的起点。

[remote "GitHub"]
url = git@github.com:elliottcable/Paws.o.git
fetch = +refs/heads/*:refs/remotes/GitHub/*
[branch "Master"]
remote = GitHub
merge = refs/heads/Master
[remote "Codaset"]
url = git@codaset.com:elliottcable/paws-o.git
fetch = +refs/heads/*:refs/remotes/Codaset/*
[remote "Paws"]
url = git@github.com:Paws/Paws.o.git
fetch = +refs/heads/*:refs/remotes/Paws/*

Emacs/Magit 中的基本 Push 命令一次只推送一个:
C-u P P [and then use arrow keys to select from the choices in the minibuffer] RET

查看可用命令的 Magit 备忘单: http://daemianmack.com/magit-cheatsheet.html

试探——使用 /usr/local/git/bin/git remote -v获取已经配置好的remote列表,然后用结果推送到每一个。 . .可行,但复杂。
$ MP:my_project.git HOME$ /usr/local/git/bin/git remote -v

origin git@github.com:lawlist/my_project.git (fetch)
origin git@github.com:lawlist/my_project.git (push)
remote_website lawlist@my-website.com:my_project.git (fetch)
remote_website lawlist@my-website.com:my_project.git (push)

命令行食谱 -- 分别推送到远程和 Github:
;; Setup the remote repository and the hook; and the remote destination folder.
ssh lawlist@my-website.com
mkdir /home/lawlist/my_project.git
cd my_project.git
git init --bare
;; git update-server-info # If planning to serve via HTTP
cat > /home/lawlist/my_project.git/hooks/post-receive ;; RET
#!/bin/sh ;; RET
GIT_WORK_TREE=/home/lawlist/my_project git checkout -f ;; RET
;; C-d
chmod 755 /home/lawlist/my_project.git/hooks/post-receive
mkdir /home/lawlist/my_project
exit

;; On local machine.
mkdir /Users/HOME/.0.data/.0.emacs/elpa/my_project.git
touch /Users/HOME/.0.data/.0.emacs/elpa/my_project.git/README.md
cd /Users/HOME/.0.data/.0.emacs/elpa/my_project.git
/usr/local/git/bin/git init
/usr/local/git/bin/git add .
/usr/local/git/bin/git commit -m "First commit."
curl -u lawlist:12345678 https://api.github.com/user/repos -d '{"name":"my_project.git"}'
/usr/local/git/bin/git remote add origin git@github.com:lawlist/my_project.git
/usr/local/git/bin/git remote add remote_website lawlist@my-website.com:my_project.git
/usr/local/git/bin/git push origin master
/usr/local/git/bin/git push remote_website master

;; For modification of local files
/usr/local/git/bin/git add .
/usr/local/git/bin/git commit -m "This is a modification . . . ."
/usr/local/git/bin/git push origin master
/usr/local/git/bin/git push remote_website master

最佳答案

编辑 (2014 年 4 月 23 日):添加了一个非 Magit 解决方案来暂存、提交所有(带有默认提交消息)并推送到所有远程。

编辑 (2014 年 4 月 24 日):所有进程的打印输出现在发送到 git-status-buffer ,它显示在函数的末尾——用户可以选择对窗口做什么——例如,删除窗口、删除缓冲区和窗口,或者什么都不做。添加了一些漂亮的颜色 (propertize "[...]" 'face 'font-lock-warning-face) .依赖于预先存在的 Magit 安装的功能的初稿已移至此答案的底部——该功能有效,但不如不依赖于安装 Magit 的当前版本那么复杂。

(defvar git-status-buffer "*GIT-STATUS*"
"The buffer name of the git-status-buffer.")

(defvar git-branch-name nil
"The current branch of the working Git directory.")
(make-variable-buffer-local 'git-branch-name)

(defvar git-remote-list nil
"List of remote locations -- e.g., lawlist_remote or github_remote.")
(make-variable-buffer-local 'git-remote-list)

(defvar git-commit-message (format "Committed -- %s" (current-time-string))
"The predetermined Git commit message.")
(make-variable-buffer-local 'git-commit-message)

(defun git-branch-process-filter (proc string)
(with-current-buffer (get-buffer git-status-buffer)
(set (make-local-variable 'git-branch-name)
(car (split-string string "\n")))))

(defun git-push-process-filter (proc string)
(when (string-match "password" string)
(process-send-string
proc
(concat (read-passwd "Password: ") "\n")))
(when (and
(not (string-equal "Password: " string))
(not (string-equal "\n" string))
(not (string-equal "stdin: is not a tty\n" string)))
(with-current-buffer git-status-buffer
(goto-char (point-max))
(insert "\n" (replace-regexp-in-string "\^M" "\n" string)))))

(defun git-push-process-sentinel (proc string)
(when (= 0 (process-exit-status proc))
(with-current-buffer (get-buffer git-status-buffer)
(insert
"\n"
(propertize
(format "Process `%s` has finished pushing to `%s`." proc git-remote-name)
'face 'font-lock-warning-face)
"\n"))
(throw 'exit nil)))

(defun stage-commit-push-all ()
"This function does the following:
* Save the current working buffer if it has been modified.
* Obtain the name of the selected branch in the current working buffer.
* Gather a list of all remotes associated with working directory Git project.
* Stage all -- `/usr/local/git/bin/git add .`
* Commit all -- `/usr/local/git/bin/git commit -m [git-commit-message]`
* Push to all remotes: `/usr/local/git/bin/git push -v [remote] [current-branch]`"
(interactive)
(when (buffer-modified-p)
(save-buffer))
(when (get-buffer git-status-buffer)
(with-current-buffer (get-buffer git-status-buffer)
(kill-local-variable 'git-remote-list)
(kill-local-variable 'git-branch-name)
(erase-buffer)))
(start-process
"current-branch"
git-status-buffer
"/usr/local/git/bin/git"
"rev-parse"
"--abbrev-ref"
"HEAD")
(set-process-filter (get-process "current-branch") 'git-branch-process-filter)
(set-process-sentinel
(get-process "current-branch")
(lambda (p e) (when (= 0 (process-exit-status p))
(set-process-sentinel
(start-process
"list-remotes"
git-status-buffer
"/usr/local/git/bin/git"
"remote"
"-v")
(lambda (p e) (when (= 0 (process-exit-status p))
(let* (
beg
end
git-remote-name)
(with-current-buffer (get-buffer git-status-buffer)
(goto-char (point-max))
(while (re-search-backward "\(push\)" nil t)
(beginning-of-line 1)
(setq beg (point))
(re-search-forward "\t" nil t)
(setq end (- (point) 1))
(setq git-remote-name (buffer-substring-no-properties beg end))
(setq git-remote-list
(append (cons git-remote-name git-remote-list)))) ))
(set-process-sentinel
(start-process
"stage-all"
git-status-buffer
"/usr/local/git/bin/git"
"add"
".")
(lambda (p e) (when (= 0 (process-exit-status p))
(with-current-buffer (get-buffer git-status-buffer)
(goto-char (point-max))
(insert "\n"))
(set-process-sentinel
(start-process
"commit-all"
git-status-buffer
"/usr/local/git/bin/git"
"commit"
"-m"
git-commit-message)
(lambda (p e) (when (= 0 (process-exit-status p))
(mapcar (lambda (git-remote-name)
(let ((proc
(start-process
"push-process"
git-status-buffer
"/usr/local/git/bin/git"
"push"
"-v"
(format "%s" git-remote-name)
(format "%s"
(with-current-buffer (get-buffer git-status-buffer)
git-branch-name)) )))
(set-process-filter proc 'git-push-process-filter)
(set-process-sentinel proc 'git-push-process-sentinel)
(recursive-edit) ))
(with-current-buffer (get-buffer git-status-buffer)
git-remote-list) )
(display-buffer (get-buffer git-status-buffer))
(message (concat
git-status-buffer
" -- ["
(propertize "d" 'face 'font-lock-warning-face)
"]elete window | ["
(propertize "k" 'face 'font-lock-warning-face)
"]ill buffer + delete window | ["
(propertize "n" 'face 'font-lock-warning-face)
"]othing"))
(let* (
(git-window-options (read-char-exclusive))
(target-window (get-buffer-window git-status-buffer)))
(cond
((eq git-window-options ?d)
(with-current-buffer (get-buffer git-status-buffer)
(delete-window target-window)))
((eq git-window-options ?k)
(with-current-buffer (get-buffer git-status-buffer)
(delete-window target-window)
(kill-buffer (get-buffer git-status-buffer))))
((eq git-window-options ?n)
(message "Done!"))
(t (message "You have exited the sub-function.")) ))
)))))))))))))

初稿 (2014 年 4 月 19 日):此功能需要预先安装 Magit。代码阐明 以上 不需要安装Magit。

(defun push-to-all-remotes ()
"This function requires a pre-existing installation of Magit, and the function assumes
that the user has already staged and committed -- i.e., it only pushes to all remotes."
(interactive)
(let* (beg end remote)
(when (get-buffer "*REMOTES*")
(with-current-buffer (get-buffer "*REMOTES*")
(erase-buffer)))
(set-process-sentinel
(start-process
"list-remotes"
"*REMOTES*"
"/usr/local/git/bin/git"
"remote"
"-v")
(lambda (p e) (when (= 0 (process-exit-status p))
(with-current-buffer (get-buffer "*REMOTES*")
(goto-char (point-max))
(while (re-search-backward "\(push\)" nil t)
(beginning-of-line 1)
(setq beg (point))
(re-search-forward "\t" nil t)
(setq end (- (point) 1))
(setq remote (buffer-substring-no-properties beg end))
(magit-run-git-async
"push"
"-v"
remote
(magit-get-current-branch))) ))))
(display-buffer (get-buffer magit-process-buffer-name)) ))

关于Emacs -- 如何将一个 Git 存储库推送到多个远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com