Section C.4 Git Won’t Let Me Pull From Upstream or Origin!
Ah, perhaps you forgot to make a branch. This error happens to me a lot when I have a lot of work on the main branch because I never made a separate branch (not smart of me, I know).
Or maybe not. Either way, Git says you have a problem with your code and won’t let you merge from upstream. Maybe you get a message like this:
> git pull upstream main
error: Your local changes to the following files would be overwritten by merge:
<file-name(s)>
Please commit your changes or stash them before you merge.
Aborting
This error occurs when you have local, uncommitted changes and a merge from origin or upstream has changes in the same exact places. Git has no idea whose changes to keep so it does nothing. There are a couple of different things you can do in this situation depending on how you feel about your changes.
Subsection C.4.1 Your Local Changes Are Irrelevant and You Want Them Overwritten
This definitely happens! Perhaps you tried a few things and they didn’t work so you want to pull from upstream or origin and start over. Maybe you corrected some typos and someone else beat you to it so you want to merge their corrections in and keep working. I could go on and on. It’s not difficult at all to force Git to make this happen, just make sure this is what you want to do!
(a)
First, create a backup branch just in case things go haywire:
git branch backup
. Since git branch
does not automatically switch you to that branch, you are free to carry on.(b)
Now, just fetch the changes. Remember that merging and fetching are separate processes. Fetching just collects the changes, attempting to merge will throw the error. Use
git fetch <remote> <branch-name>
. Example: git fetch upstream main
.(c)
Now reset your current branch to the contents you just fetched. This is not a merge, this is a complete reset. You are reverting all of your content to match what you just fetched. This is why we created a backup in case something gets deleted that you didn’t want to be.
In the event that a file you created gets deleted from the reset, just navigate to the backup branch, stage just the missing file and commit just the missing file to origin. Then pull from origin.
But to reset, use
git reset --hard <remote>/<branch-name>
. Continuing the example from above, git reset --hard upstream/main
Subsection C.4.2 Your Local Changes are Good and You Want to Keep Them
Sometimes your changes are valid and you want them to be kept, but you still want to pull in changes. One option is to force a merge conflict and deal with it in your editor, choosing which version to keep. If this is what you want to do, just
add
the troublesome files, commit
them, then pull
. Again, this will force a merge conflict but you can fix things from there.Alternatively, you can keep your changes by using
git stash
. Stashing will store your changes locally without committing and will let you pull. To bring your changes back you can use git stash pop
. In my experience, things have worked out quite nicely with git stash
. I just type git stash
before pulling, then I pull, then I use git stash pop
and my changes are popped right back into where they were. For more information, you might find opensource.com 65 useful.If
git stash pop
doesn’t work, you may have a bigger problem on your hands. You will want to do a Google search if this is your case. This happened to me at one point and unfortunately I had to completely reset my local repository to what was appearing on GitHub. In such an extreme case, I even made a copy of some of my files with my edits before doing any resetting so I could easily add my changes back in.