之前就记得其实 stash 也是一个特殊的 commit ,可以被 push 到远程仓库,于是写了两个 alias (在 ~/.gitconfig 里):

[alias]
  wips = !(git stash -u | grep -qv 'No local changes to save' || (echo 'No local changes to save' && false)) && git stash show stash@{0} && (git push origin stash@{0}:refs/stashes/wip || (git stash pop && false)) && git stash drop -q
  wipl = !git fetch origin refs/stashes/wip && git stash apply FETCH_HEAD && git push -qd origin refs/stashes/wip

使用时在需要暂存的设备上运行 git wips( Working In Progress Save )

$ git wips
 2019/12.bean |  2 --
 2020/01.bean |  2 +-
 2024/07.bean |  6 ++--
 2024/08.bean | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2024/09.bean | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2024/10.bean | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2024/11.bean | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 main.bean    |  3 ++
 8 files changed, 369 insertions(+), 6 deletions(-)
Enumerating objects: 24, done.
Counting objects: 100% (24/24), done.
Delta compression using up to 12 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (16/16), 3.30 KiB | 3.30 MiB/s, done.
Total 16 (delta 10), reused 0 (delta 0), pack-reused 0 (from 0)
To infinity:repos/accounting
 * [new reference]   stash@{0} -> refs/stashes/wip

然后可以在其他配置了同一远程仓库的设备上运行 git wipl( Working In Progress Load )

$ git wipl
From infinity:repos/accounting
 * branch            refs/stashes/wip -> FETCH_HEAD
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   2019/12.bean
	modified:   2020/01.bean
	modified:   2024/07.bean
	modified:   main.bean

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	2024/08.bean
	2024/09.bean
	2024/10.bean
	2024/11.bean

no changes added to commit (use "git add" and/or "git commit -a")

过程就是从当前的工作区创建一个 stash ,推到远程一个叫 stashes/wip 的特殊 ref 上,再从另外的设备上拉取并删除。目前版本很简单,一次只能存在一个 stash (重复运行 git wips 会报错),不过基本够我用了。

后续有更新的话会更新在 这里

举报· 138 次点击
登录 注册 站外分享
9 条回复  
quqiu 小成 2024-11-4 17:23:43
为啥不直接开临时分支搞?
liu731 小成 2024-11-4 17:25:34
为啥要分开,2 台电脑操作相同 git 不就好了吗(比如放在 NAS 里面)?
qwell 小成 2024-11-4 17:59:31
一边 commit ,另一边拉取再 reset 呢
alexsz 小成 2024-11-4 18:06:32
@quqiu #1 同问
minglanyu 初学 2024-11-4 18:17:05
蛮小众的场景 不过确实属于 git 高级玩家了
zmxnv123 小成 2024-11-4 18:18:08
我也有类似的问题,仅 sync git stash 不够用啊,如果一台电脑本地提交了没推远程另一台电脑就废了。后来我直接 remote develop 了,但是网速是个问题
wjfz 小成 2024-11-4 18:25:16
这种情况我一般都是先 push 在另一台电脑 pull 、git reset HEAD~,修改完之后再 git push -f
jadehare 初学 2024-11-4 18:38:04
commit 同步了再 rebase ?
nebkad 小成 2024-11-4 18:57:40
非常合理的软件需求分析和功能设计,写好了喊我试试
返回顶部