git:如何创建我当前工作的一个分支,但保留在我原来的分支上

标签 git save branch

我在一个分支上(假设是 master),我的工作目录有一些变化。我得到了一些东西,但需要大量清理 - 所以我想将我当前的工作目录保存到一个分支,以防我需要返回它。稍后我肯定会删除该分支。

此外,我想继续我原来的 master 工作。

现在我这样做:

# Save to topic branch
git checkout -b working-stuff
git commit -a -m "work in progress"

# go back to master and continue with the state is was in before
git checkout master
git checkout -- .
git reset

稍后,我将删除主题分支。

所以,上面的代码完全符合我的要求,但有点冗长。有没有更简单的方法来做到这一点(除了编写脚本之外)?

最佳答案

你可以使用

 git stash

来自手册

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

使用 git stash 你可以存储你的修改,然后你可以再次应用它们:

 git stash apply

或者您可以列出您 stash 的修改:

 git stash list

关于git:如何创建我当前工作的一个分支,但保留在我原来的分支上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13917636/

相关文章:

javascript - 使用 JavaScript 创建和保存文件

c++ - 以标准用户保存 'global'数据?

git - Julia git 错误

git - 两个 Git 分支名称指向同一个分支?

swift - 在全局数据类中存储和检索数据

swift - 在Xcode中,如何切换分支、 merge 变更、选择 merge 哪些变更?

svn - 使用 TortoiseSVN 进行分支和合并的最简单方法是什么?

version-control - 使用 TFS 过渡到分支

java - git 分支的有效 java 正则表达式模式是什么?

git - 如何从存储库 checkout 分支