背景
rebase 使用
1、 切換到子分支,rebase 主分支到子分支
git rebase master(主分支name)
2、如果有沖突,解決完沖突使用
git add .
git rebase --continue
3、rebase 成功后,會(huì)提示 落后多少版本 ,最后強(qiáng)制推送到遠(yuǎn)程
git push -f origin 當(dāng)前分支name
強(qiáng)制推送到遠(yuǎn)端就可以了。
git 子模塊使用:
如果未對(duì)子模塊進(jìn)行更改,但是在pull 代碼的時(shí)候,顯示出子模塊的地址,使用命令更新一下子模塊
// 更新到初始化版本
git submodule update --init
或
// 更新到最新版本
git submodule update --remote
git revert 回滾
回滾到歷史某一次提交,新開子分支,以防數(shù)據(jù)丟失
git revert -n [commitId]
手動(dòng)解決可能出現(xiàn)的沖突
git commit -m 'commit info'
git push
開發(fā)過(guò)程中,本地通常會(huì)有無(wú)數(shù)次 commit ,可以合并“相同功能”的多個(gè) commit,以保持歷史的簡(jiǎn)潔。
git rebase 合并多條 commit
從HEAD版本開始往過(guò)去數(shù)3個(gè)版本
$ git rebase -i HEAD~3
指定要 rebase 節(jié)點(diǎn)的 commitId;
如果子分支多條commit 要合并到 一條 commit , 要用子分支的根節(jié)點(diǎn) commitId
$ git rebase -i [commitid]
說(shuō)明:
-i(--interactive):彈出交互式的界面進(jìn)行編輯合并
[commitid]:要合并多個(gè)版本之前的版本號(hào),注意:[commitid] 本身不參與合并
指令解釋(交互編輯時(shí)使用):
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
d, drop = remove commit
合并步驟
查看 log 記錄,使用git rebase -i選擇要合并的 commit
編輯要合并的版本信息,保存提交,多條合并會(huì)出現(xiàn)多次(可能會(huì)出現(xiàn)沖突)
修改注釋信息后,保存提交,多條合并會(huì)出現(xiàn)多次
推送遠(yuǎn)程倉(cāng)庫(kù)或合并到主干分支
查看 log
291e427 update website
8c8f3f4 update website
1693a6f update clear-logs.sh version
3759b84 update clear-logs.sh
fc36a2a add links
1d795e6 fix && update clear-logs.sh 0.0.2
9536dab add dingtalk script
3a51aaa fix shellcheck problem
2db6ad3 add clear logs scripts
e57b0e6 fix && add batch del
17cb931 fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit```
編輯要合并版本
```# 指定要合并版本號(hào),cf7e875 不參與合并,進(jìn)入 vi 編輯器
$ git rebase -i cf7e875
pick 17cb931 fix && add batch del
pick e57b0e6 fix && add batch del
pick 2db6ad3 add clear logs scripts
pick 3a51aaa fix shellcheck problem
pick 9536dab add dingtalk script
pick 1d795e6 fix && update clear-logs.sh 0.0.2
pick fc36a2a add links
pick 3759b84 update clear-logs.sh
pick 1693a6f update clear-logs.sh version
pick 8c8f3f4 update website
# Rebase cf7e875..291e427 onto cf7e875 (10 commands)
#
# 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
# d, drop = remove commit
#
# 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
將 commit 內(nèi)容編輯如下:
# 把 e57b0e6 合并到 17cb931,不保留注釋;把 1693a6f 合并到 3759b84
pick 17cb931 fix && add batch del
f e57b0e6 fix && add batch del
pick 2db6ad3 add clear logs scripts
pick 3a51aaa fix shellcheck problem
pick 9536dab add dingtalk script
pick 1d795e6 fix && update clear-logs.sh 0.0.2
pick fc36a2a add links
pick 3759b84 update clear-logs.sh
s 1693a6f update clear-logs.sh version
pick 8c8f3f4 update website
然后:wq保存退出后是注釋界面:
# This is a combination of 2 commits.
# This is the 1st commit message:
update clear-logs.sh
# This is the commit message #2:
update clear-logs.sh version
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Jul 28 18:25:57 2020 +0800
#
# interactive rebase in progress; onto cf7e875
# Last commands done (9 commands done):
# pick 3759b84 update clear-logs.sh
# s 1693a6f update clear-logs.sh version
# Next command to do (1 remaining command):
# pick 8c8f3f4 update website
# You are currently editing a commit while rebasing branch 'master' on 'cf7e875'.
#
# Changes to be committed:
# modified: logs/README.md
# modified: logs/clear-logs.sh
#
編輯注釋信息,保存退出:wq即可完成 commit 的合并:
update clear-logs.sh
查看合并后的 log
$ git log --oneline
47e7751 update website
4c2316c update clear-logs.sh
73f082e add links
56adcf2 fix && update clear-logs.sh 0.0.2
ebf3786 add dingtalk script
6e81ea7 fix shellcheck problem
64ca58f add clear logs scripts
9327def fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit
推送遠(yuǎn)程
# 由于我是一個(gè)人開發(fā),故強(qiáng)制推送到遠(yuǎn)程倉(cāng)庫(kù)
$ git push --force origin master
沖突解決
在 git rebase 過(guò)程中,可能會(huì)存在沖突,此時(shí)就需要解決沖突。
錯(cuò)誤提示信息:git rebase -i resumeerror: could not apply ...。
查看沖突
$ git status
解決沖突之后,本地提交
$ git add .
rebase 繼續(xù)
$ git rebase --continue
rebase
切換到要 rebase 的分支
例: 將 master 主分支 rebase 到 dev子分支
切換到 dev 子分支 ,右擊 master ,選中"將當(dāng)前變更變基到master"
git rebase 誤操作導(dǎo)致文件丟失撤銷并恢復(fù)文件
第一步
執(zhí)行 git reflog
查看本地操作記錄
找到本次rebase之前的操作id 例如:89356d0
第二步 執(zhí)行恢復(fù)命令
git reset --hard 89356d0
有出現(xiàn)提示的話,則輸入y確認(rèn)
忽略文件無(wú)效
在Sourcetree 的偏好設(shè)置中,加入git 的忽略文件沒有效果
設(shè)置項(xiàng)目的忽略文件如下
打開Sourcetree中的項(xiàng)目,點(diǎn)擊右上角的 設(shè)置
按鈕
在 高級(jí)
中編輯忽略文件
點(diǎn)擊確定
完成
打開項(xiàng)目終端 , 輸入以下指令即可
git add .
git commit -m "refresh .gitignore file"
如果忽略文件失效,則需要先清除緩存
git rm -r .cached .
//然后再輸入以下指令
git add .
git commit -m "refresh .gitignore file"