git 检出并创建分支_Git分支介绍:如何在Git中删除,检出,创建和重命名分支...

git 检出并创建分支

git分支 (Git Branch)

Git’s branching functionality lets you create new branches of a project to test ideas, isolate new features, or experiment without impacting the main project.

Git的分支功能使您可以创建项目的新分支以测试想法,隔离新功能或进行实验,而不会影响主项目。

Table of Contents

目录

查看分支 (View Branches)

To view the branches in a Git repository, run the command:

要查看Git存储库中的分支,请运行以下命令:

git branch

To view both remote-tracking branches and local branches, run the command:

要查看远程跟踪分支和本地分支,请运行以下命令:

git branch -a

There will be an asterisk (*) next to the branch that you’re currently on.

您当前所在的分支旁边将有一个星号(*)。

There are a number of different options you can include with git branch to see different information. For more details about the branches, you can use the -v (or -vv, or --verbose) option. The list of branches will include the SHA-1 value and commit subject line for the HEAD of each branch next to its name.

您可以在git branch包含许多不同的选项,以查看不同的信息。 有关分支的更多详细信息,可以使用-v (或-vv--verbose )选项。 分支列表将在其名称旁边包含SHA-1值和每个分支的HEAD的提交主题行。

You can use the -a (or --all) option to show the local branches as well as any remote branches for a repository. If you only want to see the remote branches, use the -r (or --remotes) option.

您可以使用-a (或--all )选项显示存储库的本地分支以及任何远程分支。 如果只想查看远程分支,请使用-r (或--remotes )选项。

结帐分支 (Checkout a Branch)

To checkout an existing branch, run the command:

要签出现有分支,请运行以下命令:

git checkout BRANCH-NAME

Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes:

通常,除非您的工作目录干净,否则Git不会让您签出另一个分支,因为您会丢失所有未提交的工作目录更改。 您有三种选择来处理您的更改:

  1. trash them (see Git checkout for details) or

    丢弃它们( 有关详细信息,请参见Git结帐 )或

  2. commit them (see Git commit for details) or

    提交它们( 有关详细信息,请参见Git commit )或

  3. stash them (see Git stash for details).

    存放它们( 有关详细信息,请参阅Git存放 )。

创建一个新分支 (Create a New Branch)

To create a new branch, run the command:

要创建一个新分支,请运行以下命令:

git branch NEW-BRANCH-NAME

Note that this command only creates the new branch. You’ll need to run git checkout NEW-BRANCH-NAME to switch to it.

请注意,此命令仅创建新分支。 您需要运行git checkout NEW-BRANCH-NAME来切换到它。

There’s a shortcut to create and checkout a new branch at once. You can pass the -b option (for branch) with git checkout. The following commands do the same thing:

有一个快捷方式可以立即创建和签出新分支。 您可以通过git checkout传递-b选项(用于分支)。 以下命令执行相同的操作:

# Two-step method
git branch NEW-BRANCH-NAME
git checkout NEW-BRANCH-NAME

# Shortcut
git checkout -b NEW-BRANCH-NAME

When you create a new branch, it will include all commits from the parent branch. The parent branch is the branch you’re on when you create the new branch.

创建新分支时,它将包括父分支的所有提交。 父分支是创建新分支时所在的分支。

重命名分支 (Rename a Branch)

To rename a branch, run the command:

要重命名分支,请运行以下命令:

git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

# Alternative
git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAME

删除分支 (Delete a Branch)

Git won’t let you delete a branch that you’re currently on. You first need to checkout a different branch, then run the command:

Git不允许您删除当前所在的分支。 您首先需要签出其他分支,然后运行以下命令:

git branch -d BRANCH-TO-DELETE

# Alternative:
git branch --delete BRANCH-TO-DELETE

The branch that you switch to makes a difference. Git will throw an error if the changes in the branch you’re trying to delete are not fully merged into the current branch. You can override this and force Git to delete the branch with the -D option (note the capital letter) or using the --force option with -d or --delete:

您切换到的分支会有所作为。 如果您要删除的分支中的更改未完全合并到当前分支中,则Git将引发错误。 您可以使用-D选项(请注意大写字母)或将--force选项与-d--delete一起使用来强制Git删除分支:

git branch -D BRANCH-TO-DELETE

# Alternatives
git branch -d --force BRANCH-TO-DELETE
git branch --delete --force BRANCH-TO-DELETE

比较分支 (Compare Branches)

You can compare branches with the git diff command:

您可以使用git diff命令比较分支:

git diff FIRST-BRANCH..SECOND-BRANCH

You’ll see colored output for the changes between branches. For all lines that have changed, the SECOND-BRANCH version will be a green line starting with a ”+”, and the FIRST-BRANCH version will be a red line starting with a ”-“. If you don’t want Git to display two lines for each change, you can use the --color-words option. Instead, Git will show one line with deleted text in red, and added text in green.

您将看到分支之间更改的彩色输出。 对于所有已更改的行, SECOND-BRANCH版本将以“ +”开头的绿线,而FIRST-BRANCH版本将以“-”开头的红线。 如果您不希望Git为每次更改显示两行,则可以使用--color-words选项。 取而代之的是,Git将显示一行,其中删除的文本显示为红色,而添加的文本显示为绿色。

If you want to see a list of all the branches that are completely merged into your current branch (in other words, your current branch includes all the changes of the other branches that are listed), run the command git branch --merged.

如果要查看已完全合并到当前分支中的所有分支的列表(换句话说,当前分支包括列出的其他分支的所有更改),请运行命令git branch --merged

帮助Git分支 (Help with Git Branch)

If you forget how to use an option, or want to explore other functionality around the git branch command, you can run any of these commands:

如果您忘记了如何使用选项,或者想探索git branch命令周围的其他功能,则可以运行以下任何命令:

git help branch
git branch --help
man git-branch

更多信息: (More Information:)

翻译自: https://www.freecodecamp.org/news/git-branch-explained-how-to-delete-checkout-create-and-rename-a-branch-in-git/

git 检出并创建分支

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值