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

chulo
  • 53
  • 8

1 Answers1

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')

Jay
  • 3,640
  • 12
  • 17
  • 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 – chulo Sep 16 '22 at 19:30
  • 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' – chulo Sep 16 '22 at 19:39
  • `-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. – Jay Sep 16 '22 at 19:43
  • 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. – chulo Sep 16 '22 at 19:53
  • @chulo: Hm, if git reset --hard and git clean -f -d -x (or maybe `g.clean('-xdf')` as suggested in https://stackoverflow.com/questions/28569923/git-clean-with-gitpython ) doesn't work before pull, then there's something even more mysterious going on there. What does your `git status` look like? – Jay Sep 16 '22 at 20:33
  • 1
    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? – chulo Sep 16 '22 at 22:14
  • You'd get a slightly different error for an *untracked* file (at least in modern Git; ancient Git might not have distinguished these). I suspect instead that the *.sql file is highly active and/or inappropriately set up as a text file with CRLF editing (even though it's actually binary), so that it is constantly getting modified. – torek Sep 17 '22 at 08:07
  • @chulo torek's has a good point, does `git diff` mention new line changes? If so, https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings ; Another thing that could be wrong is file permissions. Git, by default tracks if files are executable or not. Maybe there is a problem with setting them because of filesystem/security restrictions. If file permissions are the problem then something like `old mode 100644` `new mode 100755` (or vice versa) should show up in the diff. If that's the case, consider `git config core.fileMode false` – Jay Sep 17 '22 at 16:11