如何在git中删除合并结果


How to remove merge results in git

本问题已经有最佳答案,请猛点这里访问。

我来自一个反复无常的背景,在那里我可以更新到一个特定的提交,这就是我的代码完全一样的地方,任何更改都会被删除。我试图在Git中做同样的事情,但没有成功,我做了以下事情:

1
2
3
4
5
6
7
8
9
10
11
12
Naguib@Naguib MINGW64 /d/.Net omitted/omitted (deploy)
$ git checkout staging
Switched to branch 'staging'
Your branch is up-to-date with 'origin/staging'.

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (staging)
$ git fetch
Password for 'https://[email protected]':

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (staging)
$ git merge preprod
Already up-to-date.

在这一点上,我意识到我想通过另一种方式进行合并,检查预处理并将分段合并到其中,但首先我想确保预处理工作正常。所以我:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Naguib@Naguib MINGW64 /d/.Net omitted/omitted (staging)
$ git checkout preprod
Switched to branch 'preprod'
Your branch is ahead of 'origin/preprod' by 68 commits.
  (use"git push" to publish your local commits)

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git fetch
Password for 'https://[email protected]':
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/BlueChilli/omitted
   55d05f4..2381261  staging    -> origin/staging

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git status
On branch preprod
Your branch is ahead of 'origin/preprod' by 68 commits.
  (use"git push" to publish your local commits)
nothing to commit, working directory clean

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git pull
Password for 'https://[email protected]':
Already up-to-date.

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git status
On branch preprod
Your branch is ahead of 'origin/preprod' by 68 commits.
  (use"git push" to publish your local commits)
nothing to commit, working directory clean

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git reset --hard
HEAD is now at 55d05f4 merge from staff_filter

Naguib@Naguib MINGW64 /d/.Net omitted/omitted (preprod)
$ git status
On branch preprod
Your branch is ahead of 'origin/preprod' by 68 commits.
  (use"git push" to publish your local commits)
nothing to commit, working directory clean

我希望预处理与原始/预处理保持最新,以便在该远程分支中具有完全相同的代码。我该怎么做?


要将分支preprod设置为origin/preprod中的分支,只需执行以下操作:

1
2
git checkout preprod
git reset --hard origin/preprod

注意:这将导致您丢失当前在这两者之间的68个提交。


您的本地分支在origin/pread之前,所以根据您所说的与远程分支"完全相同的代码"的含义,有两个不同的方向。您处于远程分支之前的状态(通过ahead by X commits消息在git status中可见)。你有两条主要途径(还有很多我不想探讨的子途径):

  • 更新remote以赶上本地分支

    Git推送原点预处理

  • 重置本地分支以恢复到远程分支

    Git重置——硬头~68

  • 其中68是在远程分支之前的提交数。注意:这将删除这些提交!如果你现在想把它们保存到一边,就把它们添加到另一个使用git checkout -b backup的分支或者运行git stash

    不管怎样,你都会在这一点上与上游保持一致。