So I git cloned a repo and wasn't thinking at the time so I didn't make a git branch. I made a lot of changes and don't really need to keep them and at this point I just want the latest stuff from the remote repo. Is there a way I can git merge and now have to deal with the diffs that will show up between the latest remote repo and the changes I've made to master? I just want the latest remote repo master and nothing else.
4 Answers
git checkout master
git fetch
git reset --hard origin/master
You can also use git branch -D <branch name>
to delete any extra branches you don't need.

- 75,175
- 8
- 100
- 122
-
does git fetch point your origin/master to the latest commit and git reset --hard point your local master to that commit too? – akantoword May 23 '16 at 20:58
-
1Yes, `git fetch` will update your `origin/master` (the "remote tracking branch") to the current value of `master` on `origin`. The `reset --hard` makes the current branch (now `master` after the `git checkout`) and working directory match that commit. – dahlbyk May 23 '16 at 21:00
-
I thought usually the reset --hard will change your staging and working directory to match the latest commit in your local repo, not your remote tracking branch. how does git know which latest commit to change to? – akantoword May 23 '16 at 21:07
-
oh is it because you explicitly state origin/master? if you didn't it would reset to the local's latest commit, correct? sorry if I'm asking basic questions – akantoword May 23 '16 at 21:09
-
1Yes, the command is `git reset [
] [ – dahlbyk May 24 '16 at 03:18]`. If you omit ` `, it defaults to `--mixed` (reset `HEAD` and index to match ` `). If you omit ` `, it defaults to `HEAD`. So `git reset` alone resets the index to match the last commit. `git reset origin/master` resets the index and current branch to match `origin/master` (but doesn't touch the working directory). Adding `--hard` also resets the working directory to match the specified commit.
You could delete and changes not committed
git reset --hard
And then pull the new stuff
git pull

- 8,966
- 5
- 34
- 34
-
-
1Deletes all unsaved changes. So anything you didn't `git add` and `git commit` will be undone to last saved commit – HarlemSquirrel May 23 '16 at 21:03
It depends on whether you've committed them:
Yes, I committed my changes
You can simply pull using the --rebase
argument, which will apply all commits from the remote before your own commits (or try to at least):
git pull --rebase origin master
No, I haven't committed my changes yet
In this case you'd want to utilize the stash
command, which takes your unstaged changes and saves them away from the working directory. You can apply them again afterwards:
git stash
git pull origin master
git stash pop
# merge any conflicts
I don't care about my changes
Your question suggests two approached - one: save my local changes and apply them over the updated master (see above), two: don't care about my local changes and just reset to origin. In this case, a simple reset will do:
git fetch
git reset --hard origin/master
-
if I use the rebase argument, will it automatically bring me to the latest commit from the origin/master onto my working directory as well? – akantoword May 23 '16 at 21:10
-
1Yep - it will just re-apply any local commits you have over the top of them – scrowler May 23 '16 at 21:37
If you already committed those undesired changes you should check for the last commit from the upstream using git log
and continue by resetting to this point with git reset --hard <HASH>
.
In case you did not already fetch the latest from the upstream git reset --hard origin/master
would be even easier.
An even quicker (and dirtier) solution would be so simply delete your directory and clone again - yet not recommended.