tags:

views:

628

answers:

2

There is a git controlled folder on a server where the main branch is checked out and a whole pile of files have been modified and not committed. Is there a way for me to commit the changes to a separate branch so I can go back to a clean version?

ie I want to effecitvely undo all this persons changes but store them in another chance so if that person wants their changes they can switch to that branch.

(Yes I know this is not how git is designed to work but that is my situation!) Any ideas very much appreciated.

+3  A: 

You can always stash your changes.

git stash
git checkout -b bravenewmaster
git stash apply

Also keep in mind, that if you commit to the "wrong" branch you can always move that branch back, because branch is nothing but a pointer to a commit.

Michael Krelin - hacker
Why bother to stash if you're going to immediately re-apply the same changes? You may as well just do `git checkout -b bravenewmaster`. It's less typing and won't touch all the changed files unnecessarily.
Charles Bailey
Well, I haven't tried, but I assumed that `git` for whatever reason doesn't let `git checkout -b` when there are changes. Otherwise, why question? ;-)
Michael Krelin - hacker
Actually, for what I can tell by "I want to effecitvely undo all this persons changes but store them in another chance so if that person wants their changes they can switch to that branch" remark — stashing alone should be sufficient with no branching and applying.
Michael Krelin - hacker
If he's storing the changes (rather than 'stashing' temporarily) then it's probably more appropriate to have a commit (with a message) on a named branch than an stash that musn't be accidentally dropped. Both would work, though.
Charles Bailey
Of course I can't fully understand other people's intentions, but got a feeling that "I want to work on this instance, but you modified the hell of it, would you sidestep please and deal with it yourself later" ;-)
Michael Krelin - hacker
Seems slightly convoluted but heck it works too (:
corydoras
+8  A: 

First of all moving to a different branch based in the current HEAD is performed like this:

git checkout -b newbranch

Commit all the changes (assuming no newly added files, otherwise git add them):

git commit -a

Go back to the master branch:

git checkout master

The previously uncommitted changes will all be on the newbranch branch, and master will still be at the state it was without those changes.

Charles Bailey
*Explanation:* contents of working repository and staged changes do not belong to any branch. Simple creating new branch off HEAD (last revision) is sufficient for the new commit to go on just created branch `<newbranch>`.
Jakub Narębski
Thanks guys, I thought switching to a new branch might wipe away the changes. Coming from an SVN background I didn't think switching like this was possible. Testing it out now.
corydoras
Perfect, did exctly as you say! Git is slightly cool (:
corydoras