0

I had some troubles with Git. So, I deleted manually all files in my local repository in order to clone all the files from my Github repository (I replaced every files of my local repository with the files from the online Github repository). The cloned files are located in the same place as before. Then I used the command:

git pull

and the terminal answers:

error: Pull is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

Then I do:

git status

On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 5 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

    new file:   src/main.cpp
    new file:   src/render.dia
    new file:   src/render/Scene.h
    new file:   src/state.dia.autosave
    new file:   src/state.h
    new file:   src/state/Status.h

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   rapport/rapport.odt

I do not understand why I have an unmerged file since I have just cloned the repository from GitHub to my local repository. In addition, I did not modify the files:

  • src/main.cpp
  • src/render.dia
  • src/render/Scene.h
  • src/state.dia.autosave
  • src/state.h
  • src/state/Status.h

Then I tried:

git reset --hard HEAD~1
HEAD is now at 9a68811 Revert "f"

and:

git pull
warning: Cannot merge binary files: src/render.dia (HEAD vs. e89ed43391b963508ea8aecfb7012ae7dda71cb0)
warning: Cannot merge binary files: rapport/rapport.odt (HEAD vs. e89ed43391b963508ea8aecfb7012ae7dda71cb0)
Auto-merging src/render/Scene.h
CONFLICT (add/add): Merge conflict in src/render/Scene.h
Auto-merging src/render.dia
CONFLICT (add/add): Merge conflict in src/render.dia
Auto-merging src/main.cpp
CONFLICT (add/add): Merge conflict in src/main.cpp
Auto-merging rapport/rapport.odt
CONFLICT (content): Merge conflict in rapport/rapport.odt
Automatic merge failed; fix conflicts and then commit the result.

How can I make my local version of master match the remote version of master?

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
cephal
  • 15
  • 1
  • 7
  • 1
    What are you trying to accomplish here? Do you just want your local branch to exactly match the remote? – Tim Biegeleisen Oct 18 '16 at 16:35
  • Yes, I just want my local branch exactly match with the remote. – cephal Oct 18 '16 at 16:38
  • 1
    A Git *repository* consists of a database (or object base) of files in `.git/objects` plus a bunch of ancillary control files also in `.git`. A checked-out commit in your *work-tree* consists of the work tree itself, plus the "index" (or "staging area") in `.git/index`. It seems that you deleted some or most of the work tree without deleting all the ancillary files and/or index. That puts you in a somewhat wacky situation: you did not actually delete your clone, you just messed with it. – torek Oct 18 '16 at 16:39
  • Do I have to delete the hidden files: .git and .gitignore before to clone the remote repository again? – cephal Oct 18 '16 at 16:44
  • 1
    Off the top of my head, given that we don't know really where your branch is, my advice would be to _delete_ your local branch, and just checkout a fresh one from the tracking branch. – Tim Biegeleisen Oct 18 '16 at 16:47
  • I do not use my own branch. Me and my friend work only on the master branch (it is a school project and we are not used to use Git). – cephal Oct 18 '16 at 16:53

1 Answers1

1

As @torek mentioned, deleting the files in the working directory is not the same as deleting your repo. If you really want to do that, then you also need to remove the .git directory, or even better, delete the parent folder entirely.

However, it shouldn't ever be necessary to delete and re-clone a Git repo, so avoid doing that if possible.


In your case, the local and remote versions of master have diverged, leading to a history that might look something like this:

*--*--X--A--B [master]
       \
        C--D--E--F--G [origin/master]

(Except after you did git reset --hard HEAD~1, master will be at A instead of B.)

To wipe out your local commits, you can do:

git reset --hard X

Or, since you are already at commit A, simply:

git reset --hard HEAD^

You can then pull in the updates from origin:

git pull origin master

In the future, if do you want to keep your changes, but want to have a linear history rather than merges, you can do:

git pull --rebase

This applies your local commits on top of the remote commits, like so:

*--*--X--A--B
       \
        C--D--E--F--G [origin/master]
                     \
                      A'--B' [master]

(You may still have to deal with conflicts.)

You can then push your commits to origin as usual (git push origin master).

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
  • Thank you! It works perfectly by deleting the parent directory and replacing by the cloned one. – cephal Oct 19 '16 at 07:22
  • @cephal Great, glad I could help! Be sure to [accept my answer](//stackoverflow.com/help/accepted-answer), as this indicates to others that your problem is solved, and also gives you 2 rep! (You can also [upvote](//stackoverflow.com/privileges/vote-up) it if you have enough [reputation](//stackoverflow.com/help/whats-reputation).) – Scott Weldon Oct 19 '16 at 17:03