3

I have a repo that's forked from a remote repository. I made some changes, the remote repository updated and now I want to pull in all changes from the remote repository, and not care about anything in my local repository. Previously I've been deleting my local repository, then doing a simple fork and clone. There's got to be a better way to do this. What's the magic command?

chum of chance
  • 6,200
  • 10
  • 46
  • 74
  • Most of the answers here suggest pulling, which means merging. If you're willing to delete your repository, I'm pretty sure that's not what you want. – Cascabel Apr 12 '11 at 05:14
  • That's correct, I don't care about the merging. reset --hard was what I was looking for. Thanks! – chum of chance Apr 12 '11 at 13:00

5 Answers5

4

If I understand you correctly, you want to throw away commits on your local branch (if you've made any). If that's the case, then you want:

# fetch updated branch(es) from origin
git fetch origin

# reset, including your work tree, to origin's master branch
# make sure you have your master branch checked out first!
# and also that you don't mind throwing away local commits or
# uncommitted modifications
git reset --hard origin/master
Cascabel
  • 479,068
  • 72
  • 370
  • 318
1

Some assumptions: master is the old branch, where you commit some changes. Now other is the fresh checkout from the remote origin.

git fetch origin
git checkout -b other origin/master

With

git diff other..master

you can compare the two branches. And at last with

git checkout other
git merge master

you merge them. Another useful tool here is cherry-pick, with that you can merge only some interesting commits into a branch

git cherry-pick <commit>
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
0

Perhaps my answer here helps

"fetch --all" in a git bare repository doesn't synchronize local branches to the remote ones

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
0

A simple way would be to branch, like this:

git commit -m "All my latest stuff I don't care about"
git branch newstuff refs/remotes/origin/master 
git pull

And now you've got all of your new stuff. Of course this is assuming you want to keep the old stuff.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
0

(Considering your branch is master and the remote is named origin)

First update your origin with:

git fetch origin

Now, if you have not committed since your last update you can simply do this:

git rebase master origin/master

If you have some commits done, this fast-forward rebase won't work. In that case you can do:

git branch -d master (to remove your local master branch)

git checkout -b master origin/master


Diffing before merging:

If you want to see what changed before doing the merge do:

git fetch origin (always to bring the changes from the remote)

git diff master origin/master

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • Git won't be too eager to delete a checked-out branch. Your rebase also doesn't do what you think - it's using master as the base. I think you meant `git rebase origin/master master`. But of course, I wouldn't use rebase, just `git merge --ff-only origin/master`. – Cascabel Apr 12 '11 at 05:13