2

I’m using Git on Mac 10.9.5 and would like to pull down the latest version of what is in a branch. I don’t care about any local changes I’ve made — I’m happy to abandon all of those. Right now when I run

git pull origin branch-name

I get errors like

Please, commit your changes or stash them before you can merge.
error: The following untracked working tree files would be overwritten by merge:
    src/main/java/com/myco/classroom/dto/ClassroomCourseDto.java
    src/main/java/com/myco/clever/dto/CleverClassroomUserDto.java
    src/main/java/com/myco/clever/exception/MissingUserException.java

I don’t want to commit anything — I just want to get a fresh copy free of any local modifications I’ve done. How do I do this?

5 Answers 5

3

In case you did something wrong, you can replace local changes using the command

git checkout --

this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.

If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this

git fetch origin git reset --hard origin/master

Visit this site if you need more help with git: http://rogerdudler.github.io/git-guide/

2

You have local changes that you need to "stash" before pulling changes.

One way is:

git stash
git pull origin branch-name
git stash pop

And now you'll be asked to merge your local changes with the changes you got from server.

Note that you don't have to stash pop once you pull, you can simply leave the stash without the need to merge your previous changes.

2
Git force pull to overwrite local files

git fetch --all
git reset --hard origin/master
git pull origin master
0

If you don't mind losing any changes, you can also git checkout the files with changes, but that could take more time depending on the amount of changes you've made, and it won't save the changes for future reference like git stash does.

0

git stash is what you want to use.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

There's even an example similar to your situation:

Pulling into a dirty tree

When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing. When your local changes do not conflict with the changes in the upstream, a simple git pull will let you move forward.

However, there are cases in which your local changes do conflict with the upstream changes, and git pull refuses to overwrite your changes. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:

$ git pull
 ...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.