简体   繁体   中英

git creating a branch from the master

I am new to using git and come from SVN background. I have checkout a project containing all the files in the default master branch. All the files in the master branch are outdated and I need to checkin a new set of files. But I am planning to create a new branch for the same and continue my development in the branch.

  1. Do I need to be in the master branch to create a new branch?
  2. Command to create a new branch so that the master branch is untouched.
  3. How to switch to the master/mybranch branch later.
  4. Command to know which branch I am currently working on?

Please let me know as I do not want to screw up my existing master branch.

1: I think you can be in whatever branch you want.

2: git checkout -b newbranch , this create and change to the new branch

3: git checkout branchtoChange

4: git branch

Also, take a look to the link @dalen post in the comment. Some time ago I created a cheatSheet of git based on that book, Git scm

  1. You can be in any branch when creating a new branch. The point is, when you create a new branch, it will fork the new branch from your current branch only.
  2. git checkout -b newBranchName
  3. git checkout branchNameToSwitch
  4. git branch . It will list all the branches in your local repository. Your current branch will be marked with * . You can also use git branch -a to list remote branches as well.

You can refer to this GitHub forum as well for basic branching and merging concepts.

  1. Yes if you need changes from master, you need to checkout in new branch from master.

  2. You can create branch using two approaches

git branch <branch name> git checkout <branch name>

OR

git checkout -b <branch name>
  1. git checkout makes you switch to different branches.

  2. When you run git branch you will get all branch names with asterstik on it.

Thanks

1) Yes, if you wish to create a new branch from the master branch then you need to be in master branch. The point is, when you create a new branch B by being in any branch A , it will create the branch B with the contents you have updated to till date in branch A .

2) The command to create a new branch,

git branch NewBranchName,

git checkout NewBranchName

(or)

git checkout -b NewBranchName (It will create a new branch and checkout to the new branch in a single command)

Then push the new branch to the remote by the following command,

git push origin master

3) git checkout NewBranchName (switches to NewBranchName branch when you are in any branch)

or

git checkout master (switches to master when you are in any branch)

4) git branch

The pointer * represents that, In which branch you are right now.

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