git-flight-rules/README_zh-TW.md

42 KiB
Raw Blame History

Git飛行規則(Flight Rules)

🌍 EnglishEspañolРусский繁體中文簡體中文한국어Tiếng ViệtFrançais日本語

前言

  • 英文原版 README
  • 翻譯可能存在錯誤或不標準的地方,歡迎大家指正和修改,謝謝!

什麼是"飛行規則"?

這是一篇給太空人(這裡就是指使用 Git 的程式設計師們)的指南,用來指導問題出現後的應對之法。

飛行規則Flight Rules是記錄在手冊上的來之不易的一系列知識記錄了某個事情發生的原因以及怎樣一步一步的進行處理。本質上它們是特定場景的非常詳細的標準處理流程。[...]

自 20 世紀 60 年代初以來NASA 一直在捕捉capturing失誤、災難和解決方案。當時水星時代Mercury-era的地面小組首先開始將「經驗教訓」收集到一個綱要compendium該綱現在已經有上千個問題情景從發動機故障、到破損的艙口把手、再到計算機故障以及它們對應的解決方案。

——Chris Hadfield《一個太空人的生活指南》An Astronaut's Guide to Life

這篇文章的約定

為了清楚的表述,這篇文件裡的所有例子使用了自訂的 bash 提示以便指示目前分支和是否有暫存的變化changes。分支名用小括號括起來分支名後面跟的 * 表示暫存的變化changes

Join the chat at https://gitter.im/k88hudson/git-flight-rules

Table of Contents generated with DocToc

編輯提交editting commits

我剛才提交了什麼?

如果你用 git commit -a 提交了一次變化changes而你又不確定到底這次提交了哪些內容你可以用以下命令顯示目前 HEAD 上的最近一次的提交commit

(main)$ git show

或者

$ git log -n1 -p

我的提交訊息commit message寫錯了

如果你的提交訊息commit message寫錯了且這次提交commit還沒有推送push你可以透過下面的方法來修改提交訊息commit message

$ git commit --amend --only

這會使用你的預設編輯器來完成。你也可以只使用一個命令:

$ git commit --amend --only -m 'xxxxxxx'

如果你已經推送push了這次提交commit你可以修改這次提交commit然後強制推送force push但是不推薦這麼做。

我提交commit裡的使用者名稱和信箱不對

如果這只是單個提交commit修改它

$ git commit --amend --author "New Authorname <authoremail@mydomain.com>"

如果你需要修改所有歷史,參考 git filter-branch 的手冊。

我想從一個提交commit裡移除一個文件

通過下面的方法從一個提交commit裡移除一個文件

$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amend

這非常有用當你有一個開放的補丁open patch你往上面提交了一個不必要的文件你需要強制推送force push去更新這個遠程補丁。

我想刪除我最後一次提交commit

如果你需要刪除推送了的提交pushed commits你可以使用以下方法。但是這將不可逆的改變你的歷史也會搞亂那些已經從該倉庫拉取pulled了的人的歷史。簡而言之如果你不是很確定千萬不要這麼做。

$ git reset HEAD^ --hard
$ git push -f [remote] [branch]

如果你還沒有推送到遠端重設reset到你最後一次提交前的狀態就可以了同時保存暫存的變化

(my-branch)$ git reset --soft HEAD^

這只能在推送之前使用。如果你已經推送了,唯一安全的做法是 git revert SHAofBadCommit那會創建一個新的提交commit來撤消前一個提交的所有變化changes或者如果這個分支是 rebase-safe 的(例如:其他開發者不會從這個分支拉取),只需要使用 git push -f;參見上一節

刪除任意提交commit

同樣,除非必須,否則不要這麼做。

$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]

或者使用互動式變基interactive rebase 刪除那些你想要刪除的提交commit所對應的行。

我嘗試推送一個修正後的提交amended commit到遠端但是報錯

To https://github.com/yourusername/repo.git
! [rejected]        mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

注意變基rebasing和修正amending會用一個新的提交commit取代舊的所以如果舊的提交已經推送到遠端上了那你必須強制推送force push。注意總是確保你指明一個分支

(my-branch)$ git push origin mybranch -f

一般來說要避免強制推送。最好是創建和推送一個新的提交commit而不是強推一個修正後的提交。後者會使在該分支或該分支的子分支上工作的開發者在源歷史中產生衝突。

我意外地硬重設hard reset我想找回我的內容

如果你意外地做了 git reset --hard你通常能找回你的提交commit因為 Git 對每件事都會有日誌,且都會保存幾天。

(main)$ git reflog

你將會看到一個你過去提交commit的列表和一個重設的提交。選擇你想要回到的提交commit的 SHA再重設一次

(main)$ git reset --hard SHA1234

暫存staging

我需要把暫存的內容添加到上一次的提交commit

(my-branch*)$ git commit --amend

我想要暫存一個新文件的一部分,而不是這個文件的全部

一般來說,如果你想暫存一個文件的一部分,你可以使用以下命令來開啟互動式介面,並使用 s 選項來選擇想要的行。

$ git add --patch filename.x # 或 `-p`。

然而,當這個檔案是新的,則需改用以下命令:

$ git add -N filename.x

然後,你需要用 e 選項來選擇需要添加的行,執行 git diff --cached 將會顯示哪些行暫存了、哪些行只是保存在本地了。

我想把在一個文件裡的變化changes加到兩個提交commit

git add 會把整個文件加入到一個提交。git add -p 則允許你互動式地選擇想要提交的部分。

我想把暫存的內容變成未暫存,把未暫存的內容暫存起來

多數情況下你應該將所有的內容變為未暫存然後再加入add你想要的內容提交commit。 但如果你就是想這麼做你可以創建一個臨時的提交來儲存你已暫存的內容然後加入未暫存的內容並儲藏stash。最後重設reset最後一個提交將原本暫存的內容變為未暫存最後彈出儲藏pop stash

$ git commit -m "WIP"     # 將之前已暫存的內容提交。
$ git add .               # 加入未暫存的內容。
$ git stash               # 儲藏剛剛加入的內容。
$ git reset HEAD^         # 重設到父提交。
$ git stash pop --index 0 # 彈出儲藏。

註一:這裡使用 pop 僅僅是因為想盡可能保持冪等。 註二:假如不加上 --index,會把暫存的文件標記為未暫存。這裡解釋得比較清楚。(其大意是說,這是一個較為底層的問題,儲藏時會創建兩個提交,一個記錄 index 狀態、暫存的內容等,另一個紀錄 worktree 和其他的一些東西,如果你不在 apply 時加 indexGit 會把兩個一起銷毀所以暫存區stage裡就空了

未暫存unstaged的變化

我想把未暫存的變化移動到新分支

$ git checkout -b my-branch

我想把未暫存的變化移動到另一個已存在的分支

$ git stash
$ git checkout my-branch
$ git stash pop

我想丟棄本地未提交的變化uncommitted changes

如果你只是想重設源origin和你本地local之間的一些提交commit你可以

# one commit
(my-branch)$ git reset --hard HEAD^
# two commits
(my-branch)$ git reset --hard HEAD^^
# four commits
(my-branch)$ git reset --hard HEAD~4
# or
(main)$ git checkout -f

如果要重設某個特定檔案,可以用檔案名做為引數:

$ git reset filename

我想丟棄某些未暫存的變化

如果你想丟棄工作拷貝中的一部分內容,而不是全部。

簽出checkout不需要的內容保留需要的。

$ git checkout -p
# Answer y to all of the snippets you want to drop

另外一個方法是使用儲藏stash儲藏所有要保留的變化重設工作拷貝然後把儲藏彈出。

$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop

或者儲藏你不需要的部分然後丟棄儲藏drop stash

$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop

分支branches

我從錯誤的分支拉取了內容,或把內容拉取到了錯誤的分支

這是另外一種可以使用 git reflog 情況找到在這次錯誤拉取pull之前 HEAD 的指向。

(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here

然後,重設分支到所需的提交:

$ git reset --hard c5bc55a

完成。

我想丟棄本地的提交commit以讓分支與遠端保持一致

首先確認你沒有推送push你的內容到遠端。

git status 會顯示本地領先aheadorigin多少個提交

(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
#   (use "git push" to publish your local commits)
#

一種方法是:

(main)$ git reset --hard origin/my-branch

我需要提交到一個新分支,但錯誤的提交到了 main

main 下創建一個新分支:

(main)$ git branch my-branch

main 重設到前一個提交:

(main)$ git reset --hard HEAD^

HEAD^HEAD^1 的縮寫,你可以指定數字來進一步重設。或者,如果你不想使用 HEAD^你可以指定一個提交commit的雜湊值hash可以使用 git log 查看),如 git reset --hard a13b85e

簽出checkout到剛才新建的分支繼續工作

(main)$ git checkout my-branch

我想保留來自另外一個 ref-ish 的整個文件

假設你正在做一個原型方案(原文為 working spike有成百上千的內容。當你提交到一個分支儲存工作內容

(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."

當你想要把它放到一個分支裡(假設是 develop),你希望保持整個檔案的完整,並將大的提交分割成數個小的。

假設這裡有:

  • 分支 solution,擁有原型方案,領先 develop 分支。
  • 分支 develop,應用原型方案的一些內容。

可以將內容放到那個分支中:

(develop)$ git checkout solution -- file1.txt
(develop)$ git status
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
#        modified:   file1.txt

然後,普通地提交。

Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ Wikipedia.

我把幾個提交commit提交到了同一個分支而這些提交應該在不同的分支上

假設在 main 分支,執行 git log 的結果如下:

(main)$ git log

commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date:   Tue Jul 22 15:39:27 2014 -0400

    Bug #21 - Added CSRF protection

commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date:   Tue Jul 22 15:39:12 2014 -0400

    Bug #14 - Fixed spacing on title

commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date:   Tue Jul 21 01:12:48 2014 -0400

    First commit

要把 e3851e85ea5173 分別移到新的分支,首先,要把 main 分支重設到正確的提交(a13b85e

(main)$ git reset --hard a13b85e
HEAD is now at a13b85e

新增一個分支:

(main)$ git checkout -b 21

接著然後挑揀cherry-pick提交到正確的分支上。這意味著我們將直接在 HEAD 上面應用apply這個提交。

(21)$ git cherry-pick e3851e8

這可能會造成衝突conflict參見〈互動式變基衝突〉來解決衝突。

同樣地,為 5ea5173 也創建一個分支,並把提交挑揀到其上:

(main)$ git checkout -b 14
(14)$ git cherry-pick 5ea5173

我想刪除上遊upstream刪除了的本地分支

比方說,在 GitHub 中合併merge了拉取請求pull request就可以刪除掉分支。如果不準備繼續在這個分支上工作刪除這個分支會使工作拷貝working copy更乾淨。

$ git fetch -p

我不小心刪除了分支

如果你定期推送push到遠端remote多數情況下應該是安全的但有時可能刪除了還沒推送的分支。

為了模擬這種情況,首先,創建一個分支和一個檔案:

(main)$ git checkout -b my-branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt

加入變化並提交:

(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ git log

commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date:   Wed Jul 30 00:34:10 2014 +0200

    foo.txt added

commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date:   Tue Jul 29 13:14:46 2014 -0400

    Fixes #6: Force pushing after amending commits

現在,切回 main 分支,並「不小心」刪除了 my-branch

(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!

你應該想起了 reflog,它記錄了所有動作。

(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch

如你所見其中包含了刪除分支的提交的雜湊值hash。可以藉此把提交找回來

(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt

我們把遺失的檔案找回來了。Git 的 reflog 在變基rebase出錯時也同樣有用。

我想刪除一個分支

刪除一個遠端分支:

(main)$ git push origin --delete my-branch

或:

(main)$ git push origin :my-branch

刪除一個本地分支:

(main)$ git branch -D my-branch

我想從別人正在工作的遠端分支簽出checkout一個分支

首先從遠端獲取fetch所有分支

(main)$ git fetch --all

假設你想要從遠端的 daves 分支簽出到本地的 daves

(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'

--trackgit checkout -b [branch] [remotename]/[branch] 的縮寫。)

這樣就有 daves 的本地拷貝了。

變基rebase與合併merge

撤銷變基或合併

你可能對一個錯誤的分支做了變基或合併或者無法完成變基或合併。Git 在進行危險操作時,會將原本的 HEAD 存成 ORIG_HEAD,因此可以很容易的恢復到之前的狀態。

(my-branch)$ git reset --hard ORIG_HEAD

我做了變基但是我不想強制推送force push

不幸的是如果你想把變基的結果反映在遠端分支上你必須強制推送force push。因為你改變了歷史遠端不會接受使用快進fast-forward而必須強制推送。這就是許多人使用合併工作流程、而不是變基工作流程的主要原因之一開發者的強制推送會使大團隊陷入麻煩。

一種安全的方式是,不要推送到遠端:

(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch

參見此 StackOverflow 討論串

我需要組合combine幾個提交commit

假設你的工作分支將對 main 分支做拉取請求pull request

最簡單的情況下不會關心提交的時間戳timestamp只想將所有的提交組合成一個單獨的提交你可以重設reset和重新提交recommit。確保 main 是最新的,且你的變化都已經提交,然後:

(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"

如果你想保留更多控制、保留時間戳你需要互動式變基interactive rebase

(my-branch)$ git rebase -i main

如果沒有相對於其他分支,將不得不相對於 HEAD 變基。例如,要組合最近的兩次提交,需相對於 HEAD~2 變基,組合最近三次提交,則相對於 HEAD~3,以此類推。

(main)$ git rebase -i HEAD~2

在執行了互動式變基的命令interactive rebase command你會在編輯器裡看到類似以下的內容

pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix

# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

(以 # 開頭的行是註解,不影響變基。)

你可以以註解中提到的命令替換 pick也可以刪除一行來刪除對應的提交。例如如果要保留最舊first的提交並將其他組合成第二個提交應該將第二個提交之後所有提交的命令改為 f

pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix

如果要組合並重新命名這個提交,應該在第二個提交加上 r,或使用 s 取代 f

pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix

你可以在接著彈出的文字提示中重新命名那個提交:

Newer, awesomer features

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
#	modified:   README.md
#

應該會看到如下的成功訊息:

(main)$ Successfully rebased and updated refs/heads/main.

安全合併的策略

--no-commit 選項會合併但不會自動提交,給使用者在提交前檢查和修改的機會。--no-ff 會留下功能分支feature branch存在過的證據保持歷史一致。

(main)$ git merge --no-ff --no-commit my-branch

我需要將一個分支合併成一個提交

(main)$ git merge --squash my-branch

我只想組合combine未推送的提交

假設在推送到上遊前,你有幾個正在進行的工作提交,這時候不希望把已推送的提交也組合進來,因為其他人可能已經有提交引用它們了。

(main)$ git rebase -i @{u}

這會進行一次互動式變基interactive rebase只會列出還沒推送的提交。對這些提交重新排序或做 squash、fixup 都是安全的。

檢查分支上的所有提交是否都合併了

要檢查一個分支上的所有提交是否都已經合併進了其它分支,應該在這些分支的 HEAD(或任何提交)之間檢查差異:

(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll

這會顯示一個分支有而另一個分支沒有的提交,和分支之間不共享的提交的列表。

另一個方法是:

(main)$ git log main ^feature/120-on-scroll --no-merges

互動式變基interactive rebase可能出現的問題

編輯介面出現「noop」

如果你看到:

noop

表示變基的分支和目前分支在同一個提交或領先ahead目前分支。你可以嘗試

  • 確保 main 分支沒有問題
  • HEAD~2 或更早的提交變基

有衝突的情況

如果不能成功的完成變基你可能必須要解決衝突resolve conflict

首先用 git status 檢查哪些檔案有衝突:

(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md

在這個例子中,README.md 有衝突。打開衝突的檔案會看到類似下面的內容:

   <<<<<<< HEAD
   some code
   =========
   some code
   >>>>>>> new-commit

你必須解決新提交的內容和 HEAD 中的內容的衝突。

有時候衝突非常複雜你可以使用可視化差異編輯器visual diff editor

(main*)$ git mergetool -t opendiff

解決所有衝突後,加入變化了的檔案,然後用 git rebase --continue 繼續變基:

(my-branch)$ git add README.md
(my-branch)$ git rebase --continue

如果在解決所有衝突過後,得到了與提交前一樣的結果,可以使用 git rebase --skip

如果想放棄變基,回到之前的狀態,可以在任何時候用:

(my-branch)$ git rebase --abort

儲藏stash

儲藏所有變化

儲藏工作目錄下所有變化:

$ git stash

可以使用 -u 選項排除一些檔案:

$ git stash -u

儲藏指定檔案

儲藏一個檔案:

$ git stash push working-directory-path/filename.ext

儲藏多個檔案:

$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext

儲藏時附加訊息

$ git stash save <message>

$ git stash push -m <message>

如此可以在使用 stash list 時看到訊息。

應用apply指定儲藏

可以先列出擁有的儲藏:

$ git stash list

然後,將以下命令的 n 替換成儲藏在堆疊中的位置(最上方為 0),應用指定儲藏:

$ git stash apply "stash@{n}"

除此之外,也可以使用時間標記(假如你能記住的話),如:

$ git stash apply "stash@{2.hours.ago}"

儲藏時保留未暫存的內容

你需要先手動創建一個儲藏提交,然後使用 git stash store

$ git stash create
$ git stash store -m "commit-message" CREATED_SHA1

雜項

複製所有子模組

$ git clone --recursive git://github.com/foo/bar.git

如果已經複製了:

$ git submodule update --init --recursive

刪除標籤tag

$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>

恢復已刪除標籤tag

如果想恢復一個已刪除標籤首先找到無法觸及的標籤unreachable tag

$ git fsck --unreachable | grep tag

記下這個標籤的雜湊值,然後用 Git 的 update-ref

$ git update-ref refs/tags/<tag_name> <hash>

已刪除補丁patch

如果有人在 GitHub 上向你提出了拉取請求pull request但他接著刪除了他的分叉fork你無法複製他的提交或使用 git am。在這種情況下,最好手動的查看他們的提交,把它們拷貝到一個本地新分支,然後提交。

最後,再修改作者,參見〈變更作者〉。然後,應用變化,再發起一個新的拉取請求。

追蹤檔案tracking files

我只想改變一個檔案名字的大小寫,而不修改內容

(main)$ git mv --force myfile MyFile

我想從 Git 刪除一個檔案,但保留該檔案

(main)$ git rm --cached log.txt

組態configuration

我想為 Git 命令設定別名alias

在 OS X 和 Linux 下Git 的組態檔案儲存在 ~/.gitconfig。可以在 [alias] 部分設定一些快捷別名(以及容易拼錯的),如:

[alias]
    a = add
    amend = commit --amend
    c = commit
    ca = commit --amend
    ci = commit -a
    co = checkout
    d = diff
    dc = diff --changed
    ds = diff --staged
    f = fetch
    loll = log --graph --decorate --pretty=oneline --abbrev-commit
    m = merge
    one = log --pretty=oneline
    outstanding = rebase -i @{u}
    s = status
    unpushed = log @{u}
    wc = whatchanged
    wip = rebase -i @{u}
    zap = fetch -p

我想快取一個倉庫repository的使用者名稱和密碼

假設有一個倉庫需要授權,這時你可以快取使用者名稱和密碼,而不用每次推送和拉取時都輸入一次:

$ git config --global credential.helper cache
# Set Git to use the credential memory cache.
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds).

我不知道我做錯了什麼

如果你把事情搞砸了:你錯誤地重設、合併,或強制推送後找不到自己的提交了,抑或你做得很好,但你想回到以前的某個狀態。

這就是 git reflog 的目的,reflog 記錄對分支頂端the tip of a branch的任何改變即使沒有任何分支或標籤參考那個頂端。基本上只要 HEAD 改變,reflog 就會記錄下來。遺憾的是,這只對本地分支起作用,且它只追蹤動作(例如,不會追蹤一個沒被記錄的檔案的任何改變)。

(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2

上面的 reflog 顯示了曾經從 main 分支簽出到 2.2 分支,然後再簽出回去,還有硬重設到一個較舊的提交。最新的動作出現在最上面,並以 HEAD@{0} 標示。

如果你不小心回移move back了提交reflog 會包含回移前 main 參考的提交(在這個例子中是 0254ea7)。只要硬重設就能恢復到之前的狀態,這提供了歷史不小心被變更時的安全網。

$ git reset --hard 0254ea7

摘自這裡

其他資源

書籍

  • Pro Git——Scott Chacon 的傑出書籍
  • Git Internals——Scott Chacon 的另一本傑出書籍

教學

腳本和工具

  • firstaidgit.io——一個可搜尋的 Git 常見問題集
  • git-extra-commands——一堆有用的額外 Git 腳本
  • git-extras——Git 工具集倉庫概要、repl、變更記錄、提交百分比和更多
  • git-fire——git-fire 是一個 Git 插件,用於在緊急情況下幫助加入目前所有檔案、提交、推送到一個新分支(防止合併衝突)。
  • git-tips——Git 小撇步
  • git-town——通用、高級 Git 工作流程支援! http://www.git-town.com

GUI 客戶端

  • GitKraken——豪華的 Git 客戶端,適用於 Windows、Mac、Linux
  • git-cola——又一個 Git 客戶端,適用於 Windows、OS X
  • GitUp——一個新的 Git 客戶端,在解決 Git 的複雜問題上有自己的特點
  • gitx-dev——又一個圖形化的 Git 客戶端,適用於 OS X
  • Source Tree——簡單而強大的免費 Git GUI 客戶端,適用於 Windows、OS X
  • Tower——圖形化 Git 客戶端,適用於 OS X付費