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

I am on a branch (let them say master), and I have some changes in my working directory. I get the job, but I need a lot of cleanup, so I would like to keep the current working directory in the branch just in case I need to return to it. I will definitely delete the branch later.

In addition, I want to continue working on the master EXACTLY where I was.

Now I am doing:

# 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 

In the future, I will delete the topic thread.

So, the above does exactly what I want, but it is a bit verbose. Is there an easier way to do this (besides just writing)?

+4
source share
3 answers

you can use

  git stash 

From the manual

Use git stash when you want to write the current state of the working directory and index, but want to return to a clean working directory. the command saves your local changes and returns the working directory according to the HEAD commit.

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

  git stash apply 

Or you can list your hidden changes with:

  git stash list 
+7
source
  $ 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 cannot do this.

+2
source

For completeness: if you want to have the correct branch (which has some advantages over the slash), you do not need to use plumbing commands, you can also do:

 git add -A . git commit -m "dirty" git branch dirtybranch git reset --hard HEAD^ 
+2
source

All Articles