git 临时切换分支

2023-11-15 10:36:33 +08:00
 yujianwjj

大家在开发过程中,遇到需要切换分支的情况,比如需要紧急修复一个线上 bug 。这个时候,是使用 git commit 临时保存本地代码还是使用 git stash 。

19183 次点击
所在节点    git
122 条回复
Jxnujason
2023-11-15 14:16:21 +08:00
习惯用 commit 了,要 push 前 squash 一下
lisxour
2023-11-15 14:23:30 +08:00
@nagisaushio 会忘那就证明你压根就没好好用 git ,stash 就是专门来干这种事的,然而大部分人基本都只会 git add 、git commit 、git pull 、git push
Ericcccccccc
2023-11-15 14:26:14 +08:00
就是 stash
ColdBird
2023-11-15 14:28:35 +08:00
我以前都是通过 commit 暂存的,记录 wip 或者暂存,学到了
whyrookie
2023-11-15 14:33:47 +08:00
stash
ichanne
2023-11-15 14:34:09 +08:00
因为经常出现这种情况,我是同一份代码库本地拉了两份,其中一份永远对应线上代码,另一份是开发代码,不需要来回保存代码切分支
shawndev
2023-11-15 14:34:48 +08:00
上策 git worktree
中策 git stash
下策 git commit
Terry166
2023-11-15 14:38:21 +08:00
可以用 git commit ,用 git stash 更简洁一些:

When you are in the middle of something, your boss comes in and demands that you fix something immediately. Traditionally, you would make a commit to a temporary branch to store your changes away, and return to your original branch to make the emergency fix, like this:

# ... hack hack hack ...
$ git switch -c my_wip
$ git commit -a -m "WIP"
$ git switch master
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git switch my_wip
$ git reset --soft HEAD^
# ... continue hacking ...

You can use git stash to simplify the above, like this:

# ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash pop
# ... continue hacking ...
MonkeyJon
2023-11-15 14:49:24 +08:00
# 保存当前未 commit 的代码
git stash

# 保存当前未 commit 的代码并添加备注
git stash save "备注的内容"

# 列出 stash 的所有记录
git stash list

# 删除 stash 的所有记录
git stash clear

# 应用最近一次的 stash
git stash apply

# 应用最近一次的 stash ,随后删除该记录
git stash pop

# 删除最近的一次 stash
git stash drop
xuxu5112
2023-11-15 14:52:41 +08:00
clone 两份互不影响
liuidetmks
2023-11-15 15:09:26 +08:00
stash 只适合 10 分钟以内能切回来的情况。


发现 bug1 stash
你切换到分支 bugfix1 ,做了一半,发现更紧急 bug2 ,又要 stash
多来几次,你确定能记住顺序?

commit + reset 挺好的, 通过少量简单指令的组合,做复杂事情,而不是提供复杂的指令做复杂的事情。
YienX
2023-11-15 15:42:36 +08:00
@MonkeyJon 其实应该先 git add .
不然新建的文件没有被 stash ,🤣等你改完 bug 发现有一堆新建的文件
YienX
2023-11-15 15:43:28 +08:00
@MonkeyJon 哦看错,以为你在说 整个流程...
unco020511
2023-11-15 15:45:58 +08:00
我们项目比较大,都是准备几个 project 来切换 workspace 的.正常的话,如果是用 jb 系列的 ide 我更倾向于用 ide 提供的 shelf,如果只用 git 自身的,那就 stash 咯
AquanllR
2023-11-15 15:48:02 +08:00
git stash
296727
2023-11-15 15:58:13 +08:00
再 clone 一个项目,在那里 fix 最简单
HangoX
2023-11-15 16:01:16 +08:00
都不是,你应该保留一个专门用来修复的 clone 版本,你操作再逆天,总有失误的时候,线上问题又比较紧急
出问题就用直接用那个 clone 版本

实际上我有 5 个项目的 clone 版本,需要的时候直接多开一个即可
mynameislihua
2023-11-15 16:04:05 +08:00
确实,可任意专门再弄一个 clone 版本
4771314
2023-11-15 16:12:29 +08:00
commit ,我甚至会 push ,别问我为什么?等你丢过代码就知道了

自己的分支,后面 rebase 就可以了
ygtq
2023-11-15 16:14:53 +08:00
@liuidetmks 这个就有点杠了,哪来那么多都是紧急且同一时间的 bug ,如果真的 bug1 非常紧急你在处理,同时 bug2 也来了也紧急,那就喊别人处理,没有别人就排队处理,总有一个优先级,真的都紧急都很复杂需要 debug 很久,你一个人也不可能同时处理两个,都是人,只能“多任务”式的并发处理,没法真正的“多线程”的并发一个以上的事情啊

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

https://ex.noerr.eu.org/t/992022

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

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

© 2021 V2EX