3

I'm from a mercurial background where I can just update to a certain commit and that's where my code would be exactly like, any changes would be removed. I'm trying to do the same thing in git but without success, here's what I did:

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.

At this point I realised I wanted to merge the other way around, checking out preprod and merging staging into it, but first I wanted to make sure that preprod is working fine. so I:

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

I want to have preprod to be up-to-date with origin/preprod, to have the exact same code in that remote branch. How do I do that?

Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80

2 Answers2

3

To set branch preprod equal to that in origin/preprod, simply do:

git checkout preprod
git reset --hard origin/preprod

NB: This will cause you to lose the 68 commits that you currently have between the two.

houtanb
  • 3,852
  • 20
  • 21
1

Your local branch is ahead of origin/preprod, so there are two different directions depending on what you mean by "exactly the same code" as the remote branch. Your in a state where you are ahead of the remote branch (which is visible in git status by the ahead by X commits message). You have two main pathways (and a ton of sub-options I won't explore):

  1. Update remote to catch up to your local branch

    git push origin preprod

  2. Reset your local branch to revert back to your remote branch

    git reset --hard HEAD~68

Where 68 is the number of commits you're ahead of your remote branch. NOTE: this will delete these commits! Add them to another branch with git checkout -b backup or run git stash if you want to save them to the side for now.

Either way, you'll be aligned with upstream at this point.

mbb
  • 3,052
  • 1
  • 27
  • 28