我在远程存储库中有一个项目,与本地存储库(开发)和服务器存储库(prod)同步。我一直在做一些已经推送到远程和从服务器上拉出来的承诺更改。现在,我想撤销这些更改。因此,我可以在更改之前签出到提交,并提交新更改,但我猜再次将它们推到远程会有问题。我该怎么做,有什么建议吗?
当前回答
这将删除您推送的提交
git reset --hard 'xxxxx'
git clean -f -d
git push -f
做个备份以防万一
评论中出现了两个注释:
'xxxxx'是你想保留的最后一次提交的哈希码。 清洁是危险的。它删除文件,不受版本控制!
其他回答
干净利落地做:
git rebase -i <hash of last good commit, 9 chars is enough>
现在,您将得到一个从上一次提交到HEAD的提交列表,以及对每个提交做什么的选项。DROP将丢弃该提交。保存文件。
现在修复上游做:
git push --force-with-lease
(使用租赁,这样你就不会意外地给其他人带来问题)
通过删除错误的提交,而不是引入新的提交来修复先前的错误提交,从而保持日志干净。
简单方法:
git reset <commit_hash>
(您希望保留的最后一个提交的哈希值)。
您将在本地保留当前未提交的更改。
如果你想再推一次,你必须做到:
git push -f
在这些情况下,我所做的是:
在服务器中,将光标移回上一次已知的正确提交: Git push -f origin <last_known_good_commit>:<branch_name> 在本地,执行同样的操作: Git重置——hard <last_known_good_commit> # ^^^^^^ # 可选
查看我为此目的创建的分支my_new_branch的完整示例:
$ git branch
my_new_branch
这是在myfile.py中添加了一些东西后的最近历史:
$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date: Wed Mar 23 12:48:03 2016 +0100
Adding new stuff in myfile.py
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
我想摆脱上一个提交,它已经被推送了,所以我运行:
$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
+ 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)
好了!现在我看到提交时更改的文件(myfile.py)显示为“not staging for commit”:
$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.py
no changes added to commit (use "git add" and/or "git commit -a")
因为我不想要这些改变,我只是把光标移回本地:
$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit
所以现在HEAD在之前的提交中,在本地和远程:
$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
你可以通过以下方法恢复单个提交:
git revert <commit_hash>
这将创建一个新的提交,该提交将恢复您指定的提交的更改。请注意,它只恢复特定的提交,而不恢复之后的提交。如果你想要还原一系列的提交,你可以这样做:
git revert <oldest_commit_hash>..<latest_commit_hash>
它将还原<oldest_commit_hash>之后直到<latest_commit_hash>的所有提交。在某些版本的git上,它还会恢复<oldest_commit_hash>,因此请仔细检查该提交是否被恢复。您总是可以使用g reset—hard HEAD~来删除最新的恢复提交(它恢复了最早的提交)。
要找出提交的哈希值,可以使用git log。
有关git revert命令的更多信息,请参阅git-revert手册页。此外,查看这个答案,了解关于恢复提交的更多信息。
你可以这样做
git push origin +<short_commit_sha>^:<branch_name>
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别