git恢复指定文件到某一个commit节点

git恢复指定文件到某一个commit节点

git checkout 126d385629a956f92ee58c03ad4651aecf47a855 -- webAPI/src/app/testCost.php
恢复文件到指定commitid的状态

在 Git 中,git revert 通常用于撤销一个提交并生成一个新的提交。如果你希望撤销某个文件在特定提交中的更改,可以通过以下步骤操作:

方法:使用 git revert 仅对单个文件
查看历史提交:
首先,使用 git log 命令查看历史提交,找到你要撤销的提交的哈希值。

bash
git log
输出示例:

commit 1234567890abcdef (HEAD -> master)
Author: Your Name <you@example.com>
Date: Mon Feb 18 15:02:00 2025 +0000

Some commit message
撤销某个文件的更改:
使用 git checkout 命令将该文件恢复到某个历史提交时的版本。例如,如果你要撤销 file.txt 在提交 1234567 中的更改,可以使用如下命令:

bash
git checkout 1234567 -- file.txt
这会将 file.txt 恢复为提交 1234567 中的版本。

提交更改:
现在,文件已经恢复到旧的状态,接下来将这些更改提交到版本库:

bash
git add file.txt
git commit -m "Revert changes in file.txt from commit 1234567"
推送更改:
最后,如果你已经将本地修改推送到远程仓库,使用 git push 将更新推送到远程:

bash
git push
总结:
使用 git revert 通常是用来撤销整个提交,但如果你只想撤销某个文件的更改,可以通过 git checkout <commit_hash> -- <file> 恢复文件到指定提交的状态,并进行提交。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: