如何在 git 中删除合并结果



我来自一个多变的背景,我可以更新到某个提交,这就是我的代码完全一样的地方,任何更改都会被删除。我正在尝试在 git 中做同样的事情但没有成功,这是我所做的:

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://naguibihab@github.com':
Naguib@Naguib MINGW64 /d/.Net omitted/omitted (staging)
$ git merge preprod
Already up-to-date.

在这一点上,我意识到我想以相反的方式合并,检查预生产并将暂存合并到其中,但首先我想确保预生产工作正常。所以我:

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://naguibihab@github.com':
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://naguibihab@github.com':
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 保持同步,在该远程分支中具有完全相同的代码。我该怎么做?

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

git checkout preprod
git reset --hard origin/preprod

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

您的本地分支在 origin/preprod 之前,因此有两个不同的方向,具体取决于您所说的与远程分支"完全相同的代码"的含义。您处于领先于远程分支的状态(ahead by X commits消息在git status中可见)。您有两种主要途径(以及我不会探索的大量子选项):

  1. 更新远程以赶上您的本地分支机构

    git push origin preprod

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

    git 重置 --硬头~68

其中 68 是您在远程分支之前提交的次数。注意:这将删除这些提交!将它们添加到另一个具有git checkout -b backup的分支,或者如果您现在想将它们保存到一侧,请运行git stash

无论哪种方式,此时您都将与上游保持一致。

最新更新