menu E4b9a6's blog
rss_feed
E4b9a6's blog
有善始者实繁,能克终者盖寡。

Git保存代码修改并切换分支

作者:E4b9a6, 创建:2021-06-15, 字数:3196, 已阅:134, 最后更新:2024-03-10

这篇文章更新于 259 天前,文中部分信息可能失效,请自行甄别无效内容。

在开发中,如果当前代码修改了一部分,紧急接到热修复的需求,就需要面临保存当前修改,切换到全新仓库热修复代码后提交再切换回来

这种场景下,git stashgit commit 更为合适

常见操作:

  • git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致,同时,将当前的工作区内容保存到Git栈中
  • git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容,由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复
  • git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复
  • git stash clear: 清空Git栈

例子

Bash
# 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

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]