通關(guān)Githug(一)


原文: 通關(guān)Githug(一)
date: 2017-12-25 12:39:04


[TOC]

一. 序言

第一眼看到githug, 以為是把github拼錯.

原來… 是個游戲!

關(guān)于Git的學(xué)習(xí), 大四寫過一篇: git小玩 .

雖然很多IDE都有集成git插件, 在了解git命令和使用場景后再去使用IDE才會更加明白

甚至我認(rèn)為很多操作使用git bash不比IDE效率慢

二. 介紹

當(dāng)然, githug不是什么RPG, FPS之類的游戲.

而是一個圍繞git知識, 每一關(guān)都會幫你自動搭建好實驗場景, 告訴你任務(wù)背景, 來完成這些關(guān)卡.

其實這個跟github提供的在線練習(xí)很像, 但是我覺得githug更加好玩!

github地址: https://github.com/Gazler/githug

花了一天時間去通關(guān). 當(dāng)中也有過不去的然后去翻譯和查通關(guān)攻略了

三. 安裝使用

1. 安裝

Githug可以使用在Linux, Windows, OS X下.

我以 CentOS 7 為例:

在安裝Githug之前首先確認(rèn)有Ruby環(huán)境.

查看是否安裝: ruby --version

這里我選擇自動安裝的方式: sudo yum install ruby

接著通過gem安裝githug: gem install githug

安裝完成.

2. 使用

Githug只有四個游戲命令, 很簡單:

  • githug play: 開始玩, 會驗證是否完成當(dāng)前關(guān)卡, 完成則進入下一關(guān)(可縮寫成githug),

    沒完成則會提示你并提示任務(wù)要求

  • githug hint: 給你一點當(dāng)前關(guān)卡的提示

  • githug reset: 重置當(dāng)前關(guān)卡

  • githug levels: 列出所有關(guān)卡

在第一次輸入githug開始游戲時, 會提示No githug directory found, do you wish to create one?

意思要初始化一個游戲目錄. 輸入y即可創(chuàng)建.

然后會出現(xiàn)一個git_hug目錄, 以后所有關(guān)卡都會在該目錄下生成文件和git對象. 每完成一關(guān)都會重新初始化游戲場景.

3. 關(guān)卡列表

為了方便查看, 所以列出所有關(guān)卡. 這里摘抄一個wiki.

關(guān)卡名稱 學(xué)習(xí)內(nèi)容 Git 命令
第1關(guān) init 初始化倉庫 git init
第2關(guān) config 設(shè)置用戶名和電子郵箱地址 git config
第3關(guān) add 把文件添加到暫存區(qū) git add
第4關(guān) commit 提交 git commit
第5關(guān) clone 克隆遠程倉庫 git clone
第6關(guān) clone_to_folder 克隆遠程倉庫,并指定本地目錄名 git clone
第7關(guān) ignore 配置不被 Git 管理的文件 vim .gitignore
第8關(guān) include 配置不被 Git 管理的文件 vim .gitignore
第9關(guān) status 查看倉庫狀態(tài) git status
第10關(guān) number_of_files_committed 查看倉庫狀態(tài) git status
第11關(guān) rm 刪除文件 git rm
第12關(guān) rm_cached 從暫存區(qū)中移除文件,系 git add 的逆操作 git rm –cached
第13關(guān) stash 保存而不提交 git stash
第14關(guān) rename 文件改名 git mv
第15關(guān) restructure 整理目錄結(jié)構(gòu)
第16關(guān) log 查詢?nèi)罩?/td> git log
第17關(guān) tag 打標(biāo)簽 git tag
第18關(guān) push_tags 把標(biāo)簽推送到遠程倉庫 git push –tags
第19關(guān) commit_amend 修改最后一次提交 git commit –amend
第20關(guān) commit_in_future 指定提交的日期 git commit –date
第21關(guān) reset 從暫存區(qū)中移除文件,系 git add 的逆操作 git reset
第22關(guān) reset_soft 撤銷提交,系 git commit 的逆操作 git reset –soft
第23關(guān) checkout_file 撤銷對一個文件的修改 git checkout
第24關(guān) remote 查詢遠程倉庫 git remote
第25關(guān) remote_url 查詢遠程倉庫的 URL git remote -v
第26關(guān) pull 從遠程倉庫拉取更新 git pull
第27關(guān) remote_add 添加遠程倉庫 git remote
第28關(guān) push 把提交推送到遠程倉庫 git push
第29關(guān) diff 查看文件被修改的細節(jié) git diff
第30關(guān) blame 查詢每一行代碼被誰編輯過 git blame
第31關(guān) branch 創(chuàng)建分支 git branch
第32關(guān) checkout 切換分支 git checkout
第33關(guān) checkout_tag 切換到標(biāo)簽 git checkout
第34關(guān) checkout_tag_over_branch 切換到標(biāo)簽 git checkout
第35關(guān) branch_at 在指定的提交處創(chuàng)建分支 git branch
第36關(guān) delete_branch 刪除分支 git branch -d
第37關(guān) push_branch 推送分支到遠程倉庫 git push
第38關(guān) merge 合并分支 git merge
第39關(guān) fetch 從遠程倉庫抓取數(shù)據(jù) git fetch
第40關(guān) rebase 變基合并 git rebase
第41關(guān) repack 重新打包 git repack
第42關(guān) cherry-pick 合并分支上指定的提交 git cherry-pick
第43關(guān) grep 搜索文本 git grep
第44關(guān) rename_commit 修改歷史提交的說明 git rebase -i
第45關(guān) squash 把多次提交合并成一次提交 git rebase -i
第46關(guān) merge_squash 合并分支時把多次提交合并成一次提交 git merge –squash
第47關(guān) reorder 調(diào)整提交順序 git rebase -i
第48關(guān) bisect 用二分法定位 bug git bisect
第49關(guān) stage_lines 添加文件的部分行到暫存區(qū) git add –edit
第50關(guān) file_old_branch 查看 Git 上的操作歷史 git reflog
第51關(guān) revert 取消已推送到遠程倉庫的提交 git revert
第52關(guān) restore 恢復(fù)被刪除的提交 git reset –hard
第53關(guān) conflict 解決沖突
第54關(guān) submodule 把第三方庫當(dāng)作子模塊 git submodule
第55關(guān) contribute 捐獻

四. 開始游戲

Level 1: init

A new directory, git_hug, has been created; initialize an empty repository in it.

git_hug目錄下初始化一個倉庫

[root@pipeline-cloud-test02 git_hug]# git init
Initialized empty Git repository in /home/githug/git_hug/.git/
[root@pipeline-cloud-test02 git_hug]# ls -a
.  ..  .git  .gitignore  .profile.yml
[root@pipeline-cloud-test02 git_hug]# githug
Congratulations, you have solved the level!

空目錄和有文件的目錄都可以初始化, 可以看到隱藏文件.git, 如同.svn一樣. 該目錄會被git所管理.

Level 2: config

Set up your git name and email, this is important so that your commits can be identified.

配置用戶名和郵箱是為了提交代碼時的團隊標(biāo)識

[root@pipeline-cloud-test02 git_hug]# git config --add user.name thank
[root@pipeline-cloud-test02 git_hug]# git config --add user.email coderthank@163.com
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is your name? thank
What is your email? coderthank@163.com
Your config has the following name: thank
Your config has the following email: coderthank@163.com
Congratulations, you have solved the level!

補充:

git config --add [--global/--local] user.name xxx # 增加name配置信息
git config --get [--global/--local] user.name xxx # 查看name配置信息
# --global/--local參數(shù)用來表示是全局配置還是本地配置
# email的話換成user.email

git config –list # 查看git全局配置

當(dāng)然, 有配置就會有配置的記錄文件. 你可以到~/.gitconfig去編輯配置信息

Level 3: add

There is a file in your folder called ‘README’, you should add it to your staging area.

添加README文件到暫存區(qū)

[root@pipeline-cloud-test02 git_hug]# git add README
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

補充:

首先補充一個圖:

[圖片上傳失敗...(image-1e51ef-1558407156745)]

關(guān)于這三個工作區(qū)域:

  • History 也可以叫repository(git倉庫): 最終確定的文件能保存到倉庫, 成為一個新的版本, 并且對他人可見
  • Staging area (暫存區(qū),Cache, Index): 暫存已經(jīng)修改的文件
  • Working directory(工作區(qū)): 也就是你實際看見的工作目錄

Level 4: commit

The ‘README’ file has been added to your staging area, now commit it.

把暫存區(qū)的README文件提交

[root@pipeline-cloud-test02 git_hug]# git commit -m "add README file"
[master (root-commit) a3317a2] add README file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 5: clone

Clone the repository at https://github.com/Gazler/cloneme.

[root@pipeline-cloud-test02 git_hug]# git clone https://github.com/Gazler/cloneme
Cloning into 'cloneme'...
remote: Counting objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (7/7), done.
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

回一下第1關(guān), 可以總結(jié)下創(chuàng)建git倉庫大致有兩種: git init初始化和git clone克隆遠程git項目

Level 6: clone to folder

Clone the repository at https://github.com/Gazler/cloneme to ‘my_cloned_repo’.

還是克隆遠程git項目, 只不過換個名字

[root@pipeline-cloud-test02 git_hug]# git clone  https://github.com/Gazler/cloneme my_cloned_repo
Cloning into 'my_cloned_repo'...
remote: Counting objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (7/7), done.
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 7: ignore

The text editor ‘vim’ creates files ending in ‘.swp’ (swap files) for all files that are currently open. We don’t want them creeping into the repository. Make this repository ignore ‘.swp’ files.

有些零食文件什么的不需要git來管理. 忽略他們

[root@pipeline-cloud-test02 git_hug]# ls -a
.  ..  .git  .gitignore  .profile.yml  README.swp
[root@pipeline-cloud-test02 git_hug]# vim .gitignore
[root@pipeline-cloud-test02 git_hug]# more .gitignore
.profile.yml
.gitignore
*.swp
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

.gitignore 在倉庫的根目錄下, 用于配置可忽略文件的規(guī)則

Level 8: include

Notice a few files with the ‘.a’ extension. We want git to ignore all but the ‘lib.a’ file.

在忽略的里面排除掉不忽略的

[root@pipeline-cloud-test02 git_hug]# ls -a
.  ..  first.a  .git  .gitignore  lib.a  .profile.yml  second.a
[root@pipeline-cloud-test02 git_hug]# vim .gitignore
[root@pipeline-cloud-test02 git_hug]# more .gitignore
.profile.yml
.gitignore
*.a
!lib.a  # 加"!"就是不忽略
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 9: status

There are some files in this repository, one of the files is untracked, which file is it?

也就是去找沒有登記的文件, Untrakced File

[root@pipeline-cloud-test02 git_hug]# git status -s
A  Guardfile
A  README
A  config.rb
A  deploy.rb
A  setup.rb
?? database.yml
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is the full file name of the untracked file? database.yml
Congratulations, you have solved the level!

通過git status查看到的文件有這三種狀態(tài):

  • untracked: 未被登記的
  • modified: 修改過的
  • staged: 暫存區(qū)的

Level 10: number_of_files_committed

There are some files in this repository, how many of the files will be committed?

找出有幾個要被提交的文件, 什么狀態(tài)的文件能被提交? 只有暫存區(qū)的了

[root@pipeline-cloud-test02 git_hug]# git status  -s
A  rubyfile1.rb
M  rubyfile4.rb
 M rubyfile5.rb
?? rubyfile6.rb
?? rubyfile7.rb
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
How many changes are going to be committed? 2
Congratulations, you have solved the level!

補充:

如果你用git status, 你會明顯看到有兩個文件處于Changes to be commited.

如果加-s查看簡要列表, 你需要明白前面的標(biāo)志位是什么意思.

首先第一個標(biāo)志位: 是Staging area中的變化, 第二個標(biāo)志位: 是Working directory中發(fā)生的更改.

標(biāo)志位上的字母A, M, D, ?就不用說了

Level 11: rm

A file has been removed from the working tree, however the file was not removed from the repository. Find out what this file was and remove it.

有個文件只在工作空間被刪了, 然而沒有從倉庫里刪掉. 找到它并刪掉rm_cached

[root@pipeline-cloud-test02 git_hug]# git status -s
 D deleteme.rb
[root@pipeline-cloud-test02 git_hug]# git rm deleteme.rb
rm 'deleteme.rb'
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

在刪除git文件時, 不要用rm的系統(tǒng)級別刪除, 這樣git會失去對這個文件的追蹤. 而應(yīng)該用git rm刪除.

Level 12: rm_cached

A file has accidentally been added to your staging area, find out which file and remove it from the staging area. NOTE Do not remove the file from the file system, only from git.

有個文件被錯誤的添加到暫存區(qū), 找到他, 并從暫存區(qū)刪除, 注意不是從文件系統(tǒng)刪除.

[root@pipeline-cloud-test02 git_hug]# git status -s
A  deleteme.rb
[root@pipeline-cloud-test02 git_hug]# git rm --cached deleteme.rb
rm 'deleteme.rb'
[root@pipeline-cloud-test02 git_hug]# git status -s
?? deleteme.rb
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 13: stash

You’ve made some changes and want to work on them later. You should save them, but don’t commit them.

你已經(jīng)在工作空間做了一些改動, 可能出現(xiàn)別的任務(wù)要你改動, 你需要保存之前的改動稍后處理, 但并不是提交.

[root@pipeline-cloud-test02 git_hug]# git status -s
 M lyrics.txt
[root@pipeline-cloud-test02 git_hug]# git stash
Saved working directory and index state WIP on master: 0206059 Add some lyrics
HEAD is now at 0206059 Add some lyrics
[root@pipeline-cloud-test02 git_hug]# git status -s
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

補充:

儲藏(Stashing): 經(jīng)常有這樣的事情發(fā)生,當(dāng)你正在進行項目中某一部分的工作,里面的東西處于一個比較雜亂的狀態(tài),而你想轉(zhuǎn)到其他分支上進行一些工作。問題是,你不想提交進行了一半的工作,否則以后你無法回到這個工作點。解決這個問題的辦法就是git stash命令。

“儲藏”可以獲取你工作目錄的中間狀態(tài)——也就是你修改過的被追蹤的文件和暫存的變更——并將它保存到一個未完結(jié)變更的堆棧中,隨時可以重新應(yīng)用。

使用儲藏命令后, 它會幫你把工作環(huán)境回到 最后一次提交的狀態(tài), 也就是一個完全干凈的工作環(huán)境.

git stash list # 查看暫存工作區(qū)的列表
git stash pop # 彈出這個現(xiàn)場

Level 14: rename

We have a file called ‘oldfile.txt’. We want to rename it to ‘newfile.txt’ and stage this change.

將這個文件改名, 并希望在暫存區(qū)也生效.

[root@pipeline-cloud-test02 git_hug]# ls
oldfile.txt
[root@pipeline-cloud-test02 git_hug]# git mv oldfile.txt newfile.txt
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

如同git rm一樣, git mv也會把改動的結(jié)果記錄到暫存區(qū).

Level 15: restructure

You added some files to your repository, but now realize that your project needs to be restructured. Make a new folder named ‘src’ and using Git move all of the .html files into this folder.

你需要重構(gòu)下倉庫里的文件了, 新建一個src目錄, 并把html文件放進去(用Git能夠追蹤的方式)

[root@pipeline-cloud-test02 git_hug]# mkdir src
[root@pipeline-cloud-test02 git_hug]# git mv *.html /s
sbin/ srv/  sys/
[root@pipeline-cloud-test02 git_hug]# git mv *.html ./src/
[root@pipeline-cloud-test02 git_hug]# ls src/
about.html  contact.html  index.html
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 16: log

You will be asked for the hash of most recent commit. You will need to investigate the logs of the repository for this.

查下最近一次commit的哈希值

[root@pipeline-cloud-test02 git_hug]# git log --oneline
7b8d2cd THIS IS THE COMMIT YOU ARE LOOKING FOR!
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is the hash of the most recent commit? 7b8d2cd
Congratulations, you have solved the level!

每個git commit都會留下一條日志.

Level 17: tag

We have a git repo and we want to tag the current commit with ‘new_tag’.

給當(dāng)前提交打一個新標(biāo)簽

[root@pipeline-cloud-test02 git_hug]# git tag new_tag
[root@pipeline-cloud-test02 git_hug]# git tag
new_tag
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!
git tag xxx # 給最近一次提交打標(biāo)簽
git tag xxx hashCodeXxx # 給某次提交打標(biāo)簽
git tag # 列出所有標(biāo)簽
git tag -d xxx # 刪除某個標(biāo)簽

Level 18: push tags

There are tags in the repository that aren’t pushed into remote repository. Push them now.

推送本地倉庫的標(biāo)簽到遠程倉庫

[root@pipeline-cloud-test02 git_hug]# git push --tags
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 19: commit_amend

The ‘README’ file has been committed, but it looks like the file ‘forgotten_file.rb’ was missing from the commit. Add the file and amend your previous commit to include it.

README文件已經(jīng)被提交, 但是另外一個文件忘記了, 添加這個文件到上一次提交中

[root@pipeline-cloud-test02 git_hug]# git status -s
?? forgotten_file.rb
[root@pipeline-cloud-test02 git_hug]# git add forgotten_file.rb
[root@pipeline-cloud-test02 git_hug]# git commit --amend -C HEAD
[master c90accd] Initial commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
 create mode 100644 forgotten_file.rb
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

一般是有文件漏提交了才會用--amend. 會把你漏的東西加到上一次提交里.

git commit --amend
git commit --amend -m "xxx"  # 并用新的注釋覆蓋上一次的
git commit --amend -C HEAD   # 還是用上一次的注釋

Level 20: commit_in_future

Commit your changes with the future date (e.g. tomorrow).

提交時候改變下日期

[root@pipeline-cloud-test02 git_hug]# date
Fri Dec 22 14:07:17 CST 2017
[root@pipeline-cloud-test02 git_hug]# git commit --date="Fri Dec 22 14:07:17 CST 2018" -m "midify commit date"
[master (root-commit) 83bca42] midify commit date
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

沒什么鳥用吧?

Level 21: reset

There are two files to be committed. The goal was to add each file as a separate commit, however both were added by accident. Unstage the file ‘to_commit_second.rb’ using the reset command (don’t commit anything).

有兩個文件要被提交, 但是你想分別提交, 把to_commit_second.rb從暫存區(qū)中拿出來

[root@pipeline-cloud-test02 git_hug]# git reset to_commit_second.rb
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

這一關(guān)是讓你從暫存區(qū)中取回文件到工作區(qū), 目的是不想讓這些文件被提交.

回憶第12關(guān), 也是從暫存區(qū)中取. 所以你用git rm --cached to_commit_second.rb也能完成該任務(wù).

區(qū)別在于, git rm適合于取回新增的文件, git reset適合取回修改的文件.

當(dāng)然git reset還有更強的恢復(fù)功能, 后面會遇到.

Level 22: reset_soft

You committed too soon. Now you want to undo the last commit, while keeping the index.

取消最后一次提交, 并保持暫存區(qū)不變

[root@pipeline-cloud-test02 git_hug]# git log --oneline
02dd7f7 Premature commit
40820f0 Initial commit
[root@pipeline-cloud-test02 git_hug]# git reset --soft HEAD^
[root@pipeline-cloud-test02 git_hug]# git log --oneline
40820f0 Initial commit
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

git reset命令中, --soft HEAD^就表示撤銷的事最近一次提交. 并且暫存區(qū)不受影響.

回憶下19關(guān), 是將某個遺漏的文件添加到上一次提交中去. 用的是git commit --amend -C HEAD

那它就等同于先執(zhí)行git reset --soft HEAD^再執(zhí)行git commit的合體.

Level 23: checkout_file

A file has been modified, but you don’t want to keep the modification. Checkout the ‘config.rb’ file from the last commit.

一個文件已經(jīng)被修改了, 但你并不想保留這份修改. 把文件撤銷到最后一次提交的狀態(tài).

[root@pipeline-cloud-test02 git_hug]# git status -s
 M config.rb
[root@pipeline-cloud-test02 git_hug]# git checkout config.rb
[root@pipeline-cloud-test02 git_hug]# git status -s
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 24: remote

This project has a remote repository. Identify it.

找到這個項目的遠程倉庫名

[root@pipeline-cloud-test02 git_hug]# git remote
my_remote_repo
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is the name of the remote repository? my_remote_repo
Congratulations, you have solved the level!

Level 25: remote_url

The remote repositories have a url associated to them. Please enter the url of remote_location.

找到這個項目的遠程倉庫的URL

[root@pipeline-cloud-test02 git_hug]# git remote -v
my_remote_repo  https://github.com/Gazler/githug (fetch)
my_remote_repo  https://github.com/Gazler/githug (push)
remote_location https://github.com/githug/not_a_repo (fetch)
remote_location https://github.com/githug/not_a_repo (push)
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is the url of the remote repository? remote_location https://github.com/githug/not_a_repo
Congratulations, you have solved the level!

Level 26: pull

You need to pull changes from your origin repository.

從遠程倉庫origin拉取更新

[root@pipeline-cloud-test02 git_hug]# git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.
From https://github.com/pull-this/thing-to-pull
 * branch            master     -> FETCH_HEAD
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

Level 27: remote_add

Add a remote repository called ‘origin’ with the url https://github.com/githug/githug

添加一個遠程倉庫, 倉庫名origin, 地址https://github.com/githug/githug

[root@pipeline-cloud-test02 git_hug]# git remote
[root@pipeline-cloud-test02 git_hug]# git remote -v
[root@pipeline-cloud-test02 git_hug]# git remote add origin https://github.com/githug/githug
[root@pipeline-cloud-test02 git_hug]# git remote -v
origin  https://github.com/githug/githug (fetch)
origin  https://github.com/githug/githug (push)
[root@pipeline-cloud-test02 git_hug]# git remote
origin
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
Congratulations, you have solved the level!

clone的項目git會自動保存地址, 以上這種方式適合于手工添加遠程倉庫

Level 28: push

Your local master branch has diverged from the remote origin/master branch. Rebase your commit onto origin/master and push it to remote.

你本地的master分支是從遠程倉庫(origin/master)上創(chuàng)建的, rebase你的提交到遠程倉庫(origin/master).

[root@pipeline-cloud-test02 git_hug]# git log --oneline
fd962e8 Third commit
98f106a Second commit
3186c7d First commit
[root@pipeline-cloud-test02 git_hug]# git log origin/master --oneline
6c41ca7 Fourth commit
[root@pipeline-cloud-test02 git_hug]# git rebase
First, rewinding head to replay your work on top of it...
Applying: First commit
Applying: Second commit
Applying: Third commit
[root@pipeline-cloud-test02 git_hug]# git push origin master
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 591 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To /tmp/d20171222-60085-1tu401p/.git
   6c41ca7..1154eda  master -> master
[root@pipeline-cloud-test02 git_hug]# git log origin/master --oneline
1154eda Third commit
f2580d0 Second commit
944547b First commit
6c41ca7 Fourth commit

補充:

關(guān)于推送到遠程:

# 推送到遠程
git push remote-name branch-name
git push -u remote-name branch-name # 推送的同時記住倉庫名和分支
git push # 記住后這樣推送

在多人協(xié)作的項目中, 大家都需要推送自己的更改到遠程倉庫.

但是推送也是有時間順序的. 如果在你推送之前有人已經(jīng)推送了, 那么你會收到"non-fast forward"的提示.

所以需要先獲取遠程的最新代碼到本地, 有如下兩種方式:

  1. git pull: 把遠程倉庫的最新代碼合并到本地,然后再提交。這時本地的提交和遠程的提交按時間順序混合排列。

  2. git rebase: 把本地倉庫的更新排到遠程倉庫更新之后,那這時候本地倉庫的所有提交都排在遠程倉庫的最后一次提交之后。rebase翻譯過來叫變基, 后面還會有關(guān)于它的應(yīng)用.

Level 29: diff

There have been modifications to the ‘a(chǎn)pp.rb’ file since your last commit. Find out whick line has changed.

最后一次提交之后你又修改了app.rb文件, 找到哪行被修改了

[root@pipeline-cloud-test02 git_hug]# git diff app.rb
diff --git a/app.rb b/app.rb
index 4f703ca..3bfa839 100644
--- a/app.rb
+++ b/app.rb
@@ -23,7 +23,7 @@ get '/yet_another' do
   erb :success
 end
 get '/another_page' do
-  @message = get_response('data.json')
+  @message = get_response('server.json')
   erb :another
 end

[root@pipeline-cloud-test02 git_hug]# vim app.rb
[root@pipeline-cloud-test02 git_hug]# githug
********************************************************************************
*                                    Githug                                    *
********************************************************************************
What is the number of the line which has changed? 26
Congratulations, you have solved the level!

補充:

git diff 是查看第二個flag(也就是Staging area和Working directory) 具體變化信息的命令

被修改后的文件是modified狀態(tài):

git diff –staged # 可以查看第一個flag(也就是Staging arae和History 之間的變化), 產(chǎn)生相同的效果
git diff HEAD # 可以看History 和 Working 之間的變化
git diff –stat # 后面加stat可以簡化變化信息

未完待續(xù): 通關(guān)Githug(二)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容