Tuntable
Tuntable

Reputation: 3574

How to pull upstream without any merging/rebasing

I have a private repository that is messed up. Not sure how, very difficult to tell.

I now want to do a

git checkout master
git fetch upstream/master
git make-my-master-the-same-as-upstream-master

No merge, no rebase. I do not want any changes from my master in there. No new files. I want to be confident that my master is exactly the same as upstream/master. No extra files. No merges. The same.

(For the local repository I use rm -rf. But this then needs to be synced to github.)

(There are 100 similar questions with 500 recipes as answers each with subtle differences, but I do not think this has been asked/answered properly. Very crude, clean and start.)

Upvotes: 1

Views: 264

Answers (3)

simryang
simryang

Reputation: 98

I suppose a few things.

  1. You want to get your remote recent master branch to your local master branch.
  2. You wanna ignore any changes of your local master branch.

git checkout master

git checkout master
git reset --hard $(git log origin/master --pretty=oneline | awk 'NR==1{print $1}')
  • Let's move to local master branch first via git checkout master.
  • And make local master branch go back to commit of last remote master which remains local
  • This local master branch has same source with remote upstream master until this commit.
  • So it is just old upstream master now.

git fetch upstream/master git make-my-master-the-same-as-upstream-master

git pull
  • It is same as upstream master now without any conflict.

Upvotes: 1

hyde
hyde

Reputation: 62906

First get all remotes up to date:

git remote update

Then go to the branch you want to reset, here:

git checkout master

Optional: take note of current state, possibly like this, quick&dirty:

git commit -a -m"messed up" # commit all changes
git tag messed_up_backup # store revision as tag

Then set the branch to what you want it to be via hard reset, in this case probably:

git reset --hard upstream/master

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42551

If you'll make your local master (of the messed up branch) to be "exactly the same as upstream/master" - you'll loose all your local changes. If this is what you want to do, there are many ways, one of them is:

  1. Find the commit id that is in your master before the mess (even the initial commit will be fine). Lets say, it has sha1: 123abc (just for example)

  2. Make sure that you're on your local master

git checkout master
  1. Branch is a named pointer at the graph of commits, so move the pointer to the "safe but fairly outdated place". At this point you'll loose all the changes that happened since that time, so be careful.
git reset --hard 123abc
  1. Get the changes from the remote upstream. After executing this command you might be in-synch
gut pull

Note, that if you want to "back up" your changes anyway, maybe to cherry-pick a couple of them afterwards (who knows, maybe they still can be worthy), before doing the step 3, you can merely create a branch out of master with the pointer on the last commit (it will be identical to the messed up master but at least you won't loose the reference after calling git reset during step 3)

Upvotes: 1

Related Questions