輸入 y ,創建 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
相關
cd
(Change Directory),跳轉目錄、切換路徑。
git init
在當前目錄新建一個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]"
相關
git config
git 的設置文件為.gitconfig
,他可以在全局配置(加上--global),也可以項目配置。
另:
git config --list
顯示當前的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
相關
git add
將所有修改過的工作文件提交暫存區
將所有文件添加到暫存區git add .
或者git add -A
-
git add .
會監控工作區的狀態樹,使它會把工作時的所有變化提交到暫存區,包括文件內容修改(modified)以及新文件(new),但不包括被刪除的文件。 -
git add -u
(git add --update
的縮寫)僅監控已經被add的文件(即tracked file),他會將被修改的文件提交到暫存區。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'
相關
git commit -m [message]
提交暫存區到倉庫區;還可以提交暫存區的指定文件到倉庫區git commit [file1] [file2] ... -m [message]
。
同時,必須要寫 Commit message(提交說明),否則就不允許提交。
參考:Git 提交的正確姿勢:Commit message 編寫指南
另:
-
git commit -a
提交工作區自上次commit之后的變化,直接到倉庫區 -
git commit -v
提交時顯示所有diff信息,-v參數表示可以看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
相關
git clone
支持多種協議,除了HTTP(s)以外,還支持SSH、Git、本地文件協議等,下面是一些例子。
$ 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協議的另一種寫法
$ 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
相關
git clone <版本庫的網址> <本地目錄名>
該命令會在本地主機生成一個目錄。如果不指定目錄名,則與遠程主機的版本庫同名。
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
相關
- 忽略掉某個文件,需要修改
.gitignore
文件的方法。可以在你的用戶目錄下創建~/.gitignoreglobal
文件中設置全局。
需要執行git config --global core.excludesfile ~/.gitignoreglobal
來使得它生效。-
*.a
忽略所有 .a 結尾的文件 -
!lib.a
但 lib.a 除外 -
/TODO
僅僅忽略項目根目錄下的 TODO 文件,但不包括 subdir/TODO -
build/
忽略 build/ 目錄下的所有文件 -
doo/*.txt
會忽略 doc/notes.txt 但不包括 doc/server/arch.txt
-
-
.gitignore
只能忽略那些原來沒有被track的文件,如果某些文件已經被納入了版本管理中,則修改.gitignore
是無效的。
正確的做法是在每個clone下來的倉庫中手動設置不要檢查特定文件的更改情況。
-
git update-index --assume-unchanged PATH
在PATH處輸入要忽略的文件。 -
git update-index --no-assume-unchanged PATH
還原。
- 另外 git 還提供了另一種
exclude
的方式來做同樣的事情,不同的是.gitignore
這個文件本身會提交到版本庫中去,用來保存的是公共的需要排除的文件。
而.git/info/exclude
這里設置的則是你自己本地需要排除的文件。他不會影響到其他人。也不會提交到版本庫中去。
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
相關
見上
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
相關
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
相關
git status
命令可以列出當前目錄所有還沒有被git管理的文件和被git管理且被修改但還未提交(git commit)的文件。
- 命令中”Changes to be committed“中所列的內容是在Index中的內容,commit之后進入Git Directory。
- 命令中“Changed but not updated”中所列的內容是在Working Directory中的內容,add之后將進入Index。
- 命令中“Untracked files”中所列的內容是尚未被Git跟蹤的內容,add之后進入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
相關
git rm [file1] [file2] ...
刪除工作區文件,并且將這次刪除放入暫存區。
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
相關
git rm --cached [file]
停止追蹤指定文件,但該文件會保留在工作區。
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
相關
git stash
用于保存和恢復工作進度。
-
git stash
保存當前的工作進度。會分別對暫存區和工作區的狀態進行保存。 -
git stash list
顯示進度列表。 -
git stash pop [--index] [<stash>]
如果不使用任何參數,會恢復最新保存的工作進度,并將恢復的工作進度從存儲的工作進度列表中清除。
如果提供<stash>參數(來自git stash list顯示的列表),則從該<stash>中恢復。恢復完畢 也將從進度列表中刪除<stash>。選項--index除了恢復工作區的文件外,還嘗試恢復暫存區。 -
git stash [save [--patch] [-k|--[no]keep-index] [-q|--quiet] [<message>]]
這是第一條命令的完整版。- 使用參數
--patch
會顯示工作區和HEAD的差異,通過對差異文件的編輯決定在進度中最終要保存的工作區的內容,通過編輯差異文件可以在進度中排除無關內容。 - 使用
-k
或者--keep-index
參數,在保存進度后不會將暫存區重置。默認會將暫存區和工作區強制重置。
- 使用參數
-
git stash apply [--index] [<stash>]
除了不刪除恢復的進度之外,其余和git stash pop 命令一樣。 -
git stash drop [<stash>]
刪除一個存儲的進度。默認刪除最新的進度。 -
git stash clear
刪除所有存儲的進度。 -
git stash branch <branchname> <stash>
基于進度創建分支。
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
相關
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/
相關
mkdir
(make directory) Mkdir 是一個用來在 Linux 系統下創建目錄的命令。此命令屬于內建命令。
-
mkdir test1
默認情況下不帶任何參數運行mkdir命令會在當前目錄下創建目錄。 -
mkdir test2[ test22 test222]
創建多個目錄。 -
mkdir -p test3/test33
遞歸創建多個目錄。例如創建的目錄包含子目錄,如果找不到父目錄,那么這個參數會首先幫助創建父目錄。 -
mkdir -m=r test4
使用-m參數,給生成的新目錄設置權限。參考:工作中常用Linux命令:mkdir命令 -
mkdir -v test5
創建目錄顯示信息。
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位)
相關
git log
顯示當前分支的版本歷史。
-
git log --stat
顯示commit歷史,以及每次commit發生變更的文件 -
git log -s [keyword]
搜索提交歷史,根據關鍵詞 -
git log -p [file]
顯示指定文件相關的每一次diff -
git log -5 --pretty --oneline
顯示過去5次提交 -
git log --follow [file]
git whatchanged [file]
顯示某個文件的版本歷史,包括文件改名 -
git log [tag] HEAD --pretty-format:%s
顯示某個commit之后的所有變動,每個commit占據一行 -
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
相關
-
git tag
列出所有的tag,在控制臺打印出當前倉庫的所有標簽 -
git tag [tag]
新建一個tag在當前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
相關
默認 git push 不會罷 tag 標簽傳送到遠端服務器上,只有通過顯示命令才能分享標簽到遠端倉庫。
-
git push origin [tagname]
push 單個 tag -
git push [origin] --tags
push 所有 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'
相關
git commit --amend
合并緩存區的修改和最近的一次commit, 然后用生成的新的commit替換掉老的. 如果緩存區沒有內容, 那么利用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'
相關
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
相關
reset 命令移動 HEAD 到當前分支的一個 commit, 這可以用來撤銷當前分支的一些 commit
-
git reset [-q] [commit] [--] <paths>
第一種用法是不會重置引用的,即不會修改master文件。 -
git reset [--soft | --mixed | --hard | --merge | --keep] [-q] [<commit>]
第二種用法不使用< paths > 則會重置引用,并且參數不同決定是否覆蓋暫存區和工作區:
-
git reset -mixed
此為默認方式,不帶任何參數的git reset,會退到某個版本只保留源碼,回退commit和index信息,staged snapshot 被更新, working directory 未被更改。 -
git reset -soft
回退到某個版本,只回退了commit信息,staged snapshot 和 working directory 都未被改變 (建議在命令行執行后,再輸入 git status 查看狀態) -
git reset -hard
徹底回退到某個版本,本地的源碼也會變為上一個版本的內容,即staged snapshot 和 working directory 都將回退
例子:
#回退所有內容到上一版本 HEAD^的意思是最近一次的提交
git reset HEAD^
#回退a.py這個文件的版本到上一個版本
git reset HEAD^ a.py
#向前回退到第3個版本
git reset –soft HEAD~3
#將本地的狀態回退到和遠程的一樣
git reset –hard origin/master
#回退到某個版本
git reset 38679ed
#回退到上一次提交的狀態,按照某一次的commit完全反向的進行一次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^
相關
見上
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
相關
git checkout
檢出。
- 創建分支
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
相關
-
git remote
不帶參數,列出已經存在的遠程分支 -
git remote -v
--verbose 列出詳細信息,在每個名字后面 -
git clone -o jQuery https://github.com/jquery/jquery.git
git remote
想用其他的主機名 需要用 git clone命令的 -o 選項指定 -
git remote show <主機名>
可以查看主機的詳細信息 -
git remote add <主機名> <網址>
添加遠程主機 -
git remote rm <主機名>
刪除遠程主機 -
git remore rename <原主機名> <新主機名>
遠程主機的改名
$ 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
相關
見上
level 26
question
Name: pull
Level: 26
Difficulty: **
You need to pull changes from your origin repository.
answer
$ git pull origin master
相關
git push origin master
的意思就是上傳本地當前分支代碼到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
相關
見 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
相關
git rebase用于把一個分支的修改合并到當前分支。
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
相關
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
相關
git blame
得到整個文件的每一行的詳細修改信息:包括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
相關
見上
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
相關
見上
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
相關
標簽可以針對某一時間點的版本做標記,常用于版本發布。
- 列出標簽
-
Git tag
在控制臺打印出當前倉庫的所有標簽 -
git tag -l 'v0.1.*'
搜索符合模式的標簽
-
- 打標簽 git標簽分為兩種類型:輕量標簽和附注標簽。輕量標簽是指向提交對象的引用,標注標簽則是倉庫中的一個獨立對象。建議使用附注標簽。
-
git tag v0.1.2-light
創建輕量標簽 -
git tag -a v0.1.2 -m "0.1.2版本"
創建附注標簽 -
git tag -a v0.1.1 9fbc3d0
給指定的commit打標簽
-
- 切換到標簽
-
git checkout [tagname]
查看標簽信息 -
git show v0.1.2
查看標簽的版本信息
-
- 刪除標簽
-
git tag -d v0.1.2
刪除標簽
-
- 標簽發布 git push 不會將標簽對象提交到git服務器,我們需要進行顯示的操作:
-
git push origin v0.1.2
將標簽提交到git服務器 -
git push origin -tags
將本地所有標簽一次性提交到git服務器
-
參考:
git命令之git tag 給當前分支打標簽
Git 基礎 - 打標簽
Git查看、刪除、重命名遠程分支和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
相關
git checkout tags/v1.2
當標簽和分支名相同時,需要指定標簽檢出
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位
相關
見上
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
相關
見上
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
相關
-
git push origin master
上面命令表示,將本地的master分支推送到origin主機的master分支。如果后者不存在,則會被新建。 -
git push origin :master
省略本地的分支名,則表示刪除指定的遠程分支,因為這等同與推送一個空的本地分支到遠程分支。等同于:git push origin --delete master -
git push origin
如果當前分支與遠程分支直接存在追蹤關系,則本地分支和遠程分支都可以省略 -
git push
如果當前分支只有一個追蹤分支,那么主機名都可以省略 -
git push -u origin master
如果當前分支和多個主機存在追蹤關系,則可以使用 -u 選項指定一個默認主機,這樣后面就可以不加任何參數使用 git push -
git push --all origin
不管是否存在對應的遠程分支,將本地的所有分支都推送到遠程主機。 -
git push origin --tags
git push 不會推送標簽(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
相關
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
相關
git fetch origin master
取回origin主機的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
相關
git rebase
命令主要用在從上游分支獲取commit信息,并有機的將當前分支和上游分支進行合并
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
相關
git rebase --onto A B C
A 代表的是你實際想要將切片放到哪個分支,B代表切片開始分支(一定要注意的問題是 B 的開閉問題,這里 rebase --onto 的機制是左開右閉)。
git rebase --onto A B~1 temp
如果想要保留A和C的歷史,就需要先在切片的末尾建立一個分支temp。這就代表把B到c之間的歷史移到了A上,并且當前temp分支的歷史狀態就是窩想要的。
參考:妙用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
相關
git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [--window=<n>] [--depth=<n>]
git repack -d
包裝后,如果新創建的包使一些現有的包冗余,刪除多余的包。同時運行 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
先查看分支列表,得到分支名,查看分支提交記錄,最后合并。
相關
Git cherry-pick
可以選擇某一個分支中的一個或幾個commit來進行操作
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
可以看到有四條
相關
git grep
查找git庫里面的某段文字
-
git grep xmmap
查看git里面這個倉庫里每個使用 ‘xmmap’ 函數的地方。 -
git grep -n xmmap
顯示行號。 -
git grep --name-only xmmap
只顯示文件名。 -
git grep -c xmmap
查看每個文件里有多少行匹配內容(line matches)。 -
git grep xmmap v1.5.0
查找git倉庫里某個特定版本里的內容,在命令行末尾加上表簽名(tag reference)。 -
git grep -e '#define' --and -e SORT_DIRENT
組合搜索條件:查找在倉庫的哪些地方定義了‘SORT_DIRENT’。 -
git grep --all-match -e '#define' -e SORT_DIRENT
進行或條件搜索。
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
查看歷史提交記錄,看到需要修改的為倒數第二個,進入編輯頁面,將需要改動的行的pick改為reword。保存退出后在彈出的第二個窗口里修改拼寫錯誤 commmit 改為 commit
相關
見上
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
相關
見上
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'
相關
--squash
選項的含義是:本地文件內容與不使用該選項的合并結果相同,但是不提交、不移動 HEAD ,因此需要一條額外的commit命令。其效果相當于將 another 分支上的多個 commit 合并成一個,放在當前分支上,原來的 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
查看歷史記錄發現提交順序錯誤,將前兩行順序調換。
相關
見上
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
查看最初一次為正確提交,得到版本號。執行完畢后日志里找到 “is the first bad commit” ,得到 18ed2ac。
(標記):出錯了 得到的結果不為這個。。。
相關
git bisect start
git bisect good fb088248
git bisect bad master
git bisect run make test
-
git bisect reset
回到之前執行 git bisect start 的狀態。
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編輯提交內容,刪除第二個。
相關
見上
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
查找到另一個分支,切換到另一個分支。
相關
reflog 是 git 用來記錄引用變化的一種機制。
git reflog
命令可以對 git 誤操作進行數據恢復。
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
相關
修復提交文件中的錯誤:
-
git reset --hard HEAD
把工作目錄中所有未提交的內容清空。 -
git checkout
恢復一個文件 -
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
查看日志,回覆至對應的版本
相關
恢復已修改的文件:
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'
相關
處理沖突
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
相關
git submodule add 子模塊倉庫地址 路徑
Git Submodule 使用簡介
level 56
最后
好多還是半懂不懂的狀態%>_<%... 嘛 有錯請指正:-D