0

I am not asking why I cant push to a repo. I am asking what is the proper way to push a new local project to an existing repo that has a ton of different code and a lot of code from the existing project has been removed. Meaning I cant pull the project because it would add all the old code back.

I have a git project.

Project A.

I made some major changes to it so I created a new project on my localhost and copied in some of the code to make changes to it. Now that I have refactored my project I want to push this project

Project B.

To be the newest commit of project A. This way I can maintain the history of the project. Instead of removing the old project.

Question

How can I push project B. to the newest commit of Project A.

What I Tried

I opened .git/config in project B. and changed the URL to project A.'s.

Then tried to push the project to that existing repo and I got this,

error: failed to push some refs to 'https://example.com/example/web-application.git' 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.

But if I pull in that repo I will be adding all the code Im trying to remove.

wuno
  • 9,547
  • 19
  • 96
  • 180
  • What don't you understand from the error message? – jonrsharpe Mar 03 '17 at 20:09
  • I understand the error message. I am not asking what the error message means. If I git pull it will pull all the code from project A into the project. The whole point is I want to push project b which has a ton of project A. code removed to be the new head of project A> – wuno Mar 03 '17 at 20:09
  • Then what do you still need to know? Have you done what the message suggests? Read any of the dozens of existing questions that tell you how to resolve this? – jonrsharpe Mar 03 '17 at 20:10
  • If my question offends you I apologize. That was not my intent. Possible this is my fault and I am not asking my question clear. I am trying to push a different project to be the new head of project A. I do not want to pull the old code into it. I want to just be able to push project B into the existing project to be the head so all of the code that has been removed from does not show up going on from this point. – wuno Mar 03 '17 at 20:12
  • 1
    Possible duplicate of [Cannot push changes to repository (GIT)](http://stackoverflow.com/questions/35950842/cannot-push-changes-to-repository-git) – jonrsharpe Mar 03 '17 at 20:12
  • This is not a duplicate of that question man. I would really appreciate if you would read my whole question and then either show me where my question is answered. – wuno Mar 03 '17 at 20:13
  • 1
    Yes, it is. You have the same problem; your local doesn't match the remote. The second answer does exactly what you want, forcing replacement of the current remote content. – jonrsharpe Mar 03 '17 at 20:13
  • Well are you still saying that me and that guy are asking the same question? I know I can force push a repo. But is that the correct way to handle this specific situation? I can not find anyone that has asked my question which is why I am asking it. If you are admitting that my question is different than the one you marked duplicate than I would appreciate if you would either update with a correct duplicate or answer the question. But marking a duplicate because the answer to a different question happens to work for my question does not call for a duplicate . – wuno Mar 03 '17 at 20:17
  • I'm no, I'm saying they have the same answers. That's what duplicate means here. It doesn't matter how the repo got in that state, either you resolve the conflict or ignore it, and from your description you want to ignore it. If you know you can force push, why didn't you just do that (or at least mention it)? – jonrsharpe Mar 03 '17 at 20:18
  • Because I have read that force pushing can result in a loss of commits from others who have pushed to the same branch. So before doing it I wanted to clarify that this was the correct and only way to handle this situation. Thanks for your help by the way. – wuno Mar 03 '17 at 20:21
  • Yes, it can. But you said you *didn't* want to pull the old code into it. If you actually do, then you do need to resolve the conflicts, but if you don't actually know which you want or need to do we can't tell you here, you need to talk to the other committers. But when you decide which, both have answers on the duplicate. – jonrsharpe Mar 03 '17 at 20:23
  • So if the project is completely up to date and I push we can all pull from here. Also if we want to revert back to an old commit we will still be able to do that without any issues? – wuno Mar 03 '17 at 20:24
  • This seems fruitless. Best of luck. – jonrsharpe Mar 03 '17 at 20:25
  • Thank you for your help. I really hope your day goes better from here. I think it is safe to assume you do not know the answer to this question. So if you do come across a similar question or the answer I would appreciate you updating me. THANK YOU! – wuno Mar 03 '17 at 20:27
  • It's not clear to me what you're finding so difficult to understand. There are many similar questions, searching for the message git is giving you will find them, but you'll *keep finding the same answers*. Again, you need to decide whether you want to keep the remote history or the local history. You need to decide which commits you want to be able to go back to. Nobody outside your team can tell you which; go and talk to the other people who might be interacting with the same repo and explain what you've done. – jonrsharpe Mar 03 '17 at 20:38
  • I do not want to lose all of my repo's history. If I git push --force I will. I am asking how I can push the new project to the repo while maintaining all of my repo's history and commits. So I can revert back to anypoint at anytime. – wuno Mar 03 '17 at 20:44

1 Answers1

0

The way you have done things has gotten your repositories into a bit of an odd state. If you truly want to preserve all history, it would have been best to retain all the code and delete it from the local repo, then push those changes. It sounds like you've tried to shortcut that and now you're having issues syncing. To be a little blunt, I'm surprised git is handling this as well as it is.

The most graceful solution will be to follow what the hints suggest, do a git pull, resolve conflicts, and then push. It sounds like this will be a lot of work, but this is best practice.

If there's a discrete group of people working on this repository you can easily communicate with and you are certain what you are pushing is what you want, then you can do the git push -f origin master.

Ilion
  • 6,772
  • 3
  • 24
  • 47