因为有两台电脑 我需要一个跨设备的 git stash

2024-11-04 16:58:17 +08:00
 jybox

之前就记得其实 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 会报错),不过基本够我用了。

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

3018 次点击
所在节点    git
16 条回复
quqiu
2024-11-04 17:23:43 +08:00
为啥不直接开临时分支搞?
liu731
2024-11-04 17:25:34 +08:00
为啥要分开,2 台电脑操作相同 git 不就好了吗(比如放在 NAS 里面)?
jybox
2024-11-04 17:35:03 +08:00
@quqiu 建分支和建 commit 按说都需要起名字,在这种场景下我就是想要 git stash 这种比较临时、不需要起任何名字的语义。或者你理解成我规定了一个特殊的名字(`refs/stashes/wip`)来建分支也可以。
@liu731 我觉得把 git 放在一个共享存储里不是一个好主意:如果是实时的共享存储那么就需要设备一直在线且性能受网络影响;如果支持离线同步的共享存储就可能会有文件冲突,git 本身就已经是一个用来同步改动解决冲突的系统了,如果还要给 git 本身来解决冲突就太麻烦了点。
qwell
2024-11-04 17:59:31 +08:00
一边 commit ,另一边拉取再 reset 呢
alexsz
2024-11-04 18:06:32 +08:00
@quqiu #1 同问
minglanyu
2024-11-04 18:17:05 +08:00
蛮小众的场景 不过确实属于 git 高级玩家了
zmxnv123
2024-11-04 18:18:08 +08:00
我也有类似的问题,仅 sync git stash 不够用啊,如果一台电脑本地提交了没推远程另一台电脑就废了。后来我直接 remote develop 了,但是网速是个问题
wjfz
2024-11-04 18:25:16 +08:00
这种情况我一般都是先 push
在另一台电脑 pull 、git reset HEAD~,修改完之后再 git push -f
jadehare
2024-11-04 18:38:04 +08:00
commit 同步了再 rebase ?
nebkad
2024-11-04 18:57:40 +08:00
非常合理的软件需求分析和功能设计,写好了喊我试试
yh7gdiaYW
2024-11-04 19:27:32 +08:00
我是用一个 git 和一个 svn 实现了差不都的效果
webcape233
2024-11-04 19:45:11 +08:00
我是家里两个电脑 syncthing...啥也不用管自己用不
mengzhuo
2024-11-04 19:49:38 +08:00
push 到一个无头分支,然后 cherry-pick 过来就行
zmxnv123
2024-11-04 20:54:15 +08:00
@webcape233 syncthing 同步不了 git 仓库,尤其是频繁修改的情况下
quqiu
2024-11-05 21:04:11 +08:00
@mengzhuo 可以的,stash 专用分支
hzzhzzdogee
270 天前
cool

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1086531

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX