简体   繁体   中英

Local branch after using git clone

After using git clone , I'm having a slight misunderstanding on how to use branches . I have the same code on both the new branch and master branch . How to properly create one?

Using git branch 'branch name' seems to create a branch, but after I add some new features, those features appears on both the new and old (master) branch

Using the command git branch -a ; I get this:

new branch
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master

I was expecting to use the new branch to add new features, and then merge them into master. But it seems i didn't understand very well how to use branches after i use the command git clone.

EDIT: Sorry, forgot to mention, i already used git checkout , and i know how to use it. But still after i switch to the new branch, add some new features, then switch back to the old branch(in this case, master) i have the newly added features on that branch (master) too.

If the branch exists already you do

git checkout [branch name]

If the branch doesn't already exist you can both create it and switch to it in a single command

git checkout -b [branch name]

After you switch to the branch you created- any changes you make will reflect on that branch.

Here's the workflow for you -

Once you clone the repo, you will have local master branch. You can create a new branch from it by doing -

git checkout -b new_branch

This will create a new branch and will also check it out. Once you make changes in your new branch, you will have to add those changes to staging area of git. Staging area is the one where changes ready to be committed reside. You can add all your changes to staging area by -

git add .

Or, you can selectively add files to by -

git add filePath

Once there are some files to staging area, you can commit them. You can run git status to see what all files are staged.

To commit the staged files, you have to run -

git commit -m "Commit Message"

This will commit the files to your branch. Now if you switch to master branch, these changes will not be there and to get them, you will have to either merge or rebase your branch with master.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM