简体   繁体   English

使用 git clone 后的本地分支

[英]Local branch after using git clone

After using git clone , I'm having a slight misunderstanding on how to use branches .使用git clone ,我对如何使用branches有点误解。 I have the same code on both the new branch and master branch .我在新branchmaster 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使用git branch 'branch name'似乎创建了一个分支,但是在我添加了一些新功能后,这些功能同时出现在新旧(主)分支上

Using the command git branch -a ;使用命令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.我期待使用新分支添加新功能,然后将它们合并到 master 中。 But it seems i didn't understand very well how to use branches after i use the command git clone.但是在我使用命令 git clone 之后,我似乎不太明白如何使用分支。

EDIT: Sorry, forgot to mention, i already used git checkout , and i know how to use it.编辑:对不起,忘了提,我已经使用过git checkout ,我知道如何使用它。 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.但是在我切换到新分支后,添加一些新功能,然后切换回旧分支(在本例中为 master),我在该分支(master)上也有新添加的功能。

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.克隆 repo 后,您将拥有本地 master 分支。 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.在新分支中进行更改后,您必须将这些更改添加到 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.您可以运行git status以查看所有文件都已暂存。

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.现在,如果您切换到 master 分支,这些更改将不存在,要获得它们,您必须将您的分支与 master 合并或 rebase。

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

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