1

I am using the following code using gitpython:

g = git.cmd.Git(r'C:\Users\alex\Files\Repo\Scripts')
g.reset('--hard')
g.pull()

but I get the following error:

GitCommandError: Cmd('git') failed due to: exit code(1)
  cmdline: git pull
  stdout: 'Updating c169660..ebe18ef'
  stderr: 'error: Your local changes to the following files would be overwritten by merge:
    Scripts/p_CBMAPPING.sql
Please commit your changes or stash them before you merge.
Aborting'

I essentially want to have the remote repo to override the local repo. This code actually works most of the time but every once in a while it gives me these errors. It is strange because I have not even touched these local files so not sure why it thinks there are changes here to be kept. Files in Remote repo, however, have changed. I want that change in remote to override local. How could I avoid this error in the future? Thanks

1 Answer 1

0

My guess is that the hard reset works, but that Scripts/p_CBMAPPING.sql is an untracked file.

git reset (even if hard) will not remove new/untracked files.

After the reset you could stash those files away using g.stash('push', '-u')
or, if you feel brave enough, delete those files with g.clean('-f', '-d')

Sign up to request clarification or add additional context in comments.

8 Comments

interesting. thanks to your comment I realized that p_CBMAPPING.sql has been removed from remote repo. In that case, I no longer want it in my local repo. I want an exact copy of the remote. Will g.clean('-f', '-d') get rid of that file in my local repo then? what do -f and -d do in this case? Thank you
well, I guess it didn't work. I tried g.clean but I got a similar error: GitCommandError: Cmd('git') failed due to: exit code(1) cmdline: git pull stdout: 'Updating c169760..ebe68ef' stderr: 'error: Your local changes to the following files would be overwritten by merge: Scripts/p_CBMAPPING.sql Please commit your changes or stash them before you merge. Aborting'
-f will force removal, -d will cause git to recurse into untracked directories. Maybe Scripts/p_CBMAPPING.sql is a file mentioned in your .gitignore? If so, then you need to add -x to git clean. That will remove ALL (untracked) ignored files. So make sure that's what you want.
I tried the -x and still getting error. it is actually the g.pull() that gives me the error. when I run g.reset and g.clean without g.pull I don't get the error, but then local repo does not match remote.
Yes, I just found out from a coworker, he just did a git clone of remote repository, and right away there are 5 files in the new local that need to be committed. we checked one file and remote repo in git hub is not showing the same as the file in local repo that was just cloned seconds ago from the remote. Like you said, that falls into the mysterious. can you think of anything that could cause that?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.