PepeHands
PepeHands

Reputation: 1406

editing non-master branch in git

I created new branch and checkouted to it

git branch new_branch
git checkout new_branch

and edited some file in it

vim main.cpp
do_something

git status shows that main.cpp was changed. Now I going back to master branch

git checkout master

and this changes are in master, too, despite I've changed file in new_brach.

What am I doing wrong?

Upvotes: 1

Views: 195

Answers (3)

MassiveData
MassiveData

Reputation: 101

I dont see anything wrong,please check it again and try

$ git checkout -b iss53

To create a branch and switch to it at the same time, you can run the git checkout command with the -b switch

Upvotes: 0

uraimo
uraimo

Reputation: 19791

Your changes are nowhere, they are still only modified files not yet committed, you can safely switch to new_branch again, you need to be on new_branch and commit to add your changes there. Once you have committed on new_branch and switch branch master will be clean. If you want to get your commit in master you can either merge both branches os cherry-pick the single commit.

Upvotes: 2

thenaterhood
thenaterhood

Reputation: 167

Did you commit your changes?

Your order of events should be:

  1. Switch to branch
  2. Make your changes
  3. Commit your changes
  4. Switch to your original branch.

Unless you commit, git will not leave your changes behind when you switch branches, since as far as git is concerned, they never happened. Committing in git tells git to record your changes on the branch you're currently on.

Upvotes: 1

Related Questions