推到原点/主人。如何还原

时间:2018-01-18 14:44:42

标签: git github

主人是GitHub的受保护分支,所以我认为这可能永远不会发生,但我错了。在github上没有恢复请求。

所以我认为不是从本地大师那里分支出来,从origin / master分支会给我一个步骤。分支为origin / master的分支称为feature-x

我做了两次提交(按顺序为SHA 718016d和8ab1912)。然后我做了一个git pull && git push,它似乎将origin / master合并到我的分支中并推入origin / master。从我分支开始直到我做了git pull时,大约有40个提交差异。

如何从源/主历史中完全删除2个提交?我读到删除已公开的提交是一个非常糟糕的想法,但这是我理想的解决方案。如果有人能够建议我如何以安全的方式做到这一点。

如果应该避免这种技术;

如何在本地恢复这些提交并推回原点/主控?我调查了git revert,但我想确定在我再次搞砸之前我知道到底发生了什么

添加了日志:

git pub是一个别名

pub = "!f() { git push -u ${1:-origin} `git symbolic-ref HEAD`; }; f"

C:\source\ [feature-x ↓1 ↑1 +6 ~8 -0 !]> git add -A; git commit -m "wip"
[feature-x 718016d8a] wip
 14 files changed, 358 insertions(+), 7 deletions(-)
------Lines removed for anonomity-------
C:\source\ [feature-x ↓1 ↑2]> git pub
To https://github.com/*********
 ! [rejected]            feature-x -> master (fetch first)
error: failed to push some refs to 'https://github.com/********.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
C:\source\ [feature-x ↓1 ↑2]> git pull
remote: Counting objects: 331, done.
remote: Compressing objects: 100% (118/118), done.
Receiving objects: 100% (331/331), 88.21 KiB | 0 bytes/s, done.sed 0Receiving objects:  27% (90/331)

Resolving deltas: 100% (254/254), completed with 78 local objects.
From https://github.com/********
------Lines removed for anonomity-------
Merge made by the 'recursive' strategy.
------Lines removed for anonomity-------
 27 files changed, 190 insertions(+), 44 deletions(-)
------Lines removed for anonomity-------
C:\source\ [feature-x ↑3]> git pub
Counting objects: 54, done.
Delta compression using up to 6 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (54/54), 7.41 KiB | 0 bytes/s, done.
Total 54 (delta 42), reused 0 (delta 0)
remote: Resolving deltas: 100% (42/42), completed with 28 local objects.
Branch feature-x set up to track remote branch master from origin.
To https://github.com/*********

1 个答案:

答案 0 :(得分:1)

有两种方法可以从git中恢复提交:

1.使用git rebase -i HEAD~<number_of_commits_from_top_u_want_to_modify>完全从历史记录中删除它。这将打开一个窗口,并列出提交。在这里,只需删除包含不需要的提交的行。然后保存并退出。但正如你所建议的,这是一个坏主意,因为它修改了git历史记录,它将要求你强制推动分支(因为这不是一个附加的改变)。但是,如果你这样做,它看起来就像 - 如果这两个提交从来没有。

2.使用git revert <commit_id>。这是您可以选择的最安全的选项。这实际上并没有删除任何提交。这只是添加一个新的提交,完全撤消提交的内容并创建一个新的提交。这不会影响历史。这是一个附加变化因此,它不需要强制推动。