如何在git

时间:2016-01-28 03:33:18

标签: 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.

此时我意识到我想要合并另一种方式,检查preprod并将staging合并到其中,但首先我想确保preprod工作正常。 所以我:

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保持同步,以便在该远程分支中拥有完全相同的代码。我该怎么做?

2 个答案:

答案 0 :(得分:2)

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

git checkout preprod
git reset --hard origin/preprod

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

答案 1 :(得分:1)

您的本地分支位于origin / preprod之前,因此根据您与远程分支“完全相同的代码”的含义,有两个不同的方向。您处于远离分支的状态(git status消息在ahead by X commits中可见)。你有两个主要途径(我将不会探讨大量的子选项):

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

    git push origin preprod

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

    git reset --hard HEAD~68

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

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