Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

save

I'm on a branch (let's say master), and I have some changes in my working dir. I get stuff working, but needing a lot of cleanup - so I'd like to save my current working dir to a branch just in case I need to go back to it. I'll definitely be deleting the branch later.

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

Right now I do:

# 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

Later on, I'll delete the topic branch.

So, the above does exactly what I want, but its a bit verbose. Is there a simpler way to do this (besides just scripting it)?

like image 820
thebriguy Avatar asked Dec 09 '22 18:12

thebriguy


2 Answers

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
like image 79
Atropo Avatar answered Dec 11 '22 07:12

Atropo


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^
like image 30
Chronial Avatar answered Dec 11 '22 08:12

Chronial