輸入 y ,創(chuàng)建 git_hug 目錄
No githug directory found, do you wish to create one? [yn] y
Welcome to Githug!
level 1
question
Name: init
Level: 1
Difficulty: *
A new directory, git_hug, has been created; initialize an empty repository in
it.
answer
$ cd git_hug
$ git init
相關(guān)
cd (Change Directory),跳轉(zhuǎn)目錄、切換路徑。
git init在當(dāng)前目錄新建一個Git代碼庫
level 2
question
Name: config
Level: 2
Difficulty: *
Set up your git name and email, this is important so that your commits can be id
entified.
answer
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
相關(guān)
git config git 的設(shè)置文件為.gitconfig,他可以在全局配置(加上--global),也可以項目配置。
另:
git config --list 顯示當(dāng)前的git配置
git config -e [--global] 編輯git的配置文件
level 3
question
Level: 3
Difficulty: *
There is a file in your folder called README, you should add it to your staging area
Note: You start each level with a new repo. Don't look for files from the previous one.
answer
$ git add .\README
相關(guān)
git add將所有修改過的工作文件提交暫存區(qū)
將所有文件添加到暫存區(qū)git add .或者git add -A
-
git add .會監(jiān)控工作區(qū)的狀態(tài)樹,使它會把工作時的所有變化提交到暫存區(qū),包括文件內(nèi)容修改(modified)以及新文件(new),但不包括被刪除的文件。 -
git add -u(git add --update的縮寫)僅監(jiān)控已經(jīng)被add的文件(即tracked file),他會將被修改的文件提交到暫存區(qū)。add -u不會提交新文件(untracked file)。 -
git add -A(git add --all的縮寫)是上面兩個功能的合集。即包括修改的文件、刪除的文件以及新文件。
level 4
question
Name: commit
Level: 4
Difficulty: *
The README file has been added to your staging area, now commit it.
answer
$ git commit -m 'message'
相關(guān)
git commit -m [message]提交暫存區(qū)到倉庫區(qū);還可以提交暫存區(qū)的指定文件到倉庫區(qū)git commit [file1] [file2] ... -m [message]。
同時,必須要寫 Commit message(提交說明),否則就不允許提交。
參考:Git 提交的正確姿勢:Commit message 編寫指南
另:
-
git commit -a提交工作區(qū)自上次commit之后的變化,直接到倉庫區(qū) -
git commit -v提交時顯示所有diff信息,-v參數(shù)表示可以看commit的差異 -
git commit --amend -m [message]使用一次新的commit,替代上一次提交。如果代碼沒有任何新變化,則用來改寫上一次commit的提交信息 -
git commit --amend [file1] [file2] ...重做上一次commit,并包括指定文件的新變化
level 5
question
Name: clone
Level: 5
Difficulty: *
Clone the repository at https://github.com/Gazler/cloneme.
answer
$ git clone https://github.com/Gazler/cloneme
相關(guān)
git clone支持多種協(xié)議,除了HTTP(s)以外,還支持SSH、Git、本地文件協(xié)議等,下面是一些例子。
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/
SSH協(xié)議的另一種寫法
$ git clone [user@]example.com:path/to/repo.git/
level 6
question
Name: clone_to_folder
Level: 6
Difficulty: *
Clone the repository at https://github.com/Gazler/cloneme to my_cloned_repo.
answer
$ git clone https://github.com/Gazler/cloneme my_cloned_repo
相關(guān)
git clone <版本庫的網(wǎng)址> <本地目錄名> 該命令會在本地主機(jī)生成一個目錄。如果不指定目錄名,則與遠(yuǎn)程主機(jī)的版本庫同名。
level 7
question
Name: ignore
Level: 7
Difficulty: **
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 those swap files which are ending in .swp.
answer
$ touch .gitignore
$ echo '*.swp'>>.gitignore
相關(guān)
- 忽略掉某個文件,需要修改
.gitignore文件的方法??梢栽谀愕挠脩裟夸浵聞?chuàng)建~/.gitignoreglobal文件中設(shè)置全局。
需要執(zhí)行git config --global core.excludesfile ~/.gitignoreglobal來使得它生效。-
*.a忽略所有 .a 結(jié)尾的文件 -
!lib.a但 lib.a 除外 -
/TODO僅僅忽略項目根目錄下的 TODO 文件,但不包括 subdir/TODO -
build/忽略 build/ 目錄下的所有文件 -
doo/*.txt會忽略 doc/notes.txt 但不包括 doc/server/arch.txt
-
-
.gitignore只能忽略那些原來沒有被track的文件,如果某些文件已經(jīng)被納入了版本管理中,則修改.gitignore是無效的。
正確的做法是在每個clone下來的倉庫中手動設(shè)置不要檢查特定文件的更改情況。
-
git update-index --assume-unchanged PATH在PATH處輸入要忽略的文件。 -
git update-index --no-assume-unchanged PATH還原。
- 另外 git 還提供了另一種
exclude的方式來做同樣的事情,不同的是.gitignore這個文件本身會提交到版本庫中去,用來保存的是公共的需要排除的文件。
而.git/info/exclude這里設(shè)置的則是你自己本地需要排除的文件。他不會影響到其他人。也不會提交到版本庫中去。
level 8
question
Name: include
Level: 8
Difficulty: **
Notice a few files with the '.a' extension. We want git to ignore all but the 'lib.a' file.
answer
$ echo '*.a'>>.gitignore
$ echo '!lib.a'>>.gitignore
相關(guān)
見上
level 9
question
Name: status
Level: 9
Difficulty: *
There are some files in this repository, one of the files is untracked, which file is it?
answer
$ git status
答案為:database.yml
相關(guān)
git status 顯示有變更的文件
level 10
question
Name: number_of_files_committed
Level: 10
Difficulty: *
There are some files in this repository, how many of the files will be committed?
answer
答案:2
相關(guān)
git status命令可以列出當(dāng)前目錄所有還沒有被git管理的文件和被git管理且被修改但還未提交(git commit)的文件。
- 命令中”Changes to be committed“中所列的內(nèi)容是在Index中的內(nèi)容,commit之后進(jìn)入Git Directory。
- 命令中“Changed but not updated”中所列的內(nèi)容是在Working Directory中的內(nèi)容,add之后將進(jìn)入Index。
- 命令中“Untracked files”中所列的內(nèi)容是尚未被Git跟蹤的內(nèi)容,add之后進(jìn)入Index
level 11
question
Name: rm
Level: 11
Difficulty: **
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.
answer
$ git status
$ git rm deleteme.rb
相關(guān)
git rm [file1] [file2] ...刪除工作區(qū)文件,并且將這次刪除放入暫存區(qū)。
git rm簡介
level 12
question
Name: rm_cached
Level: 12
Difficulty: **
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.
answer
$ git status
$ git rm --cached deleteme.rb
相關(guān)
git rm --cached [file] 停止追蹤指定文件,但該文件會保留在工作區(qū)。
level 13
question
Name: stash
Level: 13
Difficulty: **
You've made some changes and want to work on them later. You should save them, but don't commit them.
answer
$ git stash
相關(guān)
git stash 用于保存和恢復(fù)工作進(jìn)度。
-
git stash保存當(dāng)前的工作進(jìn)度。會分別對暫存區(qū)和工作區(qū)的狀態(tài)進(jìn)行保存。 -
git stash list顯示進(jìn)度列表。 -
git stash pop [--index] [<stash>]如果不使用任何參數(shù),會恢復(fù)最新保存的工作進(jìn)度,并將恢復(fù)的工作進(jìn)度從存儲的工作進(jìn)度列表中清除。
如果提供<stash>參數(shù)(來自git stash list顯示的列表),則從該<stash>中恢復(fù)?;謴?fù)完畢 也將從進(jìn)度列表中刪除<stash>。選項--index除了恢復(fù)工作區(qū)的文件外,還嘗試恢復(fù)暫存區(qū)。 -
git stash [save [--patch] [-k|--[no]keep-index] [-q|--quiet] [<message>]]這是第一條命令的完整版。- 使用參數(shù)
--patch會顯示工作區(qū)和HEAD的差異,通過對差異文件的編輯決定在進(jìn)度中最終要保存的工作區(qū)的內(nèi)容,通過編輯差異文件可以在進(jìn)度中排除無關(guān)內(nèi)容。 - 使用
-k或者--keep-index參數(shù),在保存進(jìn)度后不會將暫存區(qū)重置。默認(rèn)會將暫存區(qū)和工作區(qū)強(qiáng)制重置。
- 使用參數(shù)
-
git stash apply [--index] [<stash>]除了不刪除恢復(fù)的進(jìn)度之外,其余和git stash pop 命令一樣。 -
git stash drop [<stash>]刪除一個存儲的進(jìn)度。默認(rèn)刪除最新的進(jìn)度。 -
git stash clear刪除所有存儲的進(jìn)度。 -
git stash branch <branchname> <stash>基于進(jìn)度創(chuàng)建分支。
level 14
question
Name: rename
Level: 14
Difficulty: ***
We have a file called oldfile.txt. We want to rename it to newfile.txt and stage this change.
answer
$ git mv oldfile.txt newfile.txt
相關(guān)
git mv 重命名文件
level 15
question
Name: restructure
Level: 15
Difficulty: ***
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.
answer
$ mkdir src
$ git mv *.html src/
相關(guān)
mkdir(make directory) Mkdir 是一個用來在 Linux 系統(tǒng)下創(chuàng)建目錄的命令。此命令屬于內(nèi)建命令。
-
mkdir test1默認(rèn)情況下不帶任何參數(shù)運(yùn)行mkdir命令會在當(dāng)前目錄下創(chuàng)建目錄。 -
mkdir test2[ test22 test222]創(chuàng)建多個目錄。 -
mkdir -p test3/test33遞歸創(chuàng)建多個目錄。例如創(chuàng)建的目錄包含子目錄,如果找不到父目錄,那么這個參數(shù)會首先幫助創(chuàng)建父目錄。 -
mkdir -m=r test4使用-m參數(shù),給生成的新目錄設(shè)置權(quán)限。參考:工作中常用Linux命令:mkdir命令 -
mkdir -v test5創(chuàng)建目錄顯示信息。
level 16
question
Name: log
Level: 16
Difficulty: **
You will be asked for the hash of most recent commit. You will need to investigate the logs of the repository for this.
answer
$ git log
答案為:34b2fd7(commit后 前7位)
相關(guān)
git log 顯示當(dāng)前分支的版本歷史。
-
git log --stat顯示commit歷史,以及每次commit發(fā)生變更的文件 -
git log -s [keyword]搜索提交歷史,根據(jù)關(guān)鍵詞 -
git log -p [file]顯示指定文件相關(guān)的每一次diff -
git log -5 --pretty --oneline顯示過去5次提交 -
git log --follow [file]
git whatchanged [file]顯示某個文件的版本歷史,包括文件改名 -
git log [tag] HEAD --pretty-format:%s顯示某個commit之后的所有變動,每個commit占據(jù)一行 -
git log [tag] HEAD --grep feature顯示某個commit之后的所有變動,其“提交說明”必須符合搜索條件
level 17
question
Name: tag
Level: 17
Difficulty: **
We have a git repo and we want to tag the current commit with new_tag.
answer
$ git tag new_tag
相關(guān)
-
git tag列出所有的tag,在控制臺打印出當(dāng)前倉庫的所有標(biāo)簽 -
git tag [tag]新建一個tag在當(dāng)前commit -
git tag [tag] [commit]新建一個tag在指定commit -
git tag d [tag]刪除本地tag
level 18
question
Name: push_tags
Level: 18
Difficulty: **
There are tags in the repository that aren't pushed into remote repository. Push them now.
answer
$ git push --tags
相關(guān)
默認(rèn) git push 不會罷 tag 標(biāo)簽傳送到遠(yuǎn)端服務(wù)器上,只有通過顯示命令才能分享標(biāo)簽到遠(yuǎn)端倉庫。
-
git push origin [tagname]push 單個 tag -
git push [origin] --tagspush 所有 tag
level 19
question
Name: commit_amend
Level: 19
Difficulty: **
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.
answer
$ git add forgotten_file.rb
$ git commit --amend -m 'message'
相關(guān)
git commit --amend 合并緩存區(qū)的修改和最近的一次commit, 然后用生成的新的commit替換掉老的. 如果緩存區(qū)沒有內(nèi)容, 那么利用amend可以修改上一次commit的描述.
[譯]git commit --amend
level 20
question
Name: commit_in_future
Level: 20
Difficulty: **
Commit your changes with the future date (e.g. tomorrow).
answer
$ git commit --date 2016.10.08 -m 'message'
相關(guān)
git commit --date <date> 修改提交時間
level 21
question
Name: reset
Level: 21
Difficulty: **
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).
answer
$ git status
$ git reset HEAD to_commit_second.rb
相關(guān)
reset 命令移動 HEAD 到當(dāng)前分支的一個 commit, 這可以用來撤銷當(dāng)前分支的一些 commit
-
git reset [-q] [commit] [--] <paths>第一種用法是不會重置引用的,即不會修改master文件。 -
git reset [--soft | --mixed | --hard | --merge | --keep] [-q] [<commit>]
第二種用法不使用< paths > 則會重置引用,并且參數(shù)不同決定是否覆蓋暫存區(qū)和工作區(qū):
-
git reset -mixed此為默認(rèn)方式,不帶任何參數(shù)的git reset,會退到某個版本只保留源碼,回退commit和index信息,staged snapshot 被更新, working directory 未被更改。 -
git reset -soft回退到某個版本,只回退了commit信息,staged snapshot 和 working directory 都未被改變 (建議在命令行執(zhí)行后,再輸入 git status 查看狀態(tài)) -
git reset -hard徹底回退到某個版本,本地的源碼也會變?yōu)樯弦粋€版本的內(nèi)容,即staged snapshot 和 working directory 都將回退
例子:
#回退所有內(nèi)容到上一版本 HEAD^的意思是最近一次的提交
git reset HEAD^
#回退a.py這個文件的版本到上一個版本
git reset HEAD^ a.py
#向前回退到第3個版本
git reset –soft HEAD~3
#將本地的狀態(tài)回退到和遠(yuǎn)程的一樣
git reset –hard origin/master
#回退到某個版本
git reset 38679ed
#回退到上一次提交的狀態(tài),按照某一次的commit完全反向的進(jìn)行一次commit
git revert HEAD
level 22
question
Name: reset_soft
Level: 22
Difficulty: **
You committed too soon. Now you want to undo the last commit, while keeping the index.
answer
$ git reset --soft HEAD^
相關(guān)
見上
level 23
question
Name: checkout_file
Level: 23
Difficulty: ***
A file has been modified, but you don't want to keep the modification. Checkout the config.rb file from the last commit.
answer
$ git checkout config.rb
相關(guān)
git checkout 檢出。
- 創(chuàng)建分支
git branch branchName - 切換分支
git checkout branchName - 上面兩個命令可以合成一個命令
git checkout -b branchName
參考:
git checkout 命令詳解
Git詳解之三 Git分支
level 24
question
Name: remote
Level: 24
Difficulty: **
This project has a remote repository. Identify it.
answer
$ git remote -v
答案:my_remote_repo
相關(guān)
-
git remote不帶參數(shù),列出已經(jīng)存在的遠(yuǎn)程分支 -
git remote -v--verbose 列出詳細(xì)信息,在每個名字后面 -
git clone -o jQuery https://github.com/jquery/jquery.git
git remote想用其他的主機(jī)名 需要用 git clone命令的 -o 選項指定 -
git remote show <主機(jī)名>可以查看主機(jī)的詳細(xì)信息 -
git remote add <主機(jī)名> <網(wǎng)址>添加遠(yuǎn)程主機(jī) -
git remote rm <主機(jī)名>刪除遠(yuǎn)程主機(jī) -
git remore rename <原主機(jī)名> <新主機(jī)名>遠(yuǎn)程主機(jī)的改名
$ tail .git/config 查看remote信息。
level 25
question
Name: remote_url
Level: 25
Difficulty: **
The remote repositories have a url associated to them. Please enter the url of remote_location.
answer
$ git remote -v
答案:https://github.com/githug/not_a_repo
相關(guān)
見上
level 26
question
Name: pull
Level: 26
Difficulty: **
You need to pull changes from your origin repository.
answer
$ git pull origin master
相關(guān)
git push origin master 的意思就是上傳本地當(dāng)前分支代碼到master分支。
參考:
git pull
level 27
question
Name: remote_add
Level: 27
Difficulty: **
Add a remote repository called origin with the url https://github.com/githug/githug
answer
$ git remote add origin https://github.com/githug/githug
相關(guān)
見 level 24
level 28
question
Name: push
Level: 28
Difficulty: ***
Your local master branch has diverged from the remote origin/master branch. Rebase your commit onto origin/master and push it to remote.
answer
$ git rebase origin/master
$ git push origin master
相關(guān)
git rebase用于把一個分支的修改合并到當(dāng)前分支。
level 29
question
Name: diff
Level: 29
Difficulty: **
There have been modifications to the app.rb file since your last commit. Find out which line has changed.
answer
$ git diff app.rb
答案:26
相關(guān)
git diff app.rb查看文件改動
level 30
question
Name: blame
Level: 30
Difficulty: **
Someone has put a password inside the file config.rb find out who it was.
answer
$ git blame config.rb
答案:Spider Man
相關(guān)
git blame得到整個文件的每一行的詳細(xì)修改信息:包括SHA串,日期和作者。
level 31
question
Name: branch
Level: 31
Difficulty: *
You want to work on a piece of code that has the potential to break things, create the branch test_code.
answer
$ git branch test_code
相關(guān)
見上
level 32
question
Name: checkout
Level: 32
Difficulty: **
Create and switch to a new branch called my_branch. You will need to create a branch like you did in the previous level.
answer
$ git checkout -b my_branch
相關(guān)
見上
level 33
question
Name: checkout_tag
Level: 33
Difficulty: **
You need to fix a bug in the version 1.2 of your app. Checkout the tag v1.2.
answer
$ git checkout v1.2
相關(guān)
標(biāo)簽可以針對某一時間點(diǎn)的版本做標(biāo)記,常用于版本發(fā)布。
- 列出標(biāo)簽
-
Git tag在控制臺打印出當(dāng)前倉庫的所有標(biāo)簽 -
git tag -l 'v0.1.*'搜索符合模式的標(biāo)簽
-
- 打標(biāo)簽 git標(biāo)簽分為兩種類型:輕量標(biāo)簽和附注標(biāo)簽。輕量標(biāo)簽是指向提交對象的引用,標(biāo)注標(biāo)簽則是倉庫中的一個獨(dú)立對象。建議使用附注標(biāo)簽。
-
git tag v0.1.2-light創(chuàng)建輕量標(biāo)簽 -
git tag -a v0.1.2 -m "0.1.2版本"創(chuàng)建附注標(biāo)簽 -
git tag -a v0.1.1 9fbc3d0給指定的commit打標(biāo)簽
-
- 切換到標(biāo)簽
-
git checkout [tagname]查看標(biāo)簽信息 -
git show v0.1.2查看標(biāo)簽的版本信息
-
- 刪除標(biāo)簽
-
git tag -d v0.1.2刪除標(biāo)簽
-
- 標(biāo)簽發(fā)布 git push 不會將標(biāo)簽對象提交到git服務(wù)器,我們需要進(jìn)行顯示的操作:
-
git push origin v0.1.2將標(biāo)簽提交到git服務(wù)器 -
git push origin -tags將本地所有標(biāo)簽一次性提交到git服務(wù)器
-
參考:
git命令之git tag 給當(dāng)前分支打標(biāo)簽
Git 基礎(chǔ) - 打標(biāo)簽
Git查看、刪除、重命名遠(yuǎn)程分支和tag
level 34
question
Name: checkout_tag_over_branch
Level: 34
Difficulty: **
You need to fix a bug in the version 1.2 of your app. Checkout the tag v1.2 (Note: There is also a branch named v1.2).
answer
$ git checkout tags/v1.2
相關(guān)
git checkout tags/v1.2 當(dāng)標(biāo)簽和分支名相同時,需要指定標(biāo)簽檢出
level 35
question
Name: branch_at
Level: 35
Difficulty: ***
You forgot to branch at the previous commit and made a commit on top of it. Create branch test_branch at the commit before the last.
answer
一種方法:
$ git branch test_branch HEAD^1
第二種方法:
$ git log
$ git branch test_branch -v 00740b4
找到第二條的id,輸入前7位
相關(guān)
見上
level 36
question
Name: delete_branch
Level: 36
Difficulty: **
You have created too many branches for your project. There is an old branch in your repo called 'delete_me', you should delete it.
answer
$ git branch -d delete_me
相關(guān)
見上
level 37
question
Name: push_branch
Level: 37
Difficulty: **
You've made some changes to a local branch and want to share it, but aren't yet ready to merge it with the 'master' branch. Push only 'test_branch' to the remote repository
answer
$ git push origin test_branch
相關(guān)
-
git push origin master上面命令表示,將本地的master分支推送到origin主機(jī)的master分支。如果后者不存在,則會被新建。 -
git push origin :master省略本地的分支名,則表示刪除指定的遠(yuǎn)程分支,因為這等同與推送一個空的本地分支到遠(yuǎn)程分支。等同于:git push origin --delete master -
git push origin如果當(dāng)前分支與遠(yuǎn)程分支直接存在追蹤關(guān)系,則本地分支和遠(yuǎn)程分支都可以省略 -
git push如果當(dāng)前分支只有一個追蹤分支,那么主機(jī)名都可以省略 -
git push -u origin master如果當(dāng)前分支和多個主機(jī)存在追蹤關(guān)系,則可以使用 -u 選項指定一個默認(rèn)主機(jī),這樣后面就可以不加任何參數(shù)使用 git push -
git push --all origin不管是否存在對應(yīng)的遠(yuǎn)程分支,將本地的所有分支都推送到遠(yuǎn)程主機(jī)。 -
git push origin --tagsgit push 不會推送標(biāo)簽(tag),除非使用 -tags 選項
level 38
question
Name: merge
Level: 38
Difficulty: **
We have a file in the branch 'feature'; Let's merge it to the master branch.
answer
$ git merge feature
相關(guān)
git merge合并分支
level 39
question
Name: fetch
Level: 39
Difficulty: **
Looks like a new branch was pushed into our remote repository. Get the changes without merging them with the local repository
answer
$ git fetch origin
相關(guān)
git fetch origin master 取回origin主機(jī)的master分支
level 40
question
Name: rebase
Level: 40
Difficulty: **
We are using a git rebase workflow and the feature branch is ready to go into master. Let's rebase the feature branch onto our master branch.
answer
$ git checkout feature
$ git rebase master
相關(guān)
git rebase 命令主要用在從上游分支獲取commit信息,并有機(jī)的將當(dāng)前分支和上游分支進(jìn)行合并
git rebase [-i | --interactive] [options] [--onto ] []
git rebase [-i | --interactive] [options] –onto –root []
git rebase –continue | –skip | –abort
level 41
question
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.
answer
$ git rebase --onto master wrong_branch
相關(guān)
git rebase --onto A B C A 代表的是你實際想要將切片放到哪個分支,B代表切片開始分支(一定要注意的問題是 B 的開閉問題,這里 rebase --onto 的機(jī)制是左開右閉)。
git rebase --onto A B~1 temp 如果想要保留A和C的歷史,就需要先在切片的末尾建立一個分支temp。這就代表把B到c之間的歷史移到了A上,并且當(dāng)前temp分支的歷史狀態(tài)就是窩想要的。
參考:妙用git rebase --onto指令 - Ricky.K
level 42
question
Name: repack
Level: 42
Difficulty: **
Optimise how your repository is packaged ensuring that redundant packs are removed.
answer
$ git repack -d
相關(guān)
git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [--window=<n>] [--depth=<n>]
git repack -d 包裝后,如果新創(chuàng)建的包使一些現(xiàn)有的包冗余,刪除多余的包。同時運(yùn)行 git prune-packed 去除多余的松散對象文件。
level 43
question
Name: cherry-pick
Level: 43
Difficulty: ***
Your new feature isn't worth the time and you're going to delete it. But it has one commit that fills in README file, and you want this commit to be on the master as well.
answer
$ git branch
$ git log new-feature
$ git cherry-pick ca32a6da
先查看分支列表,得到分支名,查看分支提交記錄,最后合并。
相關(guān)
Git cherry-pick 可以選擇某一個分支中的一個或幾個commit來進(jìn)行操作
level 44
question
Name: grep
Level: 44
Difficulty: **
Your project's deadline approaches, you should evaluate how many TODOs are left in your code
answer
$ git grep TODO
可以看到有四條
相關(guān)
git grep 查找git庫里面的某段文字
-
git grep xmmap查看git里面這個倉庫里每個使用 ‘xmmap’ 函數(shù)的地方。 -
git grep -n xmmap顯示行號。 -
git grep --name-only xmmap只顯示文件名。 -
git grep -c xmmap查看每個文件里有多少行匹配內(nèi)容(line matches)。 -
git grep xmmap v1.5.0查找git倉庫里某個特定版本里的內(nèi)容,在命令行末尾加上表簽名(tag reference)。 -
git grep -e '#define' --and -e SORT_DIRENT組合搜索條件:查找在倉庫的哪些地方定義了‘SORT_DIRENT’。 -
git grep --all-match -e '#define' -e SORT_DIRENT進(jìn)行或條件搜索。
level 45
question
Name: rename_commit
Level: 45
Difficulty: ***
Correct the typo in the message of your first (non-root) commit.
answer
$ git log master
$ git rebase -i HEAD~2
查看歷史提交記錄,看到需要修改的為倒數(shù)第二個,進(jìn)入編輯頁面,將需要改動的行的pick改為reword。保存退出后在彈出的第二個窗口里修改拼寫錯誤 commmit 改為 commit
相關(guān)
見上
level 46
question
Name: squash
Level: 46
Difficulty: ****
You have committed several times but would like all those changes to be one commit.
answer
$ git rebase -i HEAD~4
將后三個改為s
相關(guān)
見上
level 47
question
Name: merge_squash
Level: 47
Difficulty: ***
Merge all commits from the long-feature-branch as a single commit.
answer
$ git merge --squash long-feature-branch
$ git commit -m 'message'
相關(guān)
--squash 選項的含義是:本地文件內(nèi)容與不使用該選項的合并結(jié)果相同,但是不提交、不移動 HEAD ,因此需要一條額外的commit命令。其效果相當(dāng)于將 another 分支上的多個 commit 合并成一個,放在當(dāng)前分支上,原來的 commit 歷史則沒有拿過來。
level 48
question
Name: reorder
Level: 48
Difficulty: ****
You have committed several times but in the wrong order. Please reorder your commits.
answer
$ git log
$ git rebase -i HEAD~2
查看歷史記錄發(fā)現(xiàn)提交順序錯誤,將前兩行順序調(diào)換。
相關(guān)
見上
level 49
question
Name: bisect
Level: 49
Difficulty: ***
A bug was introduced somewhere along the way. You know that running ruby prog.rb 5 should output 15. You can also run make test. What are the first 7 chars of the hash of the commit that introduced the bug.
answer
$ git log --reverse -p prog.rb
$ git bisect start master f6088248
$ git bisect run make test
查看最初一次為正確提交,得到版本號。執(zhí)行完畢后日志里找到 “is the first bad commit” ,得到 18ed2ac。
(標(biāo)記):出錯了 得到的結(jié)果不為這個。。。
相關(guān)
git bisect startgit bisect good fb088248git bisect bad mastergit bisect run make test-
git bisect reset回到之前執(zhí)行 git bisect start 的狀態(tài)。
level 50
question
Name: stage_lines
Level: 50
Difficulty: ****
You've made changes within a single file that belong to two different features, but neither of the changes are yet staged. Stage only the changes belonging to the first feature.
answer
$ git add -p feature.rb
輸入e編輯提交內(nèi)容,刪除第二個。
相關(guān)
見上
level 51
question
Name: find_old_branch
Level: 51
Difficulty: ****
You have been working on a branch but got distracted by a major issue and forgot the name of it. Switch back to that branch.
answer
$ git reflog
$ git checkout solve_world_hunger
查找到另一個分支,切換到另一個分支。
相關(guān)
reflog 是 git 用來記錄引用變化的一種機(jī)制。
git reflog 命令可以對 git 誤操作進(jìn)行數(shù)據(jù)恢復(fù)。
level 52
question
Name: revert
Level: 52
Difficulty: ****
You have committed several times but want to undo the middle commit.
All commits have been pushed, so you can't change existing history.
answer
$ git log
$ git revert HEAD^1
相關(guān)
修復(fù)提交文件中的錯誤:
-
git reset --hard HEAD把工作目錄中所有未提交的內(nèi)容清空。 -
git checkout恢復(fù)一個文件 -
git revert HEAD撤銷一次提交
level 53
question
Name: restore
Level: 53
Difficulty: ****
You decided to delete your latest commit by running git reset --hard HEAD^. (Not a smart thing to do.) You then change your mind, and want that commit back. Restore the deleted commit.
answer
$ git reflog
$ git checkout bdbe33d
查看日志,回覆至對應(yīng)的版本
相關(guān)
恢復(fù)已修改的文件:
level 54
question
Name: conflict
Level: 54
Difficulty: ****
You need to merge mybranch into the current branch (master). But there may be some incorrect changes in mybranch which may cause conflicts. Solve any merge-conflicts you come across and finish the merge.
answer
$ git merge mybranch
$ vim poem.txt
$ git add poem.txt
$ git commit -m 'message'
相關(guān)
處理沖突
level 55
question
Name: submodule
Level: 55
Difficulty: **
You want to include the files from the following repo: https://github.com/jackmaney/githug-include-me into a the folder ./githug-include-me. Do this without manually cloning the repo or copying the files from the repo into this repo.
answer
$ git submodule add http://github.com/jackmaney/githug-include-me
相關(guān)
git submodule add 子模塊倉庫地址 路徑
Git Submodule 使用簡介
level 56

最后
好多還是半懂不懂的狀態(tài)%>_<%... 嘛 有錯請指正:-D