作者:E4b9a6, 创建:2021-06-15, 字数:3196, 已阅:134, 最后更新:2024-03-10
在开发中,如果当前代码修改了一部分,紧急接到热修复的需求,就需要面临保存当前修改,切换到全新仓库热修复代码后提交再切换回来
这种场景下,git stash 比 git commit 更为合适
常见操作:
例子
# chancel @ chancel-cp in /tmp/stash on git:master o [12:04:03]
$ git status
On branch master
nothing to commit, working tree clean
# chancel @ chancel-cp in /tmp/stash on git:master o [12:05:11] C:130
$ echo "I'm fine,fuck you" > hello.py
# chancel @ chancel-cp in /tmp/stash on git:master x [12:05:17]
$ ll
total 8.0K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
-rw-r--r-- 1 chancel chancel 18 9月 6 12:05 hello.py
# chancel @ chancel-cp in /tmp/stash on git:master x [12:05:18]
$ git add .
# chancel @ chancel-cp in /tmp/stash on git:master x [12:05:21]
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
# chancel @ chancel-cp in /tmp/stash on git:master x [12:05:24]
$ git stash
Saved working directory and index state WIP on master: a404cee first commit
# chancel @ chancel-cp in /tmp/stash on git:master o [12:05:37]
$ git status
On branch master
nothing to commit, working tree clean
# chancel @ chancel-cp in /tmp/stash on git:master o [12:05:41]
$ ll
total 4.0K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
# chancel @ chancel-cp in /tmp/stash on git:master o [12:05:54]
$ echo "hello" > hello.txt
# chancel @ chancel-cp in /tmp/stash on git:master x [12:06:17]
$ ll
total 8.0K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
-rw-r--r-- 1 chancel chancel 6 9月 6 12:06 hello.txt
# chancel @ chancel-cp in /tmp/stash on git:master x [12:06:21]
$ git add hello.txt
# chancel @ chancel-cp in /tmp/stash on git:master x [12:06:26]
$ git stash
Saved working directory and index state WIP on master: a404cee first commit
# chancel @ chancel-cp in /tmp/stash on git:master x [12:07:20]
$ ll
total 8.0K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
-rw-r--r-- 1 chancel chancel 6 9月 6 12:07 hello.txt
# chancel @ chancel-cp in /tmp/stash on git:master x [12:07:45]
$ git stash pop
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.txt
Dropped refs/stash@{0} (b9963709ffa5959d2341b31378d9b3457a2b9798)
# chancel @ chancel-cp in /tmp/stash on git:master x [12:07:56]
$ ll
total 8.0K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
-rw-r--r-- 1 chancel chancel 6 9月 6 12:07 hello.txt
# chancel @ chancel-cp in /tmp/stash on git:master x [12:07:58]
$ git stash pop
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
new file: hello.txt
Dropped refs/stash@{0} (172bdcff963c2786becbb9acd526d968b279b3e0)
# chancel @ chancel-cp in /tmp/stash on git:master x [12:08:02]
$ ll
total 12K
-rw-r--r-- 1 chancel chancel 4 9月 6 12:03 123.txt
-rw-r--r-- 1 chancel chancel 18 9月 6 12:08 hello.py
-rw-r--r-- 1 chancel chancel 6 9月 6 12:07 hello.txt