简体   繁体   English

git 从 master 创建一个分支

[英]git creating a branch from the master

I am new to using git and come from SVN background.我是使用 git 的新手并且来自 SVN 背景。 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. master 分支中的所有文件都已过时,我需要签入一组新文件。 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?我是否需要在 master 分支中才能创建新分支?
  2. Command to create a new branch so that the master branch is untouched.命令创建一个新分支,以便主分支保持不变。
  3. How to switch to the master/mybranch branch later.稍后如何切换到 master/mybranch 分支。
  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. 1:我认为你可以在任何你想要的分支。

2: git checkout -b newbranch , this create and change to the new branch 2: git checkout -b newbranch ,这会创建并更改到新分支

3: git checkout branchtoChange 3: git checkout branchtoChange

4: git branch 4: git branch

Also, take a look to the link @dalen post in the comment.另外,请查看评论中的@dalen 帖子链接。 Some time ago I created a cheatSheet of git based on that book, Git scm前段时间我根据那本书Git scm创建了一个 git 的作弊表

  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 . 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.您还可以使用git branch -a列出远程分支。

You can refer to this GitHub forum as well for basic branching and merging concepts.您也可以参考此GitHub 论坛,了解基本的分支和合并概念。

  1. Yes if you need changes from master, you need to checkout in new branch from master.是的,如果您需要从 master 进行更改,则需要从 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. git checkout 让你切换到不同的分支。

  2. When you run git branch you will get all branch names with asterstik on it.当您运行 git branch 时,您将获得所有带有 asterstik 的分支名称。

Thanks谢谢

1) Yes, if you wish to create a new branch from the master branch then you need to be in master branch. 1) 是的,如果你想从 master 分支创建一个新分支,那么你需要在 master 分支中。 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 .关键是,当您通过在任何分支A 中创建新分支B 时,它将使用您在分支A 中更新到的内容创建分支B

2) The command to create a new branch, 2)创建新分支的命令,

git branch NewBranchName, git 分支新分支名称,

git checkout NewBranchName git checkout 新分支名称

(or) (或者)

git checkout -b NewBranchName (It will create a new branch and checkout to the new branch in a single command) git checkout -b NewBranchName (它将在单个命令中创建一个新分支并结帐到新分支)

Then push the new branch to the remote by the following command,然后通过以下命令将新分支推送到远程,

git push origin master git push origin master

3) git checkout NewBranchName (switches to NewBranchName branch when you are in any branch) 3) git checkout NewBranchName (当你在任何分支时切换到 NewBranchName 分支)

or或者

git checkout master (switches to master when you are in any branch) git checkout master (当你在任何分支时切换到master)

4) git branch 4) git 分支

The pointer * represents that, In which branch you are right now.指针 * 表示,您现在在哪个分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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