-1

few months ago I moved a project from one server to the other. I just noticed that the .git folder disappeared.

I want to connect that folder back again to the remote repo on GitHub. I tried to follow this solution, but it doesn't work in my case.

I created a new git init. The I run git remote add origin https://github.com/myusername/theRepo

Then if I run git status I get all the files I have in red as Untracked files: :

git status
On branch master

No commits yet

Untracked files:
...........

Then I run: git fetch --all

Fetching origin
Username for 'https://github.com': myusername
Password for 'https://myusername@github.com':
remote: Enumerating objects: 171, done.
remote: Counting objects: 100% (171/171), done.
remote: Compressing objects: 100% (111/111), done.
remote: Total 171 (delta 39), reused 162 (delta 30), pack-reused 0
Receiving objects: 100% (171/171), 3.98 MiB | 2.61 MiB/s, done.
Resolving deltas: 100% (39/39), done.
From https://github.com/myusername/myRepo
 * [new branch]      master     -> origin/master

then I run git branch and there is no output. git status still says all my files are untracked.

In these months few of my local files are newer than the repo. Were edited locally (it's a wordpress theme).

How can I make it to see only the files that are different be in red so that I can push to the repo only the files that are really different?

Or maybe it's better to create locally a new branch, push everything to the new branch, and then compare the two branches in github and in case of need -> merge?

Either way, can anyone please help to make it happen?

If I run git checkout master:

    git checkout master
    error: The following untracked working tree files would be overwritten by checkout:
and here the big list of files (all files)

then if I run again:

 git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
Pikk
  • 2,343
  • 6
  • 25
  • 41

2 Answers2

2

Your second impulse was the right one. Create the .git repo with init, rename master to something else like temp, add everything, and commit. Add your remote, fetch, and checkout master. Now merge, allowing unrelated histories and using the theirs option.

So delete the .git repo and start over:

% rm -rf .git
% git init
% git branch -M master temp
% git add .
% git commit -m'temp'
% git remote add origin git@github.com:you/yourrepo.git
% git fetch
% git switch master
% git merge --allow -X theirs temp
% git branch -d temp
% git push origin master
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I followed, but I got until this point and got error: `git switch master` `fatal: invalid reference: master` – Pikk May 05 '21 at 21:00
  • Maybe git checkout origin/master – matt May 05 '21 at 21:05
  • `git checkout origin master` `error: pathspec 'origin/master' did not match any file(s) known to git` – Pikk May 05 '21 at 21:19
  • You said you could do the fetch and that you got `From https://github.com/myusername/myRepo * [new branch] master -> origin/master`. Is that not true? Maybe you could edit your question to show the whole conversation with Git, both what you said and what Git replied. – matt May 05 '21 at 21:21
  • If I run now `git fetch --all` it asks for my github username and password. When I put the credentials it connects and.... ` * [new branch] master -> origin/master` – Pikk May 05 '21 at 21:23
  • Then you do have `origin/master`. – matt May 05 '21 at 21:23
  • I did exactly what you said. First with the mistake `origin master` then correctly `git checkout origin/master` as you can see I edited the comment immediately after I realized that spelling mistake. – Pikk May 05 '21 at 21:27
  • If I run `git branch -a` I get: `* temp` and `remotes/origin/master` – Pikk May 05 '21 at 21:28
  • that is totally cool and `git checkout origin/master` should work - unless maybe you are using some really old version of Git? Maybe we could be very literal and say `git branch master origin/master` and then `git checkout master`. Sorry, I can't figure out why this is a blocker; I assure you I actually enacted this whole thing and it worked fine exactly as I have it. – matt May 05 '21 at 21:29
  • I am running `git version 2.30.0. Now that I run again `git checkout origin/master` -> `Note: switching to 'origin/master'. You are in 'detached HEAD' state.` and other things, ending with `HEAD is now at c98c2e8 done`. What would be the next step? – Pikk May 05 '21 at 21:32
  • Well then say `git branch master` and `git checkout master`. – matt May 05 '21 at 21:32
  • `Switched to branch 'master'` - `git status` says `On branch master nothing to commit, working tree clean` – Pikk May 05 '21 at 21:33
  • Yep, so on you go with the `merge` etc. – matt May 05 '21 at 21:33
  • Is there any way I can do the merge in GitHub? Here I have many files and I have access to the server only via CLI, no IDE that I can use to help in the merge – Pikk May 05 '21 at 21:35
  • If you do what I said the merge will just happen. You disconnected from GitHub and worked locally only so the local version should be "right", and that is what my `merge` assumes. – matt May 05 '21 at 21:36
  • after running `git merge --allow -X theirs temp` I get `Automatic merge failed; fix conflicts and then commit the result.` – Pikk May 05 '21 at 21:38
  • well darn, that was what `theirs` was supposed to prevent :( for now you'd better abort the merge I guess – matt May 05 '21 at 21:41
  • You can probably push `temp` to GitHub, turn that into a pull request, and work on it using the web interface to GitHub, but I'm not sure what happens about the unrelated history – matt May 05 '21 at 21:42
1

to see all your branches, go like this after you fetch

git branch -a

You don't have any of your own branches, but all the branches on origin (github) are downloaded, and have names that start with origin/

something like this might work, but maybe back up your whole folder first:

git checkout -b new-branch # I don't know how necessary this is
git reset origin/master # make new-branch the same as master
git checkout master # now we can probably just swatch underneath to master

If the last command doesn't work, you might still see what you want to see

I haven't tried it, but that is probably what I would do.


Another option is just to git clone the repo into another folder, and then copy all of your files into that folder with master already checked out (but make sure you don't copy .git that you just created

cd ..
mv theRepo theRepo.bak
git clone https://github.com/myusername/theRepo
git checkout master # probably done already
cp -r ../theRepo.bak/* .

that should copy everything that doesn't start with a ... if you have files that start with a . that you need to update, then maybe grab them one by one so you don't accidentally grab the .git directory from your last init and fetch

Alex028502
  • 3,486
  • 2
  • 23
  • 50