Diff
- 查看工做區(working directory)和暫存區(staged)之間差別:
git diff
- 查看工做區(working directory)與當前倉庫版本(repository)HEAD版本差別:
git diff HEAD
- 查看暫存區(staged)與當前倉庫版本(repository)差別:
git diff --cached
/git diff --staged
- 不查看具體改動,只查看改動了哪些類:
git diff --stat
Merge
![](./_image/2016-07-14 20-53-25.jpg?r=80)git
- 解決衝突後/獲取遠程最新代碼後合併代碼:
git merge branchname
,將 branchname 分支上面的代碼合併到當前分支 - 保留該存在版本合併log:
git merge --no-ff branchname
參數--no-ff
防止 fast-forward 的提交,詳情參考:the difference,fast-forward:分支內容一致,指針直接移動,並未能看出分支信息
Rebase
Rebase 同 Merge 的結果是同樣的,就是合併本地、遠程的改動,但過程當中還有區別。程序員
git checkout mywork
git rebase origin
這些命令會把你的"mywork"分支裏的每一個提交(commit)取消掉,而且把它們臨時 保存爲補丁(patch)(這些補丁 放到".git/rebase"目錄中),而後把"mywork"分支更新 到最新的"origin"分支,最後把保存的這些補丁應用 到"mywork"分支上。 一張圖分清 rebase 和 merge 的區別github
![](./_image/2016-07-19 20-35-30.jpg?r=56) 在rebase的過程當中,也許會出現衝突(conflict). 在這種狀況,Git會中止rebase並會讓你去解決衝突;在解決完衝突後,用 git-add
命令去更新這些內容的索引(index), 而後,你無需執行 git-commit,只要執行: git rebase --continue
這樣git會繼續應用(apply)餘下的補丁。在任什麼時候候,你能夠用 --abort 參數來終止rebase的行動,而且"mywork" 分支會回到rebase開始前的狀態。 git rebase --abort
web
==============================================================shell
Git飛行規則(Flight Rules)
🌍 English ∙ Español ∙ Русский ∙ 簡體中文∙ 한국어 ∙ Tiếng Việt ∙ Françaisvim
前言
- 英文原版README
- 翻譯可能存在錯誤或不標準的地方,歡迎你們指正和修改,謝謝!
什麼是"飛行規則"?
一個 宇航員指南 (如今, 程序員們都在使用GIT) 是關於出現問題事後應該怎麼操做。緩存
飛行規則(Flight Rules) 是記錄在手冊上的來之不易的一系列知識,記錄了某個事情發生的緣由,以及怎樣一步一步的進行處理。本質上, 它們是特定場景的很是詳細的標準處理流程。 [...]安全
自20世紀60年代初以來,NASA一直在捕捉(capturing)咱們的失誤,災難和解決方案, 當時水星時代(Mercury-era)的地面小組首先開始將「經驗教訓」收集到一個綱要(compendium)中,該綱如今已經有上千個問題情景,從發動機故障到破損的艙口把手到計算機故障,以及它們對應的解決方案。bash
— Chris Hadfield, 一個宇航員的生活指南(An Astronaut's Guide to Life)。app
這篇文章的約定
爲了清楚的表述,這篇文檔裏的全部例子使用了自定義的bash 提示,以便指示當前分支和是否有暫存的變化(changes)。分支名用小括號括起來,分支名後面跟的*
表示暫存的變化(changes)。
Table of Contents generated with DocToc
- 編輯提交(editting commits)
- 暫存(Staging)
- 未暫存(Unstaged)的內容
- 分支(Branches)
- Rebasing 和合並(Merging)
- 雜項(Miscellaneous Objects)
- 跟蹤文件(Tracking Files)
- 配置(Configuration)
- 我不知道我作錯了些什麼
- 其它資源(Other Resources)
編輯提交(editting commits)
我剛纔提交了什麼?
若是你用 git commit -a
提交了一次變化(changes),而你又不肯定到底此次提交了哪些內容。 你就能夠用下面的命令顯示當前HEAD
上的最近一次的提交(commit):
(master)$ 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]
若是你尚未推到遠程, 把Git重置(reset)到你最後一次提交前的狀態就能夠了(同時保存暫存的變化):
(my-branch*)$ git reset --soft HEAD@{1}
這隻能在沒有推送以前有用. 若是你已經推了, 惟一安全能作的是 git revert SHAofBadCommit
, 那會建立一個新的提交(commit)用於撤消前一個提交的全部變化(changes); 或者, 若是你推的這個分支是rebase-safe的 (例如: 其它開發者不會從這個分支拉), 只須要使用 git push -f
; 更多, 請參考 the above section。
刪除任意提交(commit)
一樣的警告:不到萬不得已的時候不要這麼作.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]
或者作一個 交互式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)代替舊的, 因此若是以前你已經往遠程倉庫上推過一次修正前的提交(commit),那你如今就必須強推(force push) (-f
)。 注意 – 老是 確保你指明一個分支!
(my-branch)$ git push origin mybranch -f
通常來講, 要避免強推. 最好是建立和推(push)一個新的提交(commit),而不是強推一個修正後的提交。後者會使那些與該分支或該分支的子分支工做的開發者,在源歷史中產生衝突。
我意外的作了一次硬重置(hard reset),我想找回個人內容
若是你意外的作了 git reset --hard
, 你一般能找回你的提交(commit), 由於Git對每件事都會有日誌,且都會保存幾天。
(master)$ git reflog
你將會看到一個你過去提交(commit)的列表, 和一個重置的提交。 選擇你想要回到的提交(commit)的SHA,再重置一次:
(master)$ git reset --hard SHA1234
這樣就完成了。
暫存(Staging)
我須要把暫存的內容添加到上一次的提交(commit)
(my-branch*)$ git commit --amend
我想要暫存一個新文件的一部分,而不是這個文件的所有
通常來講, 若是你想暫存一個文件的一部分, 你可這樣作:
$ git add --patch filename.x
-p
簡寫。這會打開交互模式, 你將可以用 s
選項來分隔提交(commit); 然而, 若是這個文件是新的, 會沒有這個選擇, 添加一個新文件時, 這樣作:
$ git add -N filename.x
而後, 你須要用 e
選項來手動選擇須要添加的行,執行 git diff --cached
將會顯示哪些行暫存了哪些行只是保存在本地了。
我想把在一個文件裏的變化(changes)加到兩個提交(commit)裏
git add
會把整個文件加入到一個提交. git add -p
容許交互式的選擇你想要提交的部分.
我想把暫存的內容變成未暫存,把未暫存的內容暫存起來
這個有點困難, 我能想到的最好的方法是先stash未暫存的內容, 而後重置(reset),再pop第一步stashed的內容, 最後再add它們。
$ git stash -k
$ git reset --hard
$ git stash pop
$ git add -A
未暫存(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 (master)$ git checkout -f
重置某個特殊的文件, 你能夠用文件名作爲參數:
$ git reset filename
我想丟棄某些未暫存的內容
若是你想丟棄工做拷貝中的一部份內容,而不是所有。
簽出(checkout)不須要的內容,保留須要的。
$ git checkout -p
# Answer y to all of the snippets you want to drop
另一個方法是使用 stash
, Stash全部要保留下的內容, 重置工做拷貝, 從新應用保留的部分。
$ git stash -p
# Select all of the snippets you want to save $ git reset --hard $ git stash pop
或者, stash 你不須要的部分, 而後stash drop。
$ git stash -p
# Select all of the snippets you don't want to save $ git stash drop
分支(Branches)
我從錯誤的分支拉取了內容,或把內容拉取到了錯誤的分支
這是另一種使用 git reflog
狀況,找到在此次錯誤拉(pull) 以前HEAD的指向。
(master)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
重置分支到你所需的提交(desired commit):
$ git reset --hard c5bc55a
完成。
我想扔掉本地的提交(commit),以便個人分支與遠程的保持一致
先確認你沒有推(push)你的內容到遠程。
git status
會顯示你領先(ahead)源(origin)多少個提交:
(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) #
一種方法是:
(master)$ git reset --hard origin/my-branch
我須要提交到一個新分支,但錯誤的提交到了master
在master下建立一個新分支,不切換到新分支,仍在master下:
(master)$ git branch my-branch
把master分支重置到前一個提交:
(master)$ git reset --hard HEAD^
HEAD^
是 HEAD^1
的簡寫,你能夠經過指定要設置的HEAD
來進一步重置。
或者, 若是你不想使用 HEAD^
, 找到你想重置到的提交(commit)的hash(git log
可以完成), 而後重置到這個hash。 使用git push
同步內容到遠程。
例如, master分支想重置到的提交的hash爲a13b85e
:
(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
簽出(checkout)剛纔新建的分支繼續工做:
(master)$ git checkout my-branch
我想保留來自另一個ref-ish的整個文件
假設你正在作一個原型方案(原文爲working spike (see note)), 有成百的內容,每一個都工做得很好。如今, 你提交到了一個分支,保存工做內容:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
當你想要把它放到一個分支裏 (多是feature
, 或者 develop
), 你關心是保持整個文件的完整,你想要一個大的提交分隔成比較小。
假設你有:
- 分支
solution
, 擁有原型方案, 領先develop
分支。 - 分支
develop
, 在這裏你應用原型方案的一些內容。
我去能夠經過把內容拿到你的分支裏,來解決這個問題:
(develop)$ git checkout solution -- file1.txt
這會把這個文件內容從分支 solution
拿到分支 develop
裏來:
# 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
而後, 正常提交。
Note: 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)提交到了同一個分支,而這些提交應該分佈在不一樣的分支裏
假設你有一個master
分支, 執行git log
, 你看到你作過兩次提交:
(master)$ 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
讓咱們用提交hash(commit hash)標記bug (e3851e8
for #21, 5ea5173
for #14).
首先, 咱們把master
分支重置到正確的提交(a13b85e
):
(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
如今, 咱們對 bug #21 建立一個新的分支:
(master)$ git checkout -b 21
(21)$
接着, 咱們用 cherry-pick 把對bug #21的提交放入當前分支。 這意味着咱們將應用(apply)這個提交(commit),僅僅這一個提交(commit),直接在HEAD上面。
(21)$ git cherry-pick e3851e8
這時候, 這裏可能會產生衝突, 參見交互式 rebasing 章 衝突節 解決衝突.
再者, 咱們爲bug #14 建立一個新的分支, 也基於master
分支
(21)$ git checkout master
(master)$ git checkout -b 14
(14)$
最後, 爲 bug #14 執行 cherry-pick
:
(14)$ git cherry-pick 5ea5173
我想刪除上游(upstream)分支被刪除了的本地分支
一旦你在github 上面合併(merge)了一個pull request, 你就能夠刪除你fork裏被合併的分支。 若是你不許備繼續在這個分支裏工做, 刪除這個分支的本地拷貝會更乾淨,使你不會陷入工做分支和一堆陳舊分支的混亂之中。
$ git fetch -p
我不當心刪除了個人分支
若是你按期推送到遠程, 多數狀況下應該是安全的,但有些時候仍是可能刪除了尚未推到遠程的分支。 讓咱們先建立一個分支和一個新的文件:
(master)$ git checkout -b my-branch
(my-branch)$ git 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)$ foo.txt added 1 files changed, 1 insertions(+) create mode 100644 foo.txt (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
如今咱們切回到主(master)分支,‘不當心的’刪除my-branch
分支
(my-branch)$ git checkout master
Switched to branch 'master' Your branch is up-to-date with 'origin/master'. (master)$ git branch -D my-branch Deleted branch my-branch (was 4e3cd85). (master)$ echo oh noes, deleted my branch! oh noes, deleted my branch!
在這時候你應該想起了reflog
, 一個升級版的日誌,它存儲了倉庫(repo)裏面全部動做的歷史。
(master)$ git reflog 69204cd HEAD@{0}: checkout: moving from my-branch to master 4e3cd85 HEAD@{1}: commit: foo.txt added 69204cd HEAD@{2}: checkout: moving from master to my-branch
正如你所見,咱們有一個來自刪除分支的提交hash(commit hash),接下來看看是否能恢復刪除了的分支。
(master)$ 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
在rebasing出錯的時候也是一樣有用的。
我想刪除一個分支
刪除一個遠程分支:
(master)$ git push origin --delete my-branch
你也能夠:
(master)$ git push origin :my-branch
刪除一個本地分支:
(master)$ git branch -D my-branch
我想從別人正在工做的遠程分支簽出(checkout)一個分支
首先, 從遠程拉取(fetch) 全部分支:
(master)$ git fetch --all
假設你想要從遠程的daves
分支簽出到本地的daves
(master)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin. Switched to a new branch 'daves'
(--track
是 git checkout -b [branch] [remotename]/[branch]
的簡寫)
這樣就獲得了一個daves
分支的本地拷貝, 任何推過(pushed)的更新,遠程都能看到.
Rebasing 和合並(Merging)
我想撤銷rebase/merge
你能夠合併(merge)或rebase了一個錯誤的分支, 或者完成不了一個進行中的rebase/merge。 Git 在進行危險操做的時候會把原始的HEAD保存在一個叫ORIG_HEAD的變量裏, 因此要把分支恢復到rebase/merge前的狀態是很容易的。
(my-branch)$ git reset --hard ORIG_HEAD
我已經rebase過, 可是我不想強推(force push)
不幸的是,若是你想把這些變化(changes)反應到遠程分支上,你就必須得強推(force push)。 是因你快進(Fast forward)了提交,改變了Git歷史, 遠程分支不會接受變化(changes),除非強推(force push)。這就是許多人使用 merge 工做流, 而不是 rebasing 工做流的主要緣由之一, 開發者的強推(force push)會使大的團隊陷入麻煩。使用時須要注意,一種安全使用 rebase 的方法是,不要把你的變化(changes)反映到遠程分支上, 而是按下面的作:
(master)$ git checkout my-branch
(my-branch)$ git rebase -i master
(my-branch)$ git checkout master
(master)$ git merge --ff-only my-branch
更多, 參見 this SO thread.
我須要組合(combine)幾個提交(commit)
假設你的工做分支將會作對於 master
的pull-request。 通常狀況下你不關心提交(commit)的時間戳,只想組合 全部 提交(commit) 到一個單獨的裏面, 而後重置(reset)重提交(recommit)。 確保主(master)分支是最新的和你的變化都已經提交了, 而後:
(my-branch)$ git reset --soft master
(my-branch)$ git commit -am "New awesome feature"
若是你想要更多的控制, 想要保留時間戳, 你須要作交互式rebase (interactive rebase):
(my-branch)$ git rebase -i master
若是沒有相對的其它分支, 你將不得不相對本身的HEAD
進行 rebase。 例如:你想組合最近的兩次提交(commit), 你將相對於HEAD~2
進行rebase, 組合最近3次提交(commit), 相對於HEAD~3
, 等等。
(master)$ git rebase -i HEAD~2
在你執行了交互式 rebase的命令(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
全部以 #
開頭的行都是註釋, 不會影響 rebase.
而後,你能夠用任何上面命令列表的命令替換 pick
, 你也能夠經過刪除對應的行來刪除一個提交(commit)。
例如, 若是你想 單獨保留最舊(first)的提交(commit),組合全部剩下的到第二個裏面, 你就應該編輯第二個提交(commit)後面的每一個提交(commit) 前的單詞爲 f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature f b729ad5 fixup f e3851e8 another fix
若是你想組合這些提交(commit) 並重命名這個提交(commit), 你應該在第二個提交(commit)旁邊添加一個r
,或者更簡單的用s
替代 f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature s b729ad5 fixup s e3851e8 another fix
你能夠在接下來彈出的文本提示框裏重命名提交(commit)。
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 'master' on '8074d12'. # # Changes to be committed: # modified: README.md #
若是成功了, 你應該看到相似下面的內容:
(master)$ Successfully rebased and updated refs/heads/master.
安全合併(merging)策略
--no-commit
執行合併(merge)但不自動提交, 給用戶在作提交前檢查和修改的機會。 no-ff
會爲特性分支(feature branch)的存在過留下證據, 保持項目歷史一致。
(master)$ git merge --no-ff --no-commit my-branch
我須要將一個分支合併成一個提交(commit)
(master)$ git merge --squash my-branch
我只想組合(combine)未推的提交(unpushed commit)
有時候,在將數據推向上游以前,你有幾個正在進行的工做提交(commit)。這時候不但願把已經推(push)過的組合進來,由於其餘人可能已經有提交(commit)引用它們了。
(master)$ git rebase -i @{u}
這會產生一次交互式的rebase(interactive rebase), 只會列出沒有推(push)的提交(commit), 在這個列表時進行reorder/fix/squash 都是安全的。
檢查是否分支上的全部提交(commit)都合併(merge)過了
檢查一個分支上的全部提交(commit)是否都已經合併(merge)到了其它分支, 你應該在這些分支的head(或任何 commits)之間作一次diff:
(master)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
這會告訴你在一個分支裏有而另外一個分支沒有的全部提交(commit), 和分支之間不共享的提交(commit)的列表。 另外一個作法能夠是:
(master)$ git log master ^feature/120-on-scroll --no-merges
交互式rebase(interactive rebase)可能出現的問題
這個rebase 編輯屏幕出現'noop'
若是你看到的是這樣:
noop
這意味着你rebase的分支和當前分支在同一個提交(commit)上, 或者 領先(ahead) 當前分支。 你能夠嘗試:
- 檢查確保主(master)分支沒有問題
- rebase
HEAD~2
或者更早
有衝突的狀況
若是你不能成功的完成rebase, 你可能必需要解決衝突。
首先執行 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
你須要解決新提交的代碼(示例裏, 從中間==
線到new-commit
的地方)與HEAD
之間不同的地方.
有時候這些合併不是常複雜,你應該使用可視化的差別編輯器(visual diff editor):
(master*)$ git mergetool -t opendiff
在你解決完全部衝突和測試事後, git add
變化了的(changed)文件, 而後用git rebase --continue
繼續rebase。
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
若是在解決完全部的衝突事後,獲得了與提交前同樣的結果, 能夠執行git rebase --skip
。
任什麼時候候你想結束整個rebase 過程,回來rebase前的分支狀態, 你能夠作:
(my-branch)$ git rebase --abort
雜項(Miscellaneous Objects)
克隆全部子模塊
$ 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)
若是你想恢復一個已刪除標籤(tag), 能夠按照下面的步驟: 首先, 須要找到沒法訪問的標籤(unreachable tag):
$ git fsck --unreachable | grep tag
記下這個標籤(tag)的hash,而後用Git的 update-ref:
$ git update-ref refs/tags/<tag_name> <hash>
這時你的標籤(tag)應該已經恢復了。
已刪除補丁(patch)
若是某人在 GitHub 上給你發了一個pull request, 可是而後他刪除了他本身的原始 fork, 你將無法克隆他們的提交(commit)或使用 git am
。在這種狀況下, 最好手動的查看他們的提交(commit),並把它們拷貝到一個本地新分支,而後作提交。
作完提交後, 再修改做者,參見變動做者。 而後, 應用變化, 再發起一個新的pull request。
跟蹤文件(Tracking Files)
我只想改變一個文件名字的大小寫,而不修改內容
(master)$ git mv --force myfile MyFile
我想從Git刪除一個文件,但保留該文件
(master)$ 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)的用戶名和密碼
你可能有一個倉庫須要受權,這時你能夠緩存用戶名和密碼,而不用每次推/拉(push/pull)的時候都輸入,Credential helper能幫你。
$ 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)
我不知道我作錯了些什麼
你把事情搞砸了:你 重置(reset)
了一些東西, 或者你合併了錯誤的分支, 亦或你強推了後找不到你本身的提交(commit)了。有些時候, 你一直都作得很好, 但你想回到之前的某個狀態。
這就是 git reflog
的目的, reflog
記錄對分支頂端(the tip of a branch)的任何改變, 即便那個頂端沒有被任何分支或標籤引用。基本上, 每次HEAD的改變, 一條新的記錄就會增長到reflog
。遺憾的是,這隻對本地分支起做用,且它只跟蹤動做 (例如,不會跟蹤一個沒有被記錄的文件的任何改變)。
(master)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to master
c10f740 HEAD@{2}: checkout: moving from master to 2.2
上面的reflog展現了從master分支簽出(checkout)到2.2 分支,而後再籤回。 那裏,還有一個硬重置(hard reset)到一個較舊的提交。最新的動做出如今最上面以 HEAD@{0}
標識.
若是事實證實你不當心回移(move back)了提交(commit), reflog 會包含你不當心回移前master上指向的提交(0254ea7)。
$ git reset --hard 0254ea7
而後使用git reset就能夠把master改回到以前的commit,這提供了一個在歷史被意外更改狀況下的安全網。
(摘自).
其它資源(Other Resources)
書(Books)
- Pro Git - Scott Chacon's excellent git book
- Git Internals - Scott Chacon's other excellent git book
教程(Tutorials)
- Learn Git branching 一個基於網頁的交互式 branching/merging/rebasing 教程
- Getting solid at Git rebase vs. merge
- git-workflow - Aaron Meurer的怎麼使用Git爲開源倉庫貢獻
- GitHub as a workflow - 使用GitHub作爲工做流的趣事, 尤爲是空PRs
腳本和工具(Scripts and Tools)
- firstaidgit.io 一個可搜索的最常被問到的Git的問題
- git-extra-commands - 一堆有用的額外的Git腳本
- git-extras - GIT 工具集 -- repo summary, repl, changelog population, author commit percentages and more
- git-fire - git-fire 是一個 Git 插件,用於幫助在緊急狀況下添加全部當前文件, 作提交(committing), 和推(push)到一個新分支(阻止合併衝突)。
- git-tips - Git小提示
- git-town - 通用,高級Git工做流支持! http://www.git-town.com