git分支操作技巧

我從錯(cuò)誤的分支拉取了內(nèi)容,或把內(nèi)容拉取到了錯(cuò)誤的分支。
這是另外一種使用 git reflog 情況,找到在這次錯(cuò)誤拉(pull)之前 HEAD 的指向。
(main)$ git reflogab7555f HEAD@{0}: pull origin wrong-branch: Fast-forwardc5bc55a HEAD@{1}: checkout: checkout message goes here
重置分支到你所需的提交(desired commit):
$ git reset --hard c5bc55a完成。
我想扔掉本地的提交(commit),以便我的分支與遠(yuǎn)程的保持一致。
先確認(rèn)你沒有推(push)你的內(nèi)容到遠(yuǎn)程。
git status 會(huì)顯示你領(lǐng)先(ahead)源(origin)多少個(gè)提交:
git statusOn branch my-branchYour 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我需要提交到一個(gè)新分支,但錯(cuò)誤的提交到了main分支 。
在 main下創(chuàng)建一個(gè)新分支,不切換到新分支,仍在 main 下:
(main)$ git branch my-branch把main分支重置到前一個(gè)提交:
(main)$ git reset --hard HEAD^HEAD^ 是 HEAD^1 的簡寫,你可以通過指定要設(shè)置的 HEAD 來進(jìn)一步重置。
或者, 如果你不想使用 HEAD^, 找到你想重置到的提交(commit)的 hash(git log 能夠完成), 然后重置到這個(gè) hash。使用 git push 同步內(nèi)容到遠(yuǎn)程。
例如, main 分支想重置到的提交的 hash 為a13b85e:
(main)$ git reset --hard a13b85eHEAD is now at a13b85e
切換(checkout)到剛才新建的分支繼續(xù)工作:
(main)$ git checkout my-branch我把幾個(gè)提交(commit)提交到了同一個(gè)分支,而這些提交應(yīng)該分布在不同的分支里。
假設(shè)你有一個(gè)main分支, 執(zhí)行g(shù)it log, 你看到你做過兩次提交:
git logcommit e3851e817c451cc36f2e6f3049db528415e3c114Author: Alex LeeDate: Tue Jul 22 15:39:27 2014 -0400Bug #21 - Added CSRF protectioncommit 5ea51731d150f7ddc4a365437931cd8be3bf3131Author: Alex LeeDate: Tue Jul 22 15:39:12 2014 -0400Bug #14 - Fixed spacing on titlecommit a13b85e984171c6e2a1729bb061994525f626d14Author: Aki RoseDate: Tue Jul 21 01:12:48 2014 -0400First commit
讓我們用提交 hash(commit hash) 標(biāo)記 bug (e3851e8 for #21, 5ea5173 for #14).
首先, 我們把 main 分支重置到正確的提交 (a13b85e):
(main)$ git reset --hard a13b85eHEAD is now at a13b85e
現(xiàn)在, 我們對(duì) bug #21 創(chuàng)建一個(gè)新的分支:
(main)$ git checkout -b 21(21)$
接著, 我們用 cherry-pick 把對(duì) bug #21 的提交放入當(dāng)前分支。這意味著我們將應(yīng)用 (apply) 這個(gè)提交 (commit),僅僅這一個(gè)提交 (commit),直接在 HEAD 上面。
(21)$ git cherry-pick e3851e8這時(shí)候, 這里可能會(huì)產(chǎn)生沖突, 我們需要解決沖突.
然后, 我們?yōu)?bug #14 創(chuàng)建一個(gè)新的分支, 也基于 main 分支。
(21)$ git checkout main(main)$ git checkout -b 14(14)$
最后, 為 bug #14 執(zhí)行 cherry-pick:
(14)$ git cherry-pick 5ea5173我不小心刪除了我的分支
如果你定期推送到遠(yuǎn)程, 多數(shù)情況下應(yīng)該是安全的,但有些時(shí)候還是可能刪除了還沒有推到遠(yuǎn)程的分支。
讓我們先創(chuàng)建一個(gè)分支和一個(gè)新的文件:
git checkout -b my-branchgit branchtouch foo.txtlsREADME.md foo.txt
添加文件并做一次提交:
(my-branch)$ git add .(my-branch)$ git commit -m 'foo.txt added'(my-branch)$ foo.txt added1 files changed, 1 insertions(+)create mode 100644 foo.txt(my-branch)$ git logcommit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012Author: siemiatjDate: Wed Jul 30 00:34:10 2014 +0200foo.txt addedcommit 69204cdf0acbab201619d95ad8295928e7f411d5Author: Kate HudsonDate: Tue Jul 29 13:14:46 2014 -0400Fixes #6: Force pushing after amending commits
現(xiàn)在我們切回到主(main)分支,‘不小心的’刪除 my-branch 分支:
(my-branch)$ git checkout mainSwitched to branch 'main'Your branch is up-to-date with 'origin/main'.(main)$ git branch -D my-branchDeleted branch my-branch (was 4e3cd85).(main)$ echo oh noes, deleted my branch!oh noes, deleted my branch!
在這時(shí)候你應(yīng)該想起了reflog, 一個(gè)升級(jí)版的日志,它存儲(chǔ)了倉庫(repo)里面所有動(dòng)作的歷史。
(main)$ git reflog69204cd HEAD@{0}: checkout: moving from my-branch to main4e3cd85 HEAD@{1}: commit: foo.txt added69204cd HEAD@{2}: checkout: moving from main to my-branch
正如你所見,我們有一個(gè)來自刪除分支的提交 hash(commit hash),接下來看看是否能恢復(fù)刪除了的分支。
(main)$ git checkout -b my-branch-helpSwitched to a new branch 'my-branch-help'(my-branch-help)$ git reset --hard 4e3cd85HEAD is now at 4e3cd85 foo.txt added(my-branch-help)$ lsREADME.md foo.txt
看! 我們把刪除的文件找回來了。Git 的 reflog 在 rebasing 出錯(cuò)的時(shí)候也是同樣有用的。
我想刪除一個(gè)分支
刪除一個(gè)遠(yuǎn)程分支:
(main)$ git push origin --delete my-branch你也可以:
(main)$ git push origin :my-branch刪除一個(gè)本地分支:
git branch -D my-branch