githubrepositorygit-bashgit-clonerepo

Wanting to add files to an already Existing Repo on GitHub


I am very new to working with Git and GitBash.

I have a repo on GitHub called "RepoA" which already has some files (let's say files from FolderA). I uploaded these on there using add, commit and push in GitBash. Due to some issues I had to delete the ".git" folder in FolderA and now I'm trying to create another ".git" folder in it but the issue is that GitBash is not detecting the already uploaded files. I'm assuming I have to git clone the repo (?) because I read some answers about that. But the issue with that is my branch name "main" (which I actually want to push to; part of RepoA) is appearing as "master" and hence I cannot commit or push anything or there.

My main questions are:

  1. How can I make it so, so that the files in RepoA are tracked by the ".git" folder?
  2. How can I add files from FolderA to my "main" branch in RepoA?

I realize that manually uploading is possible too but I'd really like to experiment using GitBash or even CMD commands.

I tried initializing .git again but again, that resulted in all the files being untracked and me having to manually commit and push again.

I have tried git clone of RepoA but all that does is copy the files from RepoA into my Folder A.


Solution

  • The .git folder is where git keeps all its housekeeping information. If you delete it, what was your working copy before, becomes just a basic folder with files and subfolders. It loses all versioning information, upstream repositories, and so on. So don't mess with .git, unless you know what you are doing.

    If you run git init to create a new .git folder, this is a new repository with no history, no tracked files, and no connection to your github repository. You can do that, but you'll lose all your previous history and the connection to the so called upstream repository on github.

    To get your repository back, I'd do this: rename your local repository folder, then git clone from github into a new folder, outside your existing working tree. As you saw, that gets you what the remote repository (github) has. It also gets you back the version history and all the other git stuff.

    If you made changes to the files in the directory where you deleted the .git folder, the newly cloned repository will not have these changes. You can just copy everything over to the newly cloned repository folder. But don't copy the .git folder, or you overwrite the git metadata again, and you'll have to start over. By doing this you'll lose information about commits between what github has and when you deleted the .git folder, so you lose that history. No way around that, unless you have a backup you can restore. But at least you'll keep your latest changes to any files. git add and git commit them and git push to github as needed.

    As to main vs master: this is just a setting in either github or your local git configuration. You can run this command to get the setting:

    git config --global --get init.defaultBranch
    

    and

    git config --global init.defaultBranch main
    

    The --global means for all your repositories. If you leave that out and run the command in a working tree (repository), it will apply to that repository only.

    You can switch branches in git by running e.g.,

    git checkout main
    

    or

    git switch main
    

    When you commit on a branch that github doesn't know about, you can push the branch and tell github to connect these branches. The autoSetupRemote option makes this automatic; see Automatically track remote branch with git

    git config --global push.autoSetupRemote true