如何用另一个(上游)分支覆盖GIT分支中的所有文件



我在Github中有一个项目的分支。

我意外地将master分支中的本地更改提交并推送到origin/master(Github(。更改是通过自动脚本进行的,因此大约有180个文件已被更改,因此无法手动恢复更改并进行新的提交。

我找到了这个关于回滚到旧提交的教程。

我试过

git checkout master
git checkout <first commit-id> .
git pull
git commit -a -m "reverting back to older commit, so upstream can be cleanly merged"
git push
git fetch upstream
git merge upstream/master
git commit -a -m "updated with upstream/master"
git push

然而,这并不能用上游/主文件中的当前代码更新所有文件。所有文件都类似于<first commit-id>

如果我不得不猜测GIT认为提交比上游/主提交更新,那么这是正确的。但我不太了解GIT。

还有人建议我应该取消回购并重新分配项目。

但我猜GIT应该有一个选项,用旧版本或当前上游/主版本替换/覆盖整个(主(分支?

git reset --hard upstream/master

这会将当前分支重置为提交上游/主分支。

如果您想要分支上的新修订版(在现有版本的基础上(,并使文件内容看起来像旧修订版

git checkout the-old-id
git reset --soft the-branch
# all differences will be in index, branch is not touched
git commit -m "going back to revision X"
# if you like the result, move the branch
git branch -f the-branch
git checkout the-branch
# push it to remote

git read-tree也可以这样做,但我还没有尝试过,所以无法在它上面构建配方。

最新更新