如果某次改動比較大,牽涉到的文件比較多。但是某個(gè)文件本來是沒問題的,結(jié)果畫蛇添足,改出問題來了,這時(shí)候直接回退版本會把所有代碼都回退掉,可能得不償失。這時(shí)候就需要指定回退單個(gè)文件到為修改之前的版本。
- 首先先獲取需要回退的文件路徑,比較簡單的方法就是,在該文件加個(gè)細(xì)微改動,比如一條無關(guān)注釋,然后通過
git status
查看路徑
? git:(test) git status
On branch test02
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
# modified:后面的就是文件路徑
modified: packages/pages/src/table/index.vue
no changes added to commit (use "git add" and/or "git commit -a")
- 路徑獲取到后,就把剛才的改動回退掉:
git checkout -- .
? git:(test) ? git checkout -- .
? git:(test) git status
On branch test02
nothing to commit, working tree clean
- 然后再查看
commit
版本信息:git log
,后面的數(shù)字表示查看多少條記錄
? git:(test) git log -2
commit 555a38d888d4c3743491ddeb8a4235c4ec3cc49b (HEAD -> test)
Author: 流星
Date: Tue Feb 14 11:20:15 2023 +0800
chore: 登錄功能大改
commit 6f53d8d8d4d570082e580554b68d36707f50421b
Author: 流星
Date: Mon Feb 13 16:45:21 2023 +0800
feat: 添加登錄功能
(END)
-
由于我們是登錄功能大改那個(gè)出現(xiàn)的問題,所有我們需要把這個(gè)文件回退到添加登錄功能那一次文章來源:http://www.zghlxwxcb.cn/news/detail-509680.html
-
這里有2個(gè)東西是要提前復(fù)制好的:文章來源地址http://www.zghlxwxcb.cn/news/detail-509680.html
1. 需要回退的文件路徑:packages/pages/src/table/index.vue
2. 需要回退到哪的 commit ID:6f53d8d8d4d570082e580554b68d36707f50421b
- 然后執(zhí)行:
git checkout ID 路徑
? git:(test) git checkout 6f53d8d8d4d570082e580554b68d36707f50421b packages/pages/src/table/index.vue
Updated 1 path from 1ccf76171
- 此時(shí)代碼就會還原到登錄功能大改之前,如果不需要做啥修改,這時(shí)候出現(xiàn)
commit
即可
? git:(test) ? git status
On branch test02
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: packages/pages/src/table/index.vue
? git:(test) ? git commit -m 'chore: 還原某某文件'
→ No staged files match any configured task.
? Preparing lint-staged...
? Running tasks for staged files...
? Applying modifications from tasks...
? Cleaning up temporary files...
--------------------- git add ------------------
Nothing specified, nothing added.
hint: Maybe you wanted to say 'git add .'?
hint: Turn this message off by running
hint: "git config advice.addEmptyPathspec false"
---------------------- done --------------------
[lstest02 758a23df5] chore: 還原某某文件
1 file changed, 1 insertion(+), 1 deletion(-)
到了這里,關(guān)于【Git】回退單個(gè)文件到指定版本的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!