daveanderson88
daveanderson88

Reputation: 347

Bringing Newly Git Initialised Repo In Line With Origin Without Loosing Working Changes

I have a previously not git tracked directory which follows the same folder/file structure as a repo.

It is exactly the same as the repo, apart from some extra files (ignored by .gitignore in the origin repo), as well as a couple of new files.

I did the following to link it up to the origin repo:

git init
git remote add origin [the repo url]
git fetch
git co -b new-branch-to-commit-the-2-new-files-onto

I know I need to pull from origin, so git knows that only these 2 new files are different from the origin repo.

If I try git pull origin master, I get this message: "The following untracked working tree files would be overwritten by merge:... Please move or remove them before you merge." - which then lists EVERY file in the project.

If I remove all the files from the directory, I will delete some files which should not be deleted (the ones listed in .gitignore and also the 2 new files).

To summarise, I want to pull and merge master branch into my newly initialised repo. Any files in the origin repo can overwrite what I have locally, but I do not want any local files deleted.

I was hoping the following would have this effect:

git pull origin production -s recursive -X ours

but it gave the same "Please move or remove them before you merge." message.

Thanks

Upvotes: 0

Views: 52

Answers (1)

SteffenJacobs
SteffenJacobs

Reputation: 412

The easy solution would be to clone the existing remote repository into another folder and copy your two new/changed files over manually. Then you can commit and push them.

When you did git init in your local file system, you created a new git repository from scratch with an entire new history and commit graph. This new graph is now clashing with the graph from the remote repository which contains all the file changes done before in all the commits. Since the two graphs do not have any commits in common (although the files are mostly identical), you cannot really merge them.

In addition to that, git does not allow you to merge anything when you have a dirty worktree (=non-commited files). You would have to commit your changes first which will then result in the situation described above. Therefore, your local files can only be overwritten (=deleted) during the merge.

Upvotes: 1

Related Questions