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!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章