git 經(jīng)典應(yīng)用場(chǎng)景

查看提交

如果你用git commit -a提交了一次變化(changes),而你又不確定到底這次提交了哪些內(nèi)容。你就可以用下面的命令顯示當(dāng)前HEAD上的最近一次的提交(commit):

(main)$ git show
或者
$ gitlog-n1 -p

修改提交信息

提交信息(commit message)寫(xiě)錯(cuò)了且這次提交(commit)還沒(méi)有推(push), 你可以通過(guò)下面的方法來(lái)修改提交信息(commit message):

$ git commit --amend --only  # 打開(kāi)默認(rèn)編輯器
或者
$ git commit --amend --only -m 'xxxxxxx' # 編輯信息一次完成

修改用戶名和郵箱

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

移除一個(gè)文件

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

刪除最后一次提交

刪除推了的提交(pushed commits),你可以使用下面的方法。這會(huì)不可逆的改變你的歷史,也會(huì)搞亂那些已經(jīng)從該倉(cāng)庫(kù)拉取(pulled)了的人的歷史。

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

沒(méi)有推到遠(yuǎn)程, 可以把Git重置(reset)到最后一次提交前的狀態(tài)(同時(shí)保存暫存的變化):

git reset --soft HEAD@{1}

刪除任意提交(commit)

警告:不到萬(wàn)不得已的時(shí)候不要這么做.

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

修改之前的提交需要強(qiáng)制推送

rebasing(見(jiàn)下面)和修正(amending)會(huì)用一個(gè)新的提交(commit)代替舊的, 如果之前已經(jīng)往遠(yuǎn)程倉(cāng)庫(kù)上推過(guò)一次修正前的提交(commit),就必須強(qiáng)推(force push) (-f)。
注意 – 確保指明一個(gè)分支!

 git push origin mybranch -f

做了一次重置,找回內(nèi)容

意外的做了 git reset --hard, 通常能找回自己的提交(commit), 因?yàn)镚it對(duì)每件事都會(huì)有日志,并且會(huì)保存幾天。

git reflog

顯示過(guò)去提交(commit)的列表,選擇需要回到的版本(commit)的SHA,重置一次:

git reset --hard SHA1234e

暫存

添加到上一次提交

git commit --amend

暫存一個(gè)文件的一部分:

git add --patch filename.x  #暫存文件的一部分
git add -N filename.x #添加新文件
git diff --cached # 顯示哪些行只是保存在本地

文件變化添加到兩個(gè)提交里

git add #會(huì)把整個(gè)文件加入到一個(gè)提交. 
git add -p #允許交互式的選擇你想要提交的部分.

暫存、未暫存變更

git commit -m "WP" #將所有的內(nèi)容變?yōu)槲磿捍?
git add .
git stash # 暫存你的未暫存的內(nèi)容并進(jìn)行stash
git reset HEAD^  #最后一個(gè)commit將原本暫存的內(nèi)容變?yōu)槲磿捍?,最后stash pop回來(lái)
git stash pop --index 0

注意1: 使用pop僅僅是因?yàn)橄氡M可能保持冪等。
注意2: 假如不加上--index會(huì)把暫存的文件標(biāo)記為存儲(chǔ)。

未暫存

移動(dòng)到新分支

git checkout -b my-newbranch

移動(dòng)到已存在分支

 git stash
git checkout my-branch
git stash pop

丟棄未提交的變化

重置源(origin)和你本地(local)之間的一些提交(commit):

# one commit
git reset --hard HEAD^
# two commits
git reset --hard HEAD^^
# four commits
git reset --hard HEAD~4
# or
git checkout -f

重置某個(gè)特殊的文件

git reset filename

丟棄未暫存的內(nèi)容

git checkout -p
或者
git stash -p
git reset --hard
git stash pop
或
git stash -p
git stash drop

分支

錯(cuò)誤拉取處理

git reflog # 查詢head指向
git reset --hard c5vb5a # 重置分支

刪除本地提交,回復(fù)本地分支和遠(yuǎn)程分支一致

# 顯示提前(ahead)源(origin)多少個(gè)提交
git status
# resest
git reset --hard origin/my-branch

創(chuàng)建新分支

git branch my-branch # 創(chuàng)建新分支
git checkout my-branch # 簽出新分支工作

提交到不同分支

git log # 查看提交日志
# 重置正確分支
git reset --hard aqwe8re
# 創(chuàng)建bug修復(fù)分支
git checkout -b bug21
# 提交修復(fù)
git cherry-pick edf52e8

刪除遠(yuǎn)程倉(cāng)庫(kù)已經(jīng)刪除的分支

git fetch -p

恢復(fù)刪除的分支

git reflog
git checkout -b my-branch-del

刪除一個(gè)遠(yuǎn)程分支

git push origin --delete my-branch
# OR
git push origin :my-branch

刪除本地分支

git branch -D my-branch

拉取遠(yuǎn)端分支

# 拉取遠(yuǎn)端所有分支
git fetch --all
# 遠(yuǎn)程my-branch分支簽出到本地my-branch
git checkout --track origin/my-branch

rebasing 和 merging

撤銷(xiāo)rebase/merge

git reset --hard ORIG_HEAD

已經(jīng)rebase,不進(jìn)行強(qiáng)推

git checkout my-branch
git rebase -i main
git checkout main
git merge --ff-only my-branch

安全合并

--no-commit 執(zhí)行合并(merge)但不自動(dòng)提交, 做提交前檢查和修改的機(jī)會(huì)。
no-ff 為特性分支(feature branch)的存在過(guò)留下記錄, 保持項(xiàng)目歷史一致

 git merge --no-ff --no-commit my-branch

分支合并成一個(gè)提交

git merge --squash my-branch

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

git rebase -i @{u}

檢查分支上的所有提交(commit)都合并(merge)過(guò)

git log --graph --left-right --cherry-pick --oneline HEAD...feature/dev-scroll
# OR 
git log main ^feature/dev-scroll --no-merges

rebase 問(wèn)題

編輯屏幕出現(xiàn)'noop'
意味著你rebase的分支和當(dāng)前分支在同一個(gè)提交(commit)上, 或者 領(lǐng)先(ahead) 當(dāng)前分支。你可以嘗試:

檢查確保主(main)分支沒(méi)有問(wèn)題
rebase HEAD~2 或者更早

解決沖突

1、查詢沖突:
git status
2、差異查看
git mergetool -t opendiff
3、修改變化文件
git add changed.xx
git rebase --continue
#任何時(shí)候想結(jié)束整個(gè)rebase 過(guò)程,回來(lái)rebase前的分支狀態(tài):
git rebase --abort

Stash

暫存所有改動(dòng)

git stash
git stash -u # -u來(lái)排除一些文件

暫存指定文件

# 暫存一個(gè)文件
git stash push  path/filename.x
# OR 暫存多個(gè)文件
git stash push  path/filename.x path/filename2.x

暫存記錄

git stash save <message>
# OR
git stash push -m <message>

使用某個(gè)指定暫存

# 查看你的stash記錄
git stash list
# apply某個(gè)stash
git stash apply "stash@{n}"
# or
git stash apply "stash@{2.hours.ago}"

保留未暫存的內(nèi)容

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

克隆所有子模塊

git clone --recursive git://github.com/xxx/xxx.git
git submodule update --init --recursive

刪除標(biāo)簽(tag)

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

恢復(fù)已刪除標(biāo)簽(tag)

# 找到無(wú)法訪問(wèn)的標(biāo)簽
git fsck --unreachable | grep tag
git update-ref refs/tags/<tag_name> <hash>

文件

修改文件名

git mv --force myfile MyFile

Git刪除一個(gè)文件,但保留該文件

git rm --cached log.txt

配置

緩存一個(gè)倉(cāng)庫(kù)(repository)的用戶名和密碼

git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容