Git 分支相关操作

这篇具有很好参考价值的文章主要介绍了Git 分支相关操作。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 创建一个分支

Create a new directory and initialize a Git repository. We are going to create a directory named “tutorial”.

$ mkdir tutorial
$ cd tutorial
$ git init
Initialized empty Git repository in /Users/eguchi/Desktop/tutorial/.git/

进入这个tutorial文件夹,创建一个文件“myfile.txt”,在其中添加如下文本:

Git commands even a monkey can understand

然后执行如下的addcommit命令:

$ git add myfile.txt
$ git commit -m "first commit"
[master (root-commit) a73ae49] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 myfile.txt

At this point, the history tree should look like this.

Git 分支相关操作

Let’s create a new branch with the name “issue1”.

$ git branch issue1

If you do not specify any parameters, the branch command will list all branches that correspond to this repository. The asterisk indicates the current active branch.

$ git branch
      issue1
      * master

At this point, the history tree should look like this.

Git 分支相关操作

2 切换分支

Switch over to the branch “issue1” when you want to add new commits to it.

$ git checkout issue1
Switched to branch 'issue1'

This history tree should look like this at the moment.

Git 分支相关操作

Once you are on the “issue1” branch, you can start adding commits to it.

编辑我们刚才创建的myfile.txt文件,在其中添加一行,使得整个文件变为如下两行:

Git commands even a monkey can understand
add: Register a change in an index

Let’s add the bold text below to myfile.txt and commit the change.

$ git add myfile.txt
$ git commit -m "append description of the add command"
[issue1 b2b23c4] append description of the add command1 files changed, 1 insertions(+), 0 deletions(-)

Our history tree will now look like this.

Git 分支相关操作

3 分支合并

Let’s merge “issue1” with “master”

我们可以使用如下的命令来合并分支:

$ git merge

By running the command above, the specified commit will be merged to the current active branch. Most of the time, you will want to merge a branch with the current active branch and you can do so by passing in the branch name in .

To merge commits into the master branch, let’s now switch over to the master branch.

$ git checkout master
Switched to branch 'master'

在合并前,查看myfile.txt文件,可以看到其内容如下:

Git commands even a monkey can understand

可以发现,我们在issue1分支上进行的修改并没有出现master分支上的myfile.txt中。

此时,我们执行merge操作:

$ git merge issue1
Updating 1257027..b2b23c4
Fast-forward
myfile.txt |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)

The position of the master branch will now move over to that of “issue1”. A fast-forward merge has been executed here.

Git 分支相关操作

此时,查看myfile.txt文件:

Git commands even a monkey can understand
add: Register a change in an index

此时可以看到,我们应用在issue1分支上的修改出现在了master分支上。

4 删除分支

Now that “issue1” has been successfully merged with “master”, we can delete it.

We can delete a branch by calling the branch command and passing in the -d option, followed by the branch name. 如下的命令可以删除issue1分支:

$ git branch -d issue1
Deleted branch issue1 (was b2b23c4).

5 并行操作

Branching allows us to work in multiple parallel workspaces.

Let’s create two branches. Create one with the name “issue2″ and another with the name”issue3”, then switch over to “issue2”.

$ git branch issue2
$ git branch issue3
$ git checkout issue2
Switched to branch 'issue2'
$ git branch
  * issue2
    issue3
    master

Git 分支相关操作

修改myfile.txt文件,在下面添加一行,使得其内容变为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index

然后执行如下操作:

$ git add myfile.txt
$ git commit -m "append description of the commit command"
[issue2 8f7aa27] append description of the commit command
1 files changed, 2 insertions(+), 0 deletions(-)

此时各分支状态如下:

Git 分支相关操作

我们切换到issue3分支:

$ git checkout issue3
Switched to branch 'issue3'

“issue3” currently has the same history/content as the master branch. It will not include the recent change that we have just made. This is because the recent change has been commited to the “issue2” branch.

此时修改myfile.txt文件,添加一行修改为:

Git commands even a monkey can understand
add: Register a change in an index
pull: Obtain the content of the remote repository

然后提交这次修改:

$ git add myfile.txt
$ git commit -m "append description of the pull command"
[issue3 e5f91ac] append description of the pull command
1 files changed, 2 insertions(+), 0 deletions(-)

此时分支状态如下:

Git 分支相关操作

We have now added two different line of texts to two different branches in parallel.

6 解决分支冲突

Now let’s merge branches “issue2” and “issue3” into the master branch.

We will switch over to “master” and merge “issue2” with it.

$ git checkout master
Switched to branch 'master'

$ git merge issue2
Updating b2b23c4..8f7aa27
Fast-forward
myfile.txt |    2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

A fast-forward merge has now been executed.

Git 分支相关操作

Let’s try to merge “issue3” into “master”.

$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.

Git has identified a conflict and will not allow you to automatically merge “issue3” with “master”. The conflict here pertains to the same line on myfile.txt with different content on both branches.

由于我们尝试合并时发生的conflict,此时myfile.txt文件变为如下内容:

Git commands even a monkey can understand
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

You will find some strange markers being added to myfile.txt. These markers were added by Git, giving us information regarding which line of the file is related to the conflict.

我们可以手动修改冲突,将myfile.txt文件修改为如下内容:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

Once we are done with resolving the conflict, let’s commit the change.

$ git add myfile.txt
$ git commit -m "merge issue3 branch"
# On branch master
nothing to commit (working directory clean)

此时文件内容为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

The revision history will now look like the one below. A new merge commit has been created as a result of the conflict resolution. The master branch is now pointing to the latest merge commit. This is a non fast-forward merge.

Git 分支相关操作

整个rebase的过程是错误的,需要重新整理

7 Rebase a branch

Another approach we can take to integrate “issue3” branch into the master branch is by using the rebase command. Using rebase, we can streamline and clean our history tree just like how we have described earlier.

Let’s start by undoing the previous merge.

$ git reset --hard HEAD~

Git 分支相关操作

Switch over to “issue3” branch and rebase onto the master branch.

$ git checkout issue3
Switched to branch 'issue3'

$ git rebase master
  First, rewinding head to replay your work on top of it...
  Applying: append description of the pull command
  Using index info to reconstruct a base tree...
  :13: new blank line at EOF.
  +
  warning: 1 line adds whitespace errors.
  Falling back to patching base and 3-way merge...
  Auto-merging myfile.txt
  CONFLICT (content): Merge conflict in myfile.txt
  Failed to merge in the changes.
  Patch failed at 0001 append description of the pull command
  
  When you have resolved this problem run "git rebase --continue".
  If you would prefer to skip this patch, instead run "git rebase --skip".
  To check out the original branch and stop rebasing run "git rebase --abort".

When a conflict occurs during the rebase, you will have to resolve it immediately in order to resume the rebase operation.

此时myfile.txt文件内容如下:

Git commands even a monkey can understand
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

Once the conflict is resolved, you can resume rebase with the –continue option. If you wish to quit and rollback the rebase operation, you can do so by passing in the –abort option.

此时我们可以将文件修改为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

然后执行:

$ git add myfile.txt
$ git rebase --continue
Applying: append description of the pull command

Git 分支相关操作

With the “issue3” branch rebased onto “master”, we can now issue a fast-forward merge.

Switch over to the master branch and merge “issue3” with “master”.

$ git checkout master
Switched to branch 'master'

$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
myfile.txt |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)

The content of myfile.txt should now be identical to the one that we got from the previous merge. The revision history now should look like the following.

Git 分支相关操作文章来源地址https://www.toymoban.com/news/detail-460773.html

到了这里,关于Git 分支相关操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Git 分支教程:详解分支创建、合并、删除等操作

    Git是一种强大的分布式版本控制系统,它的分支功能使得团队协作和代码管理变得更加灵活和高效。分支可以让开发人员在不影响主线开发的情况下进行并行开发和实验性工作。本篇博客将详解Git分支的创建、合并、删除等操作,帮助你更好地理解和使用Git的分支功能。 在开

    2024年02月05日
    浏览(52)
  • git基于原有的分支拉取(创建)一个新的分支

    git checkout -b newbranch origin/oldbranchname newbranch:你要创建的分支的名子 oldbranchname:原来的分支(你要基于的分支) git push --set-upstream origin newbranch newbranch:你刚刚创建的新分支的名字 git push origin newbranch 或者直接 git push newbranch:你刚刚创建的新分支的名字

    2024年02月15日
    浏览(52)
  • git创建一个本地分支,并从远程分支拉取代码

    Git 是一种分布式版本控制系统,广泛用于管理项目的源代码。它可以追踪文件的修改、记录历史变更、协调多人合作开发,并提供了回滚、分支管理、合并等功能。要创建一个本地分支并从远程分支拉取代码,你可以按照以下步骤进行操作: 首先,确认远程分支存在于远程

    2024年02月14日
    浏览(56)
  • 用git在本地仓库创建一个新的分支,并且将当前分支切换到新创建的分支上

    1 用下面的命令创建本地仓库的新分支 2 用下面的命令将当前分支切换到新创建的分支上  注意:下面的chatGPT说的是错的,正确的命令是git checkout -b  3 可以用简写的方式,可以同时实现创建本地分支和切换到新创建的分支上

    2024年02月09日
    浏览(64)
  • 【小吉带你学Git】idea操作(2)_版本和分支的相关操作

    🎊专栏【Git】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【Counting Stars 】 欢迎并且感谢大家指出小吉的问题🥰 编译环境可能因为idea的版本不同而不同,请以实际的为准。 将IDEA与Git集成有很多用处,它能够为开发人员带来许多便利和优势。以下是将

    2024年02月14日
    浏览(43)
  • Git gui教程---第八篇 Git gui的使用 创建一个分支

    一般情况下一个主分支下代码稳定的情况下会新建出一个分支,然后在分支上修改,修改完成稳定后再合并到主分支上。 或者几个人合作写一份代码,每个人各一个分支,测试稳定再合并到主分支上。 在git gui选择菜单栏“分支”,选择新建 填写名称,点击新建 我在新的分

    2024年02月11日
    浏览(50)
  • GIT操作:把当前仓库的一个分支push到另一个仓库的指定分支

            有时候我们想把当前仓库A的一个指定分支1 推给 另一个仓库B的另一个指定分支2, 可以通过2个主要命令git remote xxx 和 git push newOrigin 当前分支:目标分支 完成。 示例 假设有2个仓库rep1和rep2 rep1:当前仓库  rep2:目标仓库 我们想把当前仓库[rep1]的指定分支[bran

    2024年02月03日
    浏览(59)
  • git同一分支上多个commit合成一个的操作

    1、首先git log——查看当前分支的提交记录 ,想要把圈起来的commit合成一个, 2、开始合并,敲下面这个命令 git rebase -i commitId commitId即找出要合并的几个commit的前一个commit的ID -i 的参数是不需要合并的 commit 的 hash 值,这里指的是第一条 commit, 接着键盘摁下 i 键,我们就进

    2024年01月19日
    浏览(75)
  • git push 错误 error: remote unpack failed: unable to create temporary object directory

    我们在向git服务器提交代码时候,遇到问题如下问题: git push 错误 error: remote unpack failed: unable to create temporary object directory 我们在使用git push本地代码实收遇到问题: 修改git repositories 的权限,执行如下命令:

    2024年02月07日
    浏览(39)
  • Git-Git常用命令、常见操作,创建镜像&切换新仓库、首次克隆、stash备份、查看及切换用户、修改默认分支、是否要保留本地修改等

    Git在日常开发中经常使用,但有时候还是会忘记or遇到些奇奇怪怪的问题。以此记录,提醒自己~不定期更新~ 首次克隆仓库及其模块 仓库首次拉取模块 更新子模块 保存修改 放弃修改==回退 添加当前目录下的所有文件到暂存区 提交暂存区到本地仓库中 查看项目当前状态

    2024年02月05日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包