Lucas Smith
Lucas Smith

Reputation: 231

Git Error while merging

I have a lot of untracked files in my local git repo. Someone accidentially pushed a lot of those untracked files. Now, I want to merge origin/master and I receive:

error: The following untracked working tree files would be overwritten by checkout

I want that untracked files stay in my repo as untracked and I do not want to delete them. The files shouldn't be on origin/master. Is there any elegant way to solve the issue from my side?

Upvotes: 3

Views: 194

Answers (3)

Lucas Smith
Lucas Smith

Reputation: 231

What about the following steps:

On my local master:

git stash --include-untracked

git reset --hard origin/master # (origin/master is fetched and it has all of the unnecessary files)

git rm {all of the unnecessary files that were pushed accidentially}

Edit your .gitignore to include all those files you don't want to track.

git add .gitignore

git commit

git push

Now I do not have any untracked files on local master and origin/master.

I can get my untracked files back with:

git stash apply

What do you think?

Upvotes: 3

Philipp Claßen
Philipp Claßen

Reputation: 44009

You could commit your files to a local branch. You can later restore the files by switching to the other branch and executing git reset HEAD^ (without the --hard option!). Once you have restored the untracked files, you can checkout the (hopefully fixed) master again, and your untracked files should still be there.

But why not revert just the changes from the other guy, first? Of course, it has been pushed, but he can also push the revert. Then you should be fine, again.

Upvotes: 3

Ruslan Osipov
Ruslan Osipov

Reputation: 5843

Clean way is to clone a repo to separate directory, remove untracked files from the repository (or revert the commit), push, remove cloned repository.

Upvotes: 2

Related Questions