git: How to create a branch of my current work, but stay on my original branch

浪尽此生 提交于 2019-12-05 13:15:10

You can use

 git stash

From the manual

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.

With git stash you stash your modifications, then you can apply them again with:

 git stash apply

Or you can list your stashed modifications with:

 git stash list

For completeness sake: If you want a proper branch (which has some advantages over a stash), you don’t need to use plumbing commands, you can also do:

git add -A .
git commit -m "dirty"
git branch dirtybranch
git reset --hard HEAD^
$ git add bar

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       new file:   bar
#

$ git branch newbar $(echo wip | git commit-tree \
  $(git write-tree) -p $(git rev-parse HEAD))

$ git lola --name-status
* e3f88c8 (newbar) wip
| A     bar
* a7b1a76 (HEAD, master) foo
  A     foo

Just because you can doesn’t mean you should.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!