Rob Truxal
Rob Truxal

Reputation: 6408

How do I fix the fact that I'm working on the wrong branch

Ok so I meant to be working on a branch called directory-layout, but it turns out that I am working on a branch called master. This is a problem.

I have not yet performed git add . nor git commit -m "I've made a horrendus mistake I'm sorry"

What do I do to add my changes to another (or new) branch and why?

Upvotes: 6

Views: 2519

Answers (2)

Boshika Tara
Boshika Tara

Reputation: 274

Here is what I would try doing.

  git checkout -b <newbranch> 

Upvotes: 2

VonC
VonC

Reputation: 1323115

If that branch is a new one, you can simply create it:

git checkout -b anewbranch
git add .
git commit -m "message"

But if that branch is an old one, you can cherry-pick the commit instead:

  • add, and commit
  • switch to the old branch
  • git cherry-pick master

Then reset master to its previous commit

git checkout master
git reset --hard @~1

Another approach would be to use git stash, then switch to the old branch and stash apply.

Upvotes: 5

Related Questions