githug新增第41关rebase_onto解决方案

githug新增了一关,原题为:

E:\githug\git_hug>githug reset 41
*****************************************************
*                 Githug                            *
*****************************************************
resetting level

Name: rebase_onto
Level: 41
Difficulty: **

You have created your branch from `wrong_branch` and already made some commits, 
and you realise that you needed to create your branch from `master`. 
Rebase your commits onto `master` branch so that you don't have `wrong_branch` commits.

等不及的伙伴可以点击这里,直接带你找到答案。

尝试了几种方法未果后…

让我们进入官网看看 , 内容如下:

difficulty 2

description "You have created your branch from `wrong_branch` and already made some commits, \
and you realise that you needed to create your branch from `master`. \
Rebase your commits onto `master` branch so that you don't have `wrong_branch` commits."

setup do
  readme_file  = "README.md"
  authors_file = "authors.md"

  repo.init
  FileUtils.touch(authors_file)
  File.open(authors_file, "w") { |f| f << "https://github.com/janis-vitols\n" }
  repo.add(authors_file)
  repo.commit_all("Create authors file")

  repo.git.native :checkout, { "b" => true }, "wrong_branch"
  File.open(authors_file, "w") { |f| f << "None\n" }
  repo.add(authors_file)
  repo.commit_all("Wrong changes")

  repo.git.native :checkout, { "b" => true }, "readme-update"
  FileUtils.touch(readme_file)
  File.open(readme_file, "a") { |f| f << "# SuperApp\n" }
  repo.add(readme_file)
  repo.commit_all("Add app name in readme")
  File.open(readme_file, "a") { |f| f << "## About\n" }
  repo.add(readme_file)
  repo.commit_all("Add `About` header in readme")
  File.open(readme_file, "a") { |f| f << "## Install\n" }
  repo.add(readme_file)
  repo.commit_all("Add `Install` header in readme")
end

solution do
  repo.commits("readme-update").each { |commit| return false if commit.message ==  "Wrong changes" }
  return false unless repo.commits("readme-update").length == 4
  return false unless File.readlines("authors.md").include?("https://github.com/janis-vitols\n")

  true
end

hint do
  puts "You want to research the `git rebase` commands `--onto` argument"
end

查找到git rebase中有以下用法:

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic
       branch from the latter branch, using rebase --onto.

       First let's assume your topic is based on branch next. For example, a feature developed in topic depends on some
       functionality which is found in next.

               o---o---o---o---o  master
                    \
                     o---o---o---o---o  next
                                      \
                                       o---o---o  topic

       We want to make topic forked from branch master; for example, because the functionality on which topic depends was
       merged into the more stable master branch. We want our tree to look like this:

               o---o---o---o---o  master
                   |            \
                   |             o'--o'--o'  topic
                    \
                     o---o---o---o---o  next

       We can get this using the following command:

           git rebase --onto master next topic

所以,最终答案即为:

git rebase --onto master wrong_branch readme-update
通关
E:\githug\git_hug>git rebase --onto master wrong_branch readme-update
First, rewinding head to replay your work on top of it...
Applying: Add app name in readme
Applying: Add `About` header in readme
Applying: Add `Install` header in readme

E:\githug\git_hug>githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值