Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

James Barrett
James Barrett
13,253 Points

Git: Trying to pull latest changes to a branch to my computer however receiving this error

Hi there,

Working on two computers, my usual workflow consists of:

Pull > Work > Commit > Push to GitHub <whatever-branch>

This has been working fine so far. However I have encountered an error on my latest pull:

Your local changes to the following files would be overwritten by merge: functions.js. Please commit your changes or stash them before you can merge.

I have tried asking Google to see if I can get an answer, however, being new to Git I am struggling to understand all of the commands and terminology and I want to be sure of what I am doing before I execute something.

Could someone help me understand what is going on here?

Thanks, James.

1 Answer

Hi James,

You have two versions of your work. One is in the remote repository from the other machine and the second is in your local machine that you are trying to pull to.

The message is saying that you have changed functions.js on the local machine. If you pull the remote version, those changes will be overwritten. So it aborts the pull/merge.

If the remote version is what you want to keep, try something like git checkout -- functions.js That reverts the changes to the file. Or you can use git reset HEAD functions.js which unstages the changes. If you don't need the local changes, use the first one.

I hope that helps.

Steve.

James Barrett
James Barrett
13,253 Points

Ah I think I get you. So essentially what Git was saying was "Hmm, that doesn't make sense, I can't pull something down if I see code which has not been committed yet." So I needed to add and commit those changes I made and on the computer I want to pull down to before actually pulling from GitHub, am i correct in saying that?

Yes. You have worked on files locally and those changes will be overwritten by the remote pull. Check which version of the file functions.js you want. It is possible you have changes in both places which gets complicated and confusing!

If you commit the local stuff to the remote, that will add the local changes but it may conflict with remote changes too. If it does, your code will show where those conflicts are and you can amend them. If you don't need the local changes get rid of them using checkout -- and then pull the remote down. If you do want the local changes, try adding & committing and see what issues you may get when you try to pull the remote.

Ensuring you pull changes first, then start working locally helps avoid this but it is easy to forget! Let me know how you get on.