Search code examples
gitgit-mergegit-pushgit-pull

overwriting some files in git remote


I am using git and apparently I screwed something up.

So, I have my local PC (called A), then my local PC (called B), and my remote.

After a series of of events (forgot to pull stuff, and then worked locally and so on) the situation goes like this :

  • remote has a copy
  • both of my local files have different changes
  • in the last week I have been making changes from local PC B. So local PC A has rev 2 let's say, and local PC B has rev 11 (latest of server) but both of them have different local changes

On my attempt to see what to do, I made things even worse.

  • I pushed data from local PC A (maybe forcing it - I am not sure)

But these removed all my other changes. I don't know how I managed that.

Not only that, but all my commits made from PC B (rev 3 to 11) lets say are gone from history - I cannot see them anywhere.

So, I have my remote and local PC to have the same data - and local PC B to have changes in it.

I know the files I have changes these two weeks - so I am trying the following (lets say it is one file - file.php)

git add file.php
git commit -m "test"

no changes added to commit

whey trying to push something I see something like this :

error: failed to push some refs to 'https://github.com/<url>'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So what I need to do?

What I want to do is - just push this file to my remote and then I will just do git pull to sync everything.

I am doing stuff from command line, not using any GUI since I am working on VirtualBox.

Also, if I do :

git status

I see one file as modified.

Then If I commit this one - everything goes fine but I cannot push that into server because of the previous error. Mind that this behaviour only happens for files that belong to a folder that does not exist at all in the remote branch.


Solution

  • Your main problem seems to be that you have "lost" commits on PC B.

    You have probably forced pushed them from A and then pulled those changes in B, and by doing that lost track of your changes on B.

    You should be able to find the lost commits by running

    git reflog
    

    On PC B. This will show the changes of the HEAD pointer on PC B. Examine the output. It should contain commit messages and suchlike. When you find the rev 11 commit on PC B, do a

    git reset --hard <hash of rev 11>
    

    This will take you to the lost commit on PC B.

    To be able to push these changes, you must resolve any conflicts with your remote. Do this by making a standard

    git pull
    

    This should trigger a merge between your local changes and the changes on your remote. When you have resolved those you should be able push again.