在项目上工作时,如果针对主控器引发了错误,则可能会在功能分支更改中途进行。您尚未准备好提交代码,但也不想丢失所做的更改。这是git stash派上用场的地方。
git status在分支上运行以显示未提交的更改:
(master) $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: business/com/test/core/actions/Photo.c no changes added to commit (use "git add" and/or "git commit -a")
然后运行git stash以将这些更改保存到堆栈中:
(master) $ git stash Saved working directory and index state WIP on master: 2f2a6e1 Merge pull request #1 from test/test-branch HEAD is now at 2f2a6e1 Merge pull request #1 from test/test-branch
如果已将文件添加到工作目录中,则也可以将其隐藏。您只需要先上演它们即可。
(master) $ git stash Saved working directory and index state WIP on master: (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) NewPhoto.c nothing added to commit but untracked files present (use "git add" to track) (master) $ git stage NewPhoto.c (master) $ git stash Saved working directory and index state WIP on master: (master) $ git status On branch master nothing to commit, working tree clean (master) $
现在,您的工作目录将清除您所做的任何更改。您可以通过重新运行来查看git status:
(master) $ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
要应用最后一个存储,运行git stash apply(另外,您可以应用和删除用更改的最后一个存储git stash pop):
(master) $ git stash apply On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: business/com/test/core/actions/Photo.c no changes added to commit (use "git add" and/or "git commit -a")
但是请注意,隐藏不会记住您正在处理的分支。在以上示例中,用户隐藏在master上。如果他们切换到dev分支dev并运行,git stash apply则将最后一个存储区放到dev分支上。
(master) $ git checkout -b dev Switched to a new branch 'dev' (dev) $ git stash apply On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: business/com/test/core/actions/Photo.c no changes added to commit (use "git add" and/or "git commit -a")