proper way to branch using git
So I created a new branch off master called branch-a and then switched to it. I made some modifications, switched back to master to make sure that no changes have been made since, but I see all of my modifications that were made in branch-a in the master branch. Is this right? I would expect master to not have any modifications if I made all my mods in branch-a without merging yet. Here’s what I did:
Started with a clean, up-to-date master branch.
- git branch -b branch-a
- make modifications
- git checkout master
- git status
- I see all the modifications that I made in branch-a
Is this correct? Is it because I didn’t commit branch-a before switching to master?
2 Solutions collect form web for “proper way to branch using git”
I guess there is a step missing between 2. and 3.:
2a. add and commit the changes:
git add -A git commit -m "describe your changes"
First command adds all changed files to the index,the
second command commits everything inside the index.
Now you can switch back to
EDIT: checking out a branch changes the repository your changes would be commited to (and syncs unchanged files in the working directory with the HEAD of the branch). Uncommited changes remain in the working directory, added changes remain in the index. No merging takes place until you say so. If this does not clarify things, take a look at the opening part of this article for a great explanation of git trees
git branch separates the action of creating a branch from actually switching to it. Also, you need to commit changes before they exist on a branch.
git branch -b branch-a
This creates a branch named
branch-a, but does not check it out, so you are still on
master. If you want to work on
branch-a, you need to do a
git checkout branch-a here, or replace the above command with
git checkout -b branch-a master.
Assuming you are on the branch you want to be on,
git add <modified files>; git commit at this point will actually create a new commit on the branch containing those changes.
git checkout master
You were already on master, so this probably doesn’t do much.
git attempts to not lose uncommitted changes, so your working directory should still look the same as before you
Should report that you are on
master and list the uncommitted changes you have made.
I see all the modifications that I made in branch-a
You see all the modifications you made, but you did not make them in