[Git] Revertir un solo archivo a la versión especificada

Directorio de artículos

Si un determinado cambio es relativamente grande, se involucran más archivos. Sin embargo, cierto archivo originalmente estaba bien, pero resultó ser superfluo y ocurrió un problema. En este momento, revertir directamente la versión revertirá todos los códigos, lo que puede no valer la pena. En este momento, debe especificar la reversión de un solo archivo a la versión anterior a la modificación.

  • Primero, obtenga la ruta del archivo que necesita revertirse. El método más simple es agregar un pequeño cambio al archivo, como un comentario irrelevante, y luego ver la git statusruta.
➜  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")
  • Después de obtener la ruta, deshaga los cambios justo ahora:git checkout -- .
➜ git:(test)git checkout -- .
➜ git:(test) git status
On branch test02
nothing to commit, working tree clean
  • Luego verifique commitla información de la versión: git log, el número detrás indica cuántos registros verificar
➜ 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)
  • Dado que tenemos un problema con el cambio principal de la función de inicio de sesión , debemos revertir este archivo al momento en que se agregó la función de inicio de sesión.

  • Aquí hay 2 cosas para copiar por adelantado:

1. 需要回退的文件路径:packages/pages/src/table/index.vue
2. 需要回退到哪的 commit ID:6f53d8d8d4d570082e580554b68d36707f50421b
  • Luego ejecuta:git checkout ID 路径
➜  git:(test) git checkout 6f53d8d8d4d570082e580554b68d36707f50421b packages/pages/src/table/index.vue
Updated 1 path from 1ccf76171
  • En este momento, el código se restaurará antes del cambio principal de la función de inicio de sesión . Si no es necesario realizar ningún cambio, aparecerá en este momento 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(-)

Supongo que te gusta

Origin blog.csdn.net/qq_45677671/article/details/129023739
Recomendado
Clasificación