日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Git之深入解析48个经典操作场景的分析和处理,专治不会合并代码

發布時間:2024/5/28 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Git之深入解析48个经典操作场景的分析和处理,专治不会合并代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、剛才提交了什么?

  • 如果你用 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>"
  • 如果需要修改所有歷史,如下所示,把 OLD_EMAIL,CORRECT_NAME,CORRECT_EMAIL 改成自己的新舊郵箱用戶名:
git filter-branch -f --env-filter ' OLD_EMAIL="原來的郵箱" CORRECT_NAME="現在的名字" CORRECT_EMAIL="現在的郵箱" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] thenexport GIT_COMMITTER_NAME="$CORRECT_NAME"export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] thenexport GIT_AUTHOR_NAME="$CORRECT_NAME"export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags
  • 回到 git bash 界面,復制粘貼上面的代碼并按回車執行,如果 commit 記錄比較多的話執行的時間會比較長,等待執行完成后,再查看 git log 可以看到已經修改成功。
  • 如果上面的批量修改命令執行失敗的話,執行以下命令:
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD
  • 再次復制粘貼批量修改的代碼并按回車執行。
  • 最后再執行以下命令,推送到遠程:
git push origin --force --all
  • 現在即可看到 Github 之前的提交記錄中,name 和 email 信息都已更新。

四、從一個提交(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。

六、刪除任意提交(commit)

  • 刪除任意 commit 的命令如下,當然還可以做一個交互式 rebase 刪除那些你想要刪除的提交(commit)里所對應的行:
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT $ git push -f [remote] [branch]
  • 需要注意的是:不到萬不得已的時候不要這么做。

七、試推一個修正后的提交(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 對每件事都會有日志,且都會保存幾天:
(main)$ git reflog
  • 你將會看到一個過去提交(commit)的列表,和一個重置的提交,選擇想要回到的提交(commit)的 SHA,再重置一次:
(main)$ 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

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

  • 多數情況下,應該將所有的內容變為未暫存,然后再選擇想要的內容進行 commit,但假定你就是想要這么做,這里可以創建一個臨時的 commit 來保存已暫存的內容,然后暫存你的未暫存的內容并進行 stash,然后 reset 最后一個 commit 將原本暫存的內容變為未暫存,最后 stash pop 回來:
$ git commit -m "WIP" $ git add . $ git stash $ git reset HEAD^ $ git stash pop --index 0
  • 注意:這里使用 pop 僅僅是因為想盡可能保持冪等,還有假如不加上 --index,你會把暫存的文件標記為為存儲。

十、未暫存(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,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 的指向:
(main)$ 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) #
  • 一種方法是:
(main)$ git reset --hard origin/my-branch

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

  • 在 main 下創建一個新分支,不切換到新分支,仍然在 main 下:
(main)$ git branch my-branch
  • 把 main 分支重置到前一個提交:
(main)$ git reset --hard HEAD^
  • HEAD^ 是 HEAD^1 的簡寫,可以通過指定要設置的 HEAD 來進一步重置。或者,如果不想使用 HEAD^,找到想重置到的提交(commit)的 hash(git log 能夠完成), 然后重置到這個 hash,使用 git push 同步內容到遠程。
  • 例如 main 分支想重置到的提交的 hash 為 a13b85e:
(main)$ git reset --hard a13b85e HEAD is now at a13b85e
  • 簽出(checkout)剛才新建的分支繼續工作:
(main)$ 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.

⑤ 把幾個提交(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
  • 用提交 hash(commit hash) 標記 bug (e3851e8 for #21, 5ea5173 for #14):
    • 首先把 main 分支重置到正確的提交(a13b85e):
(main)$ git reset --hard a13b85e HEAD is now at a13b85e
    • 現在對 bug #21 創建一個新的分支:
(main)$ git checkout -b 21 (21)$
    • 接著用 cherry-pick 把對 bug #21 的提交放入當前分支,這意味著我們將應用(apply)這個提交(commit),僅僅這一個提交(commit),直接在 HEAD 上面:
(21)$ git cherry-pick e3851e8
    • 這時候,這里可能會產生沖突,再者為 bug #14 創建一個新的分支,也基于 main 分支:
(21)$ git checkout main (main)$ git checkout -b 14 (14)$
    • 最后為 bug #14 執行 cherry-pick:
(14)$ git cherry-pick 5ea5173

⑥ 想刪除上游(upstream)分支被刪除的本地分支

  • 一旦你在 github 上面合并(merge)了一個 pull request,就可以刪除 fork 里被合并的分支;
  • 如果你不準備繼續在這個分支里工作,刪除這個分支的本地拷貝會更干凈,使你不會陷入工作分支和一堆陳舊分支的混亂之中:
$ git fetch -p

⑦ 不小心刪除了個人的分支

  • 如果定期推送到遠程,多數情況下應該是安全的,但有些時候還是可能刪除了還沒有推到遠程的分支,讓我們先創建一個分支和一個新的文件:
(main)$ 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
  • 現在切回到主(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,一個升級版的日志,它存儲了倉庫(repo)里面所有動作的歷史:
(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(commit 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 在 rebasing 出錯的時候也是同樣有用的。

⑧ 想刪除一個分支

  • 刪除一個遠程分支:
(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' # (--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)反映到遠程分支上,而是按下面的做:
(main)$ git checkout my-branch (my-branch)$ git rebase -i main (my-branch)$ git checkout main (main)$ git merge --ff-only my-branch

③ 組合(combine)幾個提交(commit)

  • 假設你的工作分支將會做對于 main 的 pull-request,一般情況下你不關心提交(commit)的時間戳,只想組合所有提交(commit)到一個單獨的里面,然后重置(reset)重提交(recommit),確保主(main)分支是最新的和你的變化都已經提交,然后:
(my-branch)$ git reset --soft main (my-branch)$ git commit -am "New awesome feature"
  • 如果你想要更多的控制,想要保留時間戳,需要做交互式 rebase (interactive rebase):
(my-branch)$ git rebase -i main
  • 如果沒有相對的其它分支,你將不得不相對自己的 HEAD 進行 rebase。例如:想組合最近的兩次提交(commit),你將相對于 HEAD~2 進行 rebase,組合最近 3 次提交(commit),相對于 HEAD~3 等。
(main)$ 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 'main' on '8074d12'. # # Changes to be committed: # modified: README.md #
  • 如果成功,可以看到類似下面的內容:
(main)$ Successfully rebased and updated refs/heads/main.

④ 安全合并(merging)策略

  • –no-commit 執行合并(merge)但不自動提交,給用戶在做提交前檢查和修改的機會。no-ff 會為特性分支(feature branch)的存在過留下證據,保持項目歷史一致:
(main)$ git merge --no-ff --no-commit my-branch

⑤ 將一個分支合并成一個提交(commit)

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

⑥ 組合(combine)未推的提交(unpushed commit)

  • 有時候,在將數據推向上游之前,你有幾個正在進行的工作提交(commit),這時候不希望把已經推(push)過的組合進來,因為其他人可能已經有提交(commit)引用它們:
(main)$ git rebase -i @{u}
  • 這會產生一次交互式的 rebase(interactive rebase),只會列出沒有推(push)的提交(commit),在這個列表時進行 reorder/fix/squash 都是安全的。

⑦ 檢查是否分支上的所有提交(commit)都合并(merge)過

  • 檢查一個分支上的所有提交(commit)是否都已經合并(merge)到其它分支,應該在這些分支的 head(或任何 commits)之間做一次 diff:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
  • 這會告訴你在一個分支里有而另一個分支沒有的所有提交(commit),和分支之間不共享的提交(commit)的列表,另一個做法可以是:
(main)$ git log main ^feature/120-on-scroll --no-merges

十三、交互式 rebase(interactive rebase) 可能出現的問題

① rebase 編輯屏幕出現 noop

  • 如下所示:
noop
  • 這意味著 rebase 的分支和當前分支在同一個提交 commit上,或者領先 ahead 當前分支,可以嘗試:
    • 檢查確保主 (main) 分支沒有問題;
    • 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):
(main*)$ 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

十四、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

③ 暫存時記錄消息

  • 你可以在 list 時看到它:
$ git stash save <message>
  • 或:
$ git stash push -m <message>

④ 使用某個指定暫存

  • 首先可以查看你的 stash 記錄:
$ git stash list
  • 然后可以 apply 某個 stash:
$ git stash apply "stash@{n}"
  • 此處, ‘n’ 是 stash 在棧中的位置,最上層的 stash 會是 0,除此之外,也可以使用時間標記(假如能記得的話):
$ git stash apply "stash@{2.hours.ago}"

⑤ 暫存時保留未暫存的內容

  • 需要手動 create一個stash commit,然后使用 git stash store:
$ git stash create $ git stash store -m "commit-message" CREATED_SHA1

十五、雜項(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)

① 只想改變一個文件名字的大小寫,而不修改內容

(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)的用戶名和密碼

  • 你可能有一個倉庫需要授權,這時可以緩存用戶名和密碼,而不用每次推/拉(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。遺憾的是,這只對本地分支起作用,且它只跟蹤動作 (例如,不會跟蹤一個沒有被記錄的文件的任何改變)。
(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 分支簽出(checkout)到 2.2 分支,然后再簽回。那里,還有一個硬重置(hard reset)到一個較舊的提交,最新的動作出現在最上面以 HEAD@{0} 標識。
  • 如果事實證明你不小心回移(move back)了提交(commit),reflog 會包含不小心回移前 main 上指向的提交(0254ea7)。
$ git reset --hard 0254ea7
  • 然后使用 git reset 就可以把 main 改回到之前的 commit,這提供了一個在歷史被意外更改情況下的安全網。
與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Git之深入解析48个经典操作场景的分析和处理,专治不会合并代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。