21

I have a local checkout of a repository of a fork on my computer (from github). So on github I created a fork, and checked that out.

Now I want the local checkout to be the same as the original github repository (the one I created the fork from). I added that original repo to my local repo (with the name 'orig') and did the following:

git pull orig master

However, git status shows me

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

Also git diff or git diff origin/master shows nothing.

Is there a way to make the local checkout equal to what is in the original master branch (without removing the local repo, the github fork, forking anew, checking out anew...)?

I do not care if there any uncommited changes or anything. I want the local checkout to be equal to the original repo...

Alex
  • 38,938
  • 74
  • 207
  • 406
  • Possible duplicate of [git: sync local repo with remote one](https://stackoverflow.com/questions/6373277/git-sync-local-repo-with-remote-one) – phd May 01 '18 at 14:43

3 Answers3

29

You can reset your local branch to the one in orig, then push to your fork

git checkout master
git reset --hard origin/master
git push --force origin master

Your local repo and fork master branch will be the same as the original repo.

If need clean: git clean -f -d

SwissCodeMen
  • 3,359
  • 4
  • 15
  • 26
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • 1
    Do i need to save my local changes before executing above commands ? – Deepak Yadav Apr 11 '19 at 02:43
  • 1
    @DeepakYadav Yes: at minimum a `git stash`, or even a `git add` + `git commit` is advisable. After a `reset --hard`, you will still able to get your changes back by cherry-picking that commit from the reflog, or by pop stashing https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-pop--index-q--quietltstashgt. – VonC Apr 11 '19 at 06:15
3

Use the command below :

git reset --hard HEAD

Deepak
  • 674
  • 4
  • 11
1

I have had the same issue a couple of times now and the below is the solution I wrote down for myself. I don't know all the details but I know it got me out of trouble :).

Rolling back on Commits. (run "git status" between each step)

  • e.g. "Your branch is ahead of 'origin/dev' by 2 commits."
  1. git reset HEAD^:
  • This removes one committed file at a time (I think)
  • There will be a bunch of (red) staged/untracked files. Don't panic.
  1. "Your branch and 'origin/dev' have diverged, and have 1 and 5 different commits each, respectively."
  • Repeat step 1. until git status "Your branch is behind 'origin/dev' by 5 commits"
  • Now we want to remove all those changes so we can later pull origin back in.
  1. git restore *
  • Removes all the staged changes
  • Now there may just be some Untracked files. Manually go delete them in the folder.
  1. git pull origin dev.
  • This should be the latest dev pulled from origin.