[Git] Roll back a single file to the specified version

Article directory

If a certain change is relatively large, more files are involved. However, a certain file was originally fine, but it turned out to be superfluous, and a problem occurred. At this time, directly rolling back the version will roll back all the codes, which may not be worth the candle. At this time, you need to specify to roll back a single file to the version before it was modified.

  • First, get the file path that needs to be rolled back. The simpler method is to add a small change to the file, such as an irrelevant comment, and then view the git statuspath
➜  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")
  • After the path is obtained, roll back the changes just now:git checkout -- .
➜ git:(test)git checkout -- .
➜ git:(test) git status
On branch test02
nothing to commit, working tree clean
  • Then check committhe version information: git log, the number behind indicates how many records to check
➜ 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)
  • Since we have a problem with the major change of the login function , we need to roll back this file to the time when the login function was added

  • Here are 2 things to copy in advance:

1. 需要回退的文件路径:packages/pages/src/table/index.vue
2. 需要回退到哪的 commit ID:6f53d8d8d4d570082e580554b68d36707f50421b
  • Then execute:git checkout ID 路径
➜  git:(test) git checkout 6f53d8d8d4d570082e580554b68d36707f50421b packages/pages/src/table/index.vue
Updated 1 path from 1ccf76171
  • At this time, the code will be restored to before the major change of the login function . If there is no need to make any changes, it will appear at this time 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(-)

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/129023739