Say I have an arbitrary local git working directory that was cloned from some remote. I want to make the local git repository be EXACTLY the same as the remote, irrespective of what’s happened to the local directory since it was cloned. I do not want to do another full clone.
Say the local working directory has:
- additional untracked files
- deleted files
- staged changes.
- may be on some arbitrary branch.
- have a bunch of commits since being cloned from remote
Now I want to make this local repository reflect remote exactly. Here’s my current solution but I’m not sure if it covers all cases and whether there is an easier solution:
git stash
git clean -f -x -d
git checkout master
git fetch origin
git reset --hard origin/master
git pull origin master
Is there a better way to do this?
will dump any changes that you have including any you have staged.
will get rid of anything in your working directory that git is not tracking due to ignore or exclude.
will put you on no branch so you can clean everything out.
will delete all your local branches
will delete all your tags
will get rid of any tracking branches that are no longer on the remote
will get any branches and tags from the remote that you don’t have. You can stop here and checkout any remote branches you want to work on while creating a local branch with
or you can create local branches that point to where all existing remote branches are pointing to as of your fetch with