menu Chancel's blog
rss_feed
Chancel's blog
我就是这样的人

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

作者:Chancel, 时间:2021 Jun 15, 阅读:101

Git 工具 - 储藏(Stashing)

经常有这样的事情发生,当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作。问题是,你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决这个问题的办法就是git stash命令。

重点

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

例子

# 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]])

还没有可以显示的留言...
[[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)]]