赞
踩
在开发途中,有git add了一些内容和未追加的内容,但此时有紧急处理的事情,需要切换到某一条分支下修复功能,但git不能直接切换(不在同一个开发版本下)例如v1.2的最新开发进度如下所示
commit 9815af9d1a1639499cb784c32a2c0e390998a067
Merge: 4c6447e 84cb30d
Author: zx <zx@git.com>
Date: Tue Nov 29 22:36:11 2016 +0800
Merge branch 'master' into v1.2
commit 84cb30d370453e2e1a7dc2c3d974c397a8fa53b2
Author: zx <zx@git.com>
Date: Tue Nov 29 22:28:08 2016 +0800
add by 2016.11.29
commit 4c6447ec598616638b1d78d0c04757d48a04701a
Author: zx <zx@git.com>
Date: Sun Nov 20 10:45:14 2016 +0800
test 1 for v1.2
而v1.1分支的开发进度如下所示。
commit 28f059eb7d9479cc6d5c6f684e106bd540f1f1e5
Author: zx <zx@git.com>
Date: Sun Nov 20 10:18:47 2016 +0800
test 3 for v1.1
commit d8033d48714dcaf8b8fabfbaacc6df8b79b23600
Author: zx <zx@git.com>
Date: Sun Nov 20 10:17:52 2016 +0800
test 2 for v1.1
commit f35d10ace6da47c1b19c4eaf682e5e6645a3d2e1
Author: zx <zx@git.com>
Date: Sun Nov 20 10:16:54 2016 +0800
test 1 for v1.1
使用情景:两条分支处于并行开发的状态,v1.1和v1.2负责开发和测试不同模块的功能。工作时正好处于v1.1的开发中,中途有git add一些修改好的文件,以及未提交的文件,因为代码才写到一半,此时突然需要修改v1.2代码的功能,并且任务非常紧急。因为工作区中有正在修改和添加代码的文件,暂存区中又有修改好的文件。如果强制切换到v1.2分支上,git会提示切换失败,如下所示。
ghost@ghost-machine:~/workspace/test/Project_test$ git checkout v1.2
error: Your local changes to the following files would be overwritten by checkout:
readme.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
ghost@ghost-machine:~/workspace/test/Project_test$
原因是工作区并未干净,如下所示
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.1
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: readme.txt
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: v1.1_test.txt
ghost@ghost-machine:~/workspace/test/Project_test$
此时用到git的储藏
命令:git stash
这个时候使用git储藏命令,将工作的内容暂时保存,即储藏起来,此时工作区是干净的,这样就可以切换到v1.2分支上修改功能。
ghost@ghost-machine:~/workspace/test/Project_test$ git stash
Saved working directory and index state WIP on v1.1: 28f059e test 3 for v1.1
HEAD 现在位于 28f059e test 3 for v1.1
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.1
无文件要提交,干净的工作区
ghost@ghost-machine:~/workspace/test/Project_test$ git checkout v1.2
切换到分支 'v1.2'
ghost@ghost-machine:~/workspace/test/Project_test$
命令:git stash pop
或命令:git stash apply
该命令是将保存的进度恢复到工作区!
v1.2添加代码完成后,切换回v1.1将保存的进度恢复现场
ghost@ghost-machine:~/workspace/test/Project_test$ git stash pop
位于分支 v1.1
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: readme.txt
修改: v1.1_test.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
丢弃了 refs/stash@{0} (21bfc22670ffe8c1a2d86dacfc9869954d49541b)
ghost@ghost-machine:~/workspace/test/Project_test$
可以看到,恢复了之后有个标记被丢弃,该标记的使命已经完成
丢弃了 refs/stash@{0} (21bfc22670ffe8c1a2d86dacfc9869954d49541b)
并且之前提交的git add 也返还成未提交的状态。
再次将刚才的信息压入储藏,使用命令:git stash list可以看到储藏信息。
ghost@ghost-machine:~/workspace/test/Project_test$ git stash list
stash@{0}: WIP on v1.1: 28f059e test 3 for v1.1
ghost@ghost-machine:~/workspace/test/Project_test$ git log
commit 28f059eb7d9479cc6d5c6f684e106bd540f1f1e5
Author: zx <zx@git.com>
Date: Sun Nov 20 10:18:47 2016 +0800
test 3 for v1.1
储藏的信息
WIP on v1.1 :执行储藏的分支为v1.1分支
28f059e :执行储藏是所处的工作区位置的commit哈希值为28f059e,这边仅显示前面的几位数。
test 3 for v1.1 :执行储藏是所处的工作区位置的commit的注释。
git 储藏区可以储藏多条工作区信息
ghost@ghost-machine:~/workspace/test/Project_test$ git stash list
stash@{0}: WIP on v1.4: 8602795 add pathc diff is 2
stash@{1}: WIP on v1.5: 8602795 add pathc diff is 2
stash@{2}: WIP on v1.1: 28f059e test 3 for v1.1
若储藏着多条信息,需要恢复中间或下面的几条时,只需要在恢复命令后面添加对应的编号即可。
如恢复stash@{2}: WIP on v1.1: 28f059e test 3 for v1.1这个储藏,切换至v1.1分支,输入一下命令:
命令:git stash pop stash@{2}
ghost@ghost-machine:~/workspace/test/Project_test$ git stash pop stash@{2}
位于分支 v1.1
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: readme.txt
修改: v1.1_test.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
丢弃了 stash@{2} (f84083046e26f5fa5d96bd2d2952f0a6f063633f)
ghost@ghost-machine:~/workspace/test/Project_test$
注意:工作区与暂存区无分支概念,在哪条分支或master主干上都可以进行恢复,此时就会造成冲突!如下所示,在v1.2添加完功能后,忘记切换回v1.1分支,就会出现冲突。
ghost@ghost-machine:~/workspace/test/Project_test$ git stash pop
自动合并 readme.txt
冲突(内容):合并冲突于 readme.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.2
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: v1.1_test.txt
未合并的路径:
(使用 "git reset HEAD <文件>..." 以取消暂存)
(使用 "git add <文件>..." 标记解决方案)
双方修改: readme.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git diff
diff --cc readme.txt
index eb645f1,86550a8..0000000
--- a/readme.txt
+++ b/readme.txt
@@@ -1,5 -1,3 +1,9 @@@
add 2016.06.13
++<<<<<<< Updated upstream
+test for git stasha
+
+add by 2016.11.29
++=======
+ test git stash
++>>>>>>> Stashed changes
ghost@ghost-machine:~/workspace/test/Project_test$
git stash pop恢复后并没有删除储藏进度,恢复错误后将错误的文件git checkout还原,工作区干净后切换到对应的分支,在git stash pop恢复现场
步骤如下
根据提示现将文件reset,然后再将文件checkout还原。
ghost@ghost-machine:~/workspace/test/Project_test$ git reset HEAD v1.1_test.txt readme.txt
重置后取消暂存的变更:
M readme.txt
M v1.1_test.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.2
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: readme.txt
修改: v1.1_test.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
ghost@ghost-machine:~/workspace/test/Project_test$ git checkout readme.txt v1.1_test.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.2
无文件要提交,干净的工作区
ghost@ghost-machine:~/workspace/test/Project_test$
恢复后就可以切换回分支v1.1来恢复现场。
例如v1.1分支在某个时间处理紧急任务,将工作进度储藏,但处理完后回到v1.1分支,忘记了该分支有储藏,没有恢复现场,继续开发。分支提交了几个commit后发现该分支还有代码在储藏着,需要恢复回来。
v1.1日志
commit 5bb1cf87d50d9591f98d3966c9da278bbd23766d
Author: zx <zx@git.com>
Date: Thu Dec 1 21:28:45 2016 +0800
add 2016.12.01
commit 28f059eb7d9479cc6d5c6f684e106bd540f1f1e5
Author: zx <zx@git.com>
Date: Sun Nov 20 10:18:47 2016 +0800
test 3 for v1.1
git 储藏区情况
ghost@ghost-machine:~/workspace/test/Project_test$ git stash list
stash@{0}: WIP on v1.1: 28f059e test 3 for v1.1
stash@{1}: WIP on v1.4: 8602795 add pathc diff is 2
stash@{2}: WIP on v1.5: 8602795 add pathc diff is 2
ghost@ghost-machine:~/workspace/test/Project_test$
执行git stash pop后,git会自动合并冲突,并列出冲突的地方。
ghost@ghost-machine:~/workspace/test/Project_test$ git stash pop
自动合并 readme.txt
冲突(内容):合并冲突于 readme.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 v1.1
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: v1.1_test.txt
未合并的路径:
(使用 "git reset HEAD <文件>..." 以取消暂存)
(使用 "git add <文件>..." 标记解决方案)
双方修改: readme.txt
冲突的文件:
ghost@ghost-machine:~/workspace/test/Project_test$ git diff
diff --cc readme.txt
index 664a8e0,86550a8..0000000
--- a/readme.txt
+++ b/readme.txt
@@@ -1,3 -1,3 +1,7 @@@
add 2016.06.13
++<<<<<<< Updated upstream
+add by 2016.12.01
++=======
+ test git stash
++>>>>>>> Stashed changes
ghost@ghost-machine:~/workspace/test/Project_test$
这时解决冲突的方法和打补丁解决冲突的方法一样,手动删除旧的代码,保留最新的修改,再次提交说明“这个操作是合并储藏”即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。