8

Possible Duplicate:
Git - Create a branch with current changes

I have done a whole lot of work on my project which I realise should have been done on another branch. If I create a branch now, will my current changes need to be checked in, or will they be wiped when the new branch is created? I'm rather new to GIT and am just trying to avoid making a newbie mistake.

Community
  • 1
  • 1
Nippysaurus
  • 20,110
  • 21
  • 77
  • 129

3 Answers3

10

If you create a new branch from the current HEAD using:

$ git checkout -b newbranchname

Then changes will NOT be overwritten.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 1
    Quite correct, as long as the OP hasn't committed any changes to the wrong branch yet. The important thing to understand is that a branch is merely a pointer to a commit, so when you do this, all you're doing is creating a new pointer to the current commit, and checking it out - but since it's the same commit, it needn't touch your work tree at all. – Cascabel Jan 29 '11 at 15:40
2

In your case, bdonlan's answer applies, since creating a new branch doesn't touch the working directory.

In more general cases where it would, Git will warn you and abort if you attempt to switch branches with working directory changes still present. Either way it's good about not losing changes.

In those cases, to move uncommitted working directory changes to a new branch, first save it to the Git stash:

git stash save

Then create and checkout that branch:

git checkout -b new-branch-name

Then pop your working directory changes from the Git stash:

git stash pop
Tung Nguyen
  • 12,655
  • 4
  • 19
  • 10
  • 1
    This doesn't make sense: if you're creating the new branch from your current HEAD, there's never any need to stash. – Cascabel Jan 29 '11 at 15:28
1

They will be moved to the new branch. But if you feel unsafe you can always take a backup of your local project directory first.

  • 5
    No, they won't be moved anywhere. Maybe I sound pedantic, but thinking of it like that is just going to mess up your instincts about this and other commands. – Cascabel Jan 29 '11 at 15:29
  • Let's just say the changes will be visible in the new branch. – jeremyjjbrown Jan 14 '14 at 22:12