Git -如何将整个目录恢复到特定的提交(删除任何添加的文件)

8cdiaqws  于 12个月前  发布在  Git
关注(0)|答案(5)|浏览(86)

我想在git中恢复一个目录-恢复里面的所有文件,以及删除自提交以来添加的任何文件。 checkout 似乎只满足了我的第一个要求,但不会删除任何文件。

nhaq1z21

nhaq1z211#

我想出了最简单的解决办法。

git rm /path/to/dir
git checkout <rev> /path/to/dir
git commit -m "reverting directory"

然后删除任何未跟踪的文件。

git rm

从工作树和索引中删除文件https://git-scm.com/docs/git-rm

git checkout

更新工作树中的文件,以匹配索引或指定树中的版本。https://www.git-scm.com/docs/git-checkout

git commit

将更改记录到存储库https://www.git-scm.com/docs/git-commit

wmtdaxz3

wmtdaxz32#

仅删除git上的文件夹及其内容

git rm -r --cached myFolder

在git和本地删除文件夹

git rm -r myFolder

然后提交并再次推送

恢复到上一次提交

#reset to previous commit, replace with your commit hash code, you can find it from your commit history 
git reset {commit hash} 

#moves pointer back to previous head branch
git reset --soft HEAD@{1}

git commit -m "Reverted commit to blah"

#update your working copy
git reset --hard

恢复到提交的一部分在这种情况下,您需要恢复到特定的提交并添加补丁

#reset to previous commit, but don't commit the changes
$ git revert --no-commit {last commit hash}   

# unstage the changes
$ git reset HEAD .             

# add/remove stuff here
$ git add file
$ git rm -r myfolder/somefiles          

# commit the changes  
$ git commit -m "fixed something"

# check the files
$ git status

#discard unwanted changes
$ git reset --hard
9avjhtql

9avjhtql3#

第一个答案很好,但我想简化一下。

git rm -r /path/to/dir
git checkout <rev> /path/to/dir       <-- the <rev> is the git commit id
git commit -m "reverting directory"

一切都是完全相同的,除了在我的例子中的checkout命令是:
git checkout 70157c4f57aa21307cd96f146f8e98b808e1aada ./WebRoot
在命令行中,我在一个名为Memento的目录中,我用一个点表示git commit信息将进入的新目录将在Memento文件夹中。
我在检查文件夹里的提交。

d4so4syb

d4so4syb4#

恢复到与
首先从集结地用核武器摧毁目录

git rm --cached -r <directory>

现在读取该目录的treeish

git read-tree <old SHA>^{tree}:<directory> --prefix=<directory>

然后提交

git commit -m "reverting <directory>"

然后抛出剩余的工作目录更改。

ffx8fchx

ffx8fchx5#

我认为你可以一步完成,

git checkout --no-overlay <ref> -- dir/to/revert

--no-overlay https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt---no-overlay将删除不在参考文件中的文件

相关问题